-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathNodeCreationService.php
More file actions
189 lines (165 loc) · 8.04 KB
/
NodeCreationService.php
File metadata and controls
189 lines (165 loc) · 8.04 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace Flowpack\NodeTemplates\Domain\NodeCreation;
use Flowpack\NodeTemplates\Domain\ErrorHandling\ProcessingError;
use Flowpack\NodeTemplates\Domain\ErrorHandling\ProcessingErrors;
use Flowpack\NodeTemplates\Domain\Template\RootTemplate;
use Flowpack\NodeTemplates\Domain\Template\Templates;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Domain\Model\NodeType;
use Neos\ContentRepository\Domain\Service\Context;
use Neos\ContentRepository\Domain\Service\NodeTypeManager;
use Neos\Flow\Annotations as Flow;
use Neos\Neos\Utility\NodeUriPathSegmentGenerator;
/**
* Declares the steps how to create a node subtree starting from the root template {@see RootTemplate}
*
* The steps can to be applied to create the node structure via {@see NodeMutatorCollection::executeWithStartingNode()}
*
* @Flow\Scope("singleton")
*/
class NodeCreationService
{
/**
* @Flow\Inject
* @var NodeUriPathSegmentGenerator
*/
protected $nodeUriPathSegmentGenerator;
/**
* @Flow\Inject
* @var PropertiesProcessor
*/
protected $propertiesProcessor;
/**
* @Flow\Inject
* @var ReferencesProcessor
*/
protected $referencesProcessor;
/**
* Creates mutator {@see NodeMutatorCollection} for the root template and its descending configured child node templates to be applied on a node.
* @throws \InvalidArgumentException
*/
public function createMutatorsForRootTemplate(RootTemplate $template, NodeType $nodeType, NodeTypeManager $nodeTypeManager, Context $subgraph, ProcessingErrors $processingErrors): NodeMutatorCollection
{
$node = TransientNode::forRegular($nodeType, $nodeTypeManager, $subgraph, $template->getProperties());
$validProperties = array_merge(
$this->propertiesProcessor->processAndValidateProperties($node, $processingErrors),
$this->referencesProcessor->processAndValidateReferences($node, $processingErrors)
);
return NodeMutatorCollection::from(
NodeMutator::setProperties($validProperties),
NodeMutator::setDisabled($template->getTags()->isDisabled()),
$this->createMutatorForUriPathSegment($template->getProperties()),
)->merge(
$this->createMutatorsForChildNodeTemplates(
$template->getChildNodes(),
$node,
$processingErrors
)
);
}
private function createMutatorsForChildNodeTemplates(Templates $templates, TransientNode $parentNode, ProcessingErrors $processingErrors): NodeMutatorCollection
{
$nodeMutators = NodeMutatorCollection::createEmpty();
// `hasAutoCreatedChildNode` actually has a bug; it looks up the NodeName parameter against the raw configuration instead of the transliterated NodeName
// https://github.com/neos/neos-ui/issues/3527
$parentNodesAutoCreatedChildNodes = $parentNode->getNodeType()->getAutoCreatedChildNodes();
foreach ($templates as $template) {
if ($template->getName() && isset($parentNodesAutoCreatedChildNodes[$template->getName()->__toString()])) {
/**
* Case 1: Auto created child nodes
*/
if ($template->getType() !== null) {
$processingErrors->add(
ProcessingError::fromException(new \RuntimeException(sprintf('Template cant mutate type of auto created child nodes. Got: "%s"', $template->getType()->getValue()), 1685999829307))
);
// we continue processing the node
}
$node = $parentNode->forTetheredChildNode($template->getName(), $template->getProperties());
$validProperties = array_merge(
$this->propertiesProcessor->processAndValidateProperties($node, $processingErrors),
$this->referencesProcessor->processAndValidateReferences($node, $processingErrors)
);
$nodeMutators = $nodeMutators->append(
NodeMutator::isolated(
NodeMutatorCollection::from(
NodeMutator::selectChildNode($template->getName()),
NodeMutator::setProperties($validProperties)
)->merge($this->createMutatorsForChildNodeTemplates(
$template->getChildNodes(),
$node,
$processingErrors
))
)
);
continue;
}
/**
* Case 2: Regular to be created nodes (non auto-created nodes)
*/
if ($template->getType() === null) {
$processingErrors->add(
ProcessingError::fromException(new \RuntimeException('Template requires type to be set for non auto created child nodes.', 1685999829307))
);
continue;
}
if (!$parentNode->getNodeTypeManager()->hasNodeType($template->getType()->getValue())) {
$processingErrors->add(
ProcessingError::fromException(new \RuntimeException(sprintf('Template requires type to be a valid NodeType. Got: "%s".', $template->getType()->getValue()), 1685999795564))
);
continue;
}
$nodeType = $parentNode->getNodeTypeManager()->getNodeType($template->getType()->getValue());
if ($nodeType->isAbstract()) {
$processingErrors->add(
ProcessingError::fromException(new \RuntimeException(sprintf('Template requires type to be a non abstract NodeType. Got: "%s".', $template->getType()->getValue()), 1686417628976))
);
continue;
}
try {
$parentNode->requireConstraintsImposedByAncestorsToBeMet($nodeType);
} catch (NodeConstraintException $nodeConstraintException) {
$processingErrors->add(
ProcessingError::fromException($nodeConstraintException)
);
continue;
}
$node = $parentNode->forRegularChildNode($nodeType, $template->getProperties());
$validProperties = array_merge(
$this->propertiesProcessor->processAndValidateProperties($node, $processingErrors),
$this->referencesProcessor->processAndValidateReferences($node, $processingErrors)
);
$nodeMutators = $nodeMutators->append(
NodeMutator::isolated(
NodeMutatorCollection::from(
NodeMutator::createAndSelectNode($template->getType(), $template->getName()),
NodeMutator::setProperties($validProperties),
NodeMutator::setDisabled($template->getTags()->isDisabled()),
$this->createMutatorForUriPathSegment($template->getProperties())
)->merge($this->createMutatorsForChildNodeTemplates(
$template->getChildNodes(),
$node,
$processingErrors
))
)
);
}
return $nodeMutators;
}
/**
* All document node types get a uri path segment; if it is not explicitly set in the properties,
* it should be built based on the title property
*/
private function createMutatorForUriPathSegment(array $properties): NodeMutator
{
return NodeMutator::unsafeFromClosure(function (NodeInterface $nodePointer) use ($properties) {
if (!$nodePointer->getNodeType()->isOfType('Neos.Neos:Document')) {
return null;
}
if (isset($properties['uriPathSegment'])) {
return null;
}
$nodePointer->setProperty('uriPathSegment', $this->nodeUriPathSegmentGenerator->generateUriPathSegment($nodePointer, $properties['title'] ?? null));
return null;
});
}
}