-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathNodeCreationService.php
More file actions
322 lines (287 loc) · 13.2 KB
/
NodeCreationService.php
File metadata and controls
322 lines (287 loc) · 13.2 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
namespace Flowpack\NodeTemplates\Domain\NodeCreation;
use Behat\Transliterator\Transliterator;
use Flowpack\NodeTemplates\Domain\ErrorHandling\ProcessingError;
use Flowpack\NodeTemplates\Domain\ErrorHandling\ProcessingErrors;
use Flowpack\NodeTemplates\Domain\Template\RootTemplate;
use Flowpack\NodeTemplates\Domain\Template\Template;
use Flowpack\NodeTemplates\Domain\Template\Templates;
use Neos\ContentRepository\Core\Dimension\ContentDimensionId;
use Neos\ContentRepository\Core\DimensionSpace\DimensionSpacePoint;
use Neos\ContentRepository\Core\DimensionSpace\OriginDimensionSpacePoint;
use Neos\ContentRepository\Core\Feature\NodeCreation\Command\CreateNodeAggregateWithNode;
use Neos\ContentRepository\Core\Feature\NodeDuplication\Command\CopyNodesRecursively;
use Neos\ContentRepository\Core\Feature\NodeModification\Command\SetNodeProperties;
use Neos\ContentRepository\Core\Feature\NodeModification\Dto\PropertyValuesToWrite;
use Neos\ContentRepository\Core\Feature\NodeReferencing\Command\SetNodeReferences;
use Neos\ContentRepository\Core\Feature\NodeReferencing\Dto\NodeReferencesToWrite;
use Neos\ContentRepository\Core\NodeType\NodeType;
use Neos\ContentRepository\Core\NodeType\NodeTypeManager;
use Neos\ContentRepository\Core\Projection\ContentGraph\ContentSubgraphInterface;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateIds;
use Neos\ContentRepository\Core\SharedModel\Node\NodeName;
use Neos\ContentRepository\Core\SharedModel\Node\ReferenceName;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\I18n\Exception\InvalidLocaleIdentifierException;
use Neos\Flow\I18n\Locale;
use Neos\Neos\Service\TransliterationService;
use Neos\Neos\Ui\NodeCreationHandler\NodeCreationCommands;
/**
* Declares the steps how to create a node subtree starting from the root template {@see RootTemplate}
*
* The commands can then be handled by the content repository to create the node structure
*
* @Flow\Scope("singleton")
*/
class NodeCreationService
{
/**
* @Flow\Inject
* @var TransliterationService
*/
protected $transliterationService;
/**
* @Flow\Inject
* @var PropertiesProcessor
*/
protected $propertiesProcessor;
/**
* @Flow\Inject
* @var ReferencesProcessor
*/
protected $referencesProcessor;
/**
* Creates commands {@see NodeCreationCommands} for the root template and its descending configured child node templates.
* @throws \InvalidArgumentException
*/
public function apply(RootTemplate $template, NodeCreationCommands $commands, NodeTypeManager $nodeTypeManager, ContentSubgraphInterface $subgraph, ProcessingErrors $processingErrors): NodeCreationCommands
{
$nodeType = $nodeTypeManager->getNodeType($commands->first->nodeTypeName);
$node = TransientNode::forRegular(
$commands->first->nodeAggregateId,
$commands->first->contentStreamId,
$commands->first->originDimensionSpacePoint,
$nodeType,
$commands->first->tetheredDescendantNodeAggregateIds,
$nodeTypeManager,
$subgraph,
$template->getProperties()
);
$initialProperties = $commands->first->initialPropertyValues;
$initialProperties = $initialProperties->merge(
PropertyValuesToWrite::fromArray(
$this->propertiesProcessor->processAndValidateProperties($node, $processingErrors)
)
);
$initialProperties = $this->ensureNodeHasUriPathSegment(
$nodeType,
$commands->first->nodeName,
$commands->first->originDimensionSpacePoint->toDimensionSpacePoint(),
$initialProperties
);
return $this->applyTemplateRecursively(
$template->getChildNodes(),
$node,
$commands->withInitialPropertyValues($initialProperties)->withAdditionalCommands(
...$this->createReferencesCommands(
$commands->first->contentStreamId,
$commands->first->nodeAggregateId,
$commands->first->originDimensionSpacePoint,
$this->referencesProcessor->processAndValidateReferences($node, $processingErrors)
)
),
$processingErrors
);
}
private function copyTo(Template $template, TransientNode $node)
{
if (empty($template->getCopyFrom())) {
return;
}
$sources = is_array($template->getCopyFrom()) ? $template->getCopyFrom() : [$template->getCopyFrom()];
foreach ($sources as $sourceNode) {
yield CopyNodesRecursively::createFromSubgraphAndStartNode(
$node->subgraph,
$sourceNode,
$node->originDimensionSpacePoint,
$node->nodeAggregateId,
null,
null
);
}
}
private function applyTemplateRecursively(Templates $templates, TransientNode $parentNode, NodeCreationCommands $commands, ProcessingErrors $processingErrors): NodeCreationCommands
{
foreach ($templates as $template) {
if ($template->getName() && $parentNode->nodeType->hasTetheredNode($template->getName())) {
/**
* 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()->value), 1685999829307))
);
// we continue processing the node
}
$node = $parentNode->forTetheredChildNode(
$template->getName(),
$template->getProperties()
);
$commands = $commands->withAdditionalCommands(
SetNodeProperties::create(
$parentNode->contentStreamId,
$node->nodeAggregateId,
$parentNode->originDimensionSpacePoint,
PropertyValuesToWrite::fromArray(
$this->propertiesProcessor->processAndValidateProperties($node, $processingErrors)
)
),
...$this->createReferencesCommands(
$parentNode->contentStreamId,
$node->nodeAggregateId,
$parentNode->originDimensionSpacePoint,
$this->referencesProcessor->processAndValidateReferences($node, $processingErrors)
),
...iterator_to_array($this->copyTo($template, $node))
);
$commands = $this->applyTemplateRecursively(
$template->getChildNodes(),
$node,
$commands,
$processingErrors
);
continue;
}
/**
* Case 2: Regular to be created nodes (non auto-created nodes)
*/
if ($template->getType() === null) {
$processingErrors->add(
ProcessingError::fromException(new \RuntimeException(sprintf('Template requires type to be set for non auto created child nodes.'), 1685999829307))
);
continue;
}
if (!$parentNode->nodeTypeManager->hasNodeType($template->getType())) {
$processingErrors->add(
ProcessingError::fromException(new \RuntimeException(sprintf('Template requires type to be a valid NodeType. Got: "%s".', $template->getType()->value), 1685999795564))
);
continue;
}
$nodeType = $parentNode->nodeTypeManager->getNodeType($template->getType());
if ($nodeType->isAbstract()) {
$processingErrors->add(
ProcessingError::fromException(new \RuntimeException(sprintf('Template requires type to be a non abstract NodeType. Got: "%s".', $template->getType()->value), 1686417628976))
);
continue;
}
try {
$parentNode->requireConstraintsImposedByAncestorsToBeMet($nodeType);
} catch (NodeConstraintException $nodeConstraintException) {
$processingErrors->add(
ProcessingError::fromException($nodeConstraintException)
);
continue;
}
$node = $parentNode->forRegularChildNode(NodeAggregateId::create(), $nodeType, $template->getProperties());
$nodeName = $template->getName() ?? NodeName::fromString(uniqid('node-', false));
$initialProperties = PropertyValuesToWrite::fromArray(
$this->propertiesProcessor->processAndValidateProperties($node, $processingErrors)
);
$initialProperties = $this->ensureNodeHasUriPathSegment(
$nodeType,
$nodeName,
$parentNode->originDimensionSpacePoint->toDimensionSpacePoint(),
$initialProperties
);
$commands = $commands->withAdditionalCommands(
CreateNodeAggregateWithNode::create(
$parentNode->contentStreamId,
$node->nodeAggregateId,
$template->getType(),
$parentNode->originDimensionSpacePoint,
$parentNode->nodeAggregateId,
nodeName: $nodeName,
initialPropertyValues: $initialProperties
)->withTetheredDescendantNodeAggregateIds($node->tetheredNodeAggregateIds),
...$this->createReferencesCommands(
$parentNode->contentStreamId,
$node->nodeAggregateId,
$parentNode->originDimensionSpacePoint,
$this->referencesProcessor->processAndValidateReferences($node, $processingErrors)
),
// todo ...iterator_to_array($this->copyTo($template, $node))
);
$commands = $this->applyTemplateRecursively(
$template->getChildNodes(),
$node,
$commands,
$processingErrors
);
}
return $commands;
}
/**
* @param array<string, NodeAggregateIds> $references
* @return list<SetNodeReferences>
*/
private function createReferencesCommands(ContentStreamId $contentStreamId, NodeAggregateId $nodeAggregateId, OriginDimensionSpacePoint $originDimensionSpacePoint, array $references): array
{
$commands = [];
foreach ($references as $name => $nodeAggregateIds) {
$commands[] = SetNodeReferences::create(
$contentStreamId,
$nodeAggregateId,
$originDimensionSpacePoint,
ReferenceName::fromString($name),
NodeReferencesToWrite::fromNodeAggregateIds($nodeAggregateIds)
);
}
return $commands;
}
/**
* 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 ensureNodeHasUriPathSegment(
NodeType $nodeType,
?NodeName $nodeName,
DimensionSpacePoint $dimensionSpacePoint,
PropertyValuesToWrite $propertiesToWrite
): PropertyValuesToWrite {
if (!$nodeType->isOfType('Neos.Neos:Document')) {
return $propertiesToWrite;
}
if (isset($propertiesToWrite->values['uriPathSegment'])) {
return $propertiesToWrite;
}
return $propertiesToWrite->withValue(
'uriPathSegment',
$this->generateUriPathSegment(
$dimensionSpacePoint,
$propertiesToWrite->values['title'] ?? $nodeName?->value ?? uniqid('', true)
)
);
}
/**
* Copied from https://github.com/neos/neos-ui/blob/6929f73ffc74b1c7b63fbf80b5c2b3152e443534/Classes/NodeCreationHandler/DocumentTitleNodeCreationHandler.php#L80
*
* The {@see \Neos\Neos\Utility\NodeUriPathSegmentGenerator::generateUriPathSegment()} only works with whole Nodes.
*
* Duplicated code might be cleaned up via https://github.com/neos/neos-development-collection/pull/4324
*/
private function generateUriPathSegment(DimensionSpacePoint $dimensionSpacePoint, string $text): string
{
$languageDimensionValue = $dimensionSpacePoint->getCoordinate(new ContentDimensionId('language'));
if ($languageDimensionValue !== null) {
try {
$language = (new Locale($languageDimensionValue))->getLanguage();
} catch (InvalidLocaleIdentifierException $e) {
// we don't need to do anything here; we'll just transliterate the text.
}
}
$transliterated = $this->transliterationService->transliterate($text, $language ?? null);
return Transliterator::urlize($transliterated);
}
}