forked from neos/neos-development-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFusionSourceCodeFactory.php
110 lines (97 loc) · 4.02 KB
/
FusionSourceCodeFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
declare(strict_types=1);
namespace Neos\Neos\Domain\Service;
/*
* This file is part of the Neos.Neos package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\ContentRepository\Core\NodeType\NodeType;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Neos\Flow\Package\PackageManager;
use Neos\Fusion\Core\FusionSourceCode;
use Neos\Fusion\Core\FusionSourceCodeCollection;
use Neos\Neos\Domain\Exception as NeosDomainException;
use Neos\Neos\Domain\Model\Site;
/**
* @internal For interacting with Fusion from the outside a FusionView should be used.
*/
class FusionSourceCodeFactory
{
/**
* @var array<string, mixed>
*/
#[Flow\InjectConfiguration("fusion.autoInclude")]
protected array $autoIncludeConfiguration = [];
#[Flow\Inject]
protected FusionAutoIncludeHandler $fusionAutoIncludeHandler;
#[Flow\Inject]
protected ContentRepositoryRegistry $contentRepositoryRegistry;
#[Flow\Inject]
protected PackageManager $packageManager;
#[Flow\Inject]
protected ObjectManagerInterface $objectManager;
public function createFromAutoIncludes(): FusionSourceCodeCollection
{
$sourcecode = FusionSourceCodeCollection::createEmpty();
foreach (array_keys($this->packageManager->getAvailablePackages()) as $packageKey) {
if (isset($this->autoIncludeConfiguration[$packageKey]) && $this->autoIncludeConfiguration[$packageKey] === true) {
$sourcecode = $this->fusionAutoIncludeHandler->loadFusionFromPackage($packageKey, $sourcecode);
}
}
return $sourcecode;
}
/**
* @deprecated with Neos 9 - YAGNI from the start :)
*/
public function createFromSite(Site $site): FusionSourceCodeCollection
{
return FusionSourceCodeCollection::tryFromPackageRootFusion($site->getSiteResourcesPackageKey());
}
/**
* Generate Fusion prototype definitions for all node types
*
* Only fully qualified node types (e.g. MyVendor.MyPackage:NodeType) will be considered.
*
* @throws NeosDomainException
*/
public function createFromNodeTypeDefinitions(ContentRepositoryId $contentRepositoryId): FusionSourceCodeCollection
{
$contentRepository = $this->contentRepositoryRegistry->get($contentRepositoryId);
$fusion = [];
foreach ($contentRepository->getNodeTypeManager()->getNodeTypes(false) as $nodeType) {
$fusion[] = $this->tryCreateFromNodeTypeDefinition($nodeType);
}
return new FusionSourceCodeCollection(...array_filter($fusion));
}
/**
* Generate a Fusion prototype definition for a given node type
*
* A prototype will be rendered with the generator-class defined in the
* nodeType-configuration 'fusion.prototypeGenerator'
*
* @throws NeosDomainException
*/
private function tryCreateFromNodeTypeDefinition(NodeType $nodeType): ?FusionSourceCode
{
$generatorClassName = $nodeType->getConfiguration('options.fusion.prototypeGenerator');
if ($generatorClassName === null) {
return null;
}
if (!class_exists($generatorClassName)) {
throw new NeosDomainException('Fusion prototype-generator Class ' . $generatorClassName . ' does not exist');
}
$generator = $this->objectManager->get($generatorClassName);
if (!$generator instanceof DefaultPrototypeGeneratorInterface) {
throw new NeosDomainException('Fusion prototype-generator Class ' . $generatorClassName . ' does not implement interface ' . DefaultPrototypeGeneratorInterface::class);
}
return FusionSourceCode::fromString($generator->generate($nodeType));
}
}