/
NodeTypeManager.php
397 lines (354 loc) · 14.7 KB
/
NodeTypeManager.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/*
* This file is part of the Neos.ContentRepository 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.
*/
declare(strict_types=1);
namespace Neos\ContentRepository\Core\NodeType;
use Neos\ContentRepository\Core\NodeType\Exception\TetheredNodeNotConfigured;
use Neos\ContentRepository\Core\SharedModel\Exception\NodeConfigurationException;
use Neos\ContentRepository\Core\SharedModel\Exception\NodeTypeIsFinalException;
use Neos\ContentRepository\Core\SharedModel\Exception\NodeTypeNotFoundException;
use Neos\ContentRepository\Core\SharedModel\Node\NodeName;
use Neos\Utility\Arrays;
use Neos\Utility\Exception\PropertyNotAccessibleException;
/**
* Manager for node types
* @api Note: The constructor is not part of the public API
*/
final class NodeTypeManager
{
/**
* Node types, indexed by name
*
* @var array<string,NodeType>
*/
private array $cachedNodeTypes = [];
/**
* Node types, indexed by supertype (also including abstract node types)
*
* @var array<string,array<string,NodeType>>
*/
private array $cachedSubNodeTypes = [];
/**
* @internal
*/
public function __construct(
private readonly \Closure $nodeTypeConfigLoader,
private readonly NodeLabelGeneratorFactoryInterface $nodeLabelGeneratorFactory
) {
}
/**
* Return all registered node types.
*
* @param boolean $includeAbstractNodeTypes Whether to include abstract node types, defaults to true
* @return array<string,NodeType> All node types registered in the system, indexed by node type name
*/
public function getNodeTypes(bool $includeAbstractNodeTypes = true): array
{
if ($this->cachedNodeTypes === []) {
$this->loadNodeTypes();
}
if ($includeAbstractNodeTypes) {
return $this->cachedNodeTypes;
}
return array_filter($this->cachedNodeTypes, function ($nodeType) {
return !$nodeType->isAbstract();
});
}
/**
* Return all non-abstract node types which have a certain $superType, without
* the $superType itself.
*
* @param boolean $includeAbstractNodeTypes Whether to include abstract node types, defaults to true
* @return array<NodeType> Sub node types of the given super type, indexed by node type name
*/
public function getSubNodeTypes(string|NodeTypeName $superTypeName, bool $includeAbstractNodeTypes = true): array
{
if ($superTypeName instanceof NodeTypeName) {
$superTypeName = $superTypeName->value;
}
if ($this->cachedNodeTypes === []) {
$this->loadNodeTypes();
}
if (!isset($this->cachedSubNodeTypes[$superTypeName])) {
$filteredNodeTypes = [];
foreach ($this->cachedNodeTypes as $nodeTypeName => $nodeType) {
if ($nodeType->isOfType($superTypeName) && $nodeTypeName !== $superTypeName) {
$filteredNodeTypes[$nodeTypeName] = $nodeType;
}
}
$this->cachedSubNodeTypes[$superTypeName] = $filteredNodeTypes;
}
if ($includeAbstractNodeTypes === false) {
return array_filter($this->cachedSubNodeTypes[$superTypeName], function (NodeType $nodeType) {
return !$nodeType->isAbstract();
});
}
return $this->cachedSubNodeTypes[$superTypeName];
}
/**
* Returns the specified node type (which could be abstract)
*
* @throws NodeTypeNotFoundException
*/
public function getNodeType(string|NodeTypeName $nodeTypeName): ?NodeType
{
if ($nodeTypeName instanceof NodeTypeName) {
$nodeTypeName = $nodeTypeName->value;
}
if ($this->cachedNodeTypes === []) {
$this->loadNodeTypes();
}
if (isset($this->cachedNodeTypes[$nodeTypeName])) {
return $this->cachedNodeTypes[$nodeTypeName];
}
return null;
}
/**
* Checks if the specified node type exists
*
* @param string|NodeTypeName $nodeTypeName Name of the node type
* @return boolean true if it exists, otherwise false
*/
public function hasNodeType(string|NodeTypeName $nodeTypeName): bool
{
if ($nodeTypeName instanceof NodeTypeName) {
$nodeTypeName = $nodeTypeName->value;
}
if ($this->cachedNodeTypes === []) {
$this->loadNodeTypes();
}
return isset($this->cachedNodeTypes[$nodeTypeName]);
}
/**
* Loads all node types into memory.
*/
private function loadNodeTypes(): void
{
$completeNodeTypeConfiguration = ($this->nodeTypeConfigLoader)();
// the root node type must always exist
$completeNodeTypeConfiguration[NodeTypeName::ROOT_NODE_TYPE_NAME] ??= [];
foreach (array_keys($completeNodeTypeConfiguration) as $nodeTypeName) {
if (!is_string($nodeTypeName)) {
continue;
}
if (!is_array($completeNodeTypeConfiguration[$nodeTypeName])) {
continue;
}
$this->loadNodeType($nodeTypeName, $completeNodeTypeConfiguration);
}
}
/**
* This method can be used by Functional of Behavioral Tests to completely
* override the node types known in the system.
*
* In order to reset the node type override, an empty array can be passed in.
* In this case, the system-node-types are used again.
*
* @internal
* @param array<string,mixed> $completeNodeTypeConfiguration
*/
public function overrideNodeTypes(array $completeNodeTypeConfiguration): void
{
$this->cachedNodeTypes = [];
if ($completeNodeTypeConfiguration === []) {
// as cachedNodeTypes is now empty loadNodeTypes will reload the default nodeTypes
return;
}
// the root node type must always exist
$completeNodeTypeConfiguration[NodeTypeName::ROOT_NODE_TYPE_NAME] ??= [];
foreach (array_keys($completeNodeTypeConfiguration) as $nodeTypeName) {
/** @var string $nodeTypeName */
$this->loadNodeType($nodeTypeName, $completeNodeTypeConfiguration);
}
}
/**
* @param NodeType $nodeType
* @param NodeName $tetheredNodeName
* @return NodeType
* @throws TetheredNodeNotConfigured if the requested tethered node is not configured. Check via {@see NodeType::hasTetheredNode()}.
*/
public function getTypeOfTetheredNode(NodeType $nodeType, NodeName $tetheredNodeName): NodeType
{
$nameOfTetheredNode = $nodeType->getNodeTypeNameOfTetheredNode($tetheredNodeName);
return $this->requireNodeType($nameOfTetheredNode);
}
/**
* Return an array with child nodes which should be automatically created
*
* @return array<string,NodeType> the key of this array is the name of the child, and the value its NodeType.
*/
public function getTetheredNodesConfigurationForNodeType(NodeType $nodeType): array
{
$childNodeConfiguration = $nodeType->getConfiguration('childNodes');
$autoCreatedChildNodes = [];
foreach ($childNodeConfiguration ?? [] as $childNodeName => $configurationForChildNode) {
if (isset($configurationForChildNode['type'])) {
$autoCreatedChildNodes[NodeName::transliterateFromString($childNodeName)->value] = $this->requireNodeType($configurationForChildNode['type']);
}
}
return $autoCreatedChildNodes;
}
/**
* Checks if the given $nodeType is allowed as a childNode of the given $tetheredNodeName
* (which must be tethered in $parentNodeType).
*
* Only allowed to be called if $tetheredNodeName is actually tethered.
*
* @param NodeType $parentNodeType The NodeType to check constraints based on.
* @param NodeName $tetheredNodeName The name of a configured tethered node of this NodeType
* @param NodeType $nodeType The NodeType to check constraints for.
* @return bool true if the $nodeType is allowed as grandchild node, false otherwise.
* @throws \InvalidArgumentException if the requested tethered node is not configured in the parent NodeType. Check via {@see NodeType::hasTetheredNode()}.
*/
public function isNodeTypeAllowedAsChildToTetheredNode(NodeType $parentNodeType, NodeName $tetheredNodeName, NodeType $nodeType): bool
{
try {
$typeOfTetheredNode = $this->getTypeOfTetheredNode($parentNodeType, $tetheredNodeName);
} catch (TetheredNodeNotConfigured $exception) {
throw new \InvalidArgumentException(
sprintf(
'Cannot determine if grandchild is allowed in %s. Because the given child node name "%s" is not auto-created.',
$parentNodeType->name->value,
$tetheredNodeName->value
),
1403858395,
$exception
);
}
// Constraints configured on the NodeType for the child node
$constraints = $typeOfTetheredNode->getConfiguration('constraints.nodeTypes') ?: [];
// Constraints configured at the childNode configuration of the parent.
try {
$childNodeConstraintConfiguration = $parentNodeType->getConfiguration('childNodes.' . $tetheredNodeName->value . '.constraints.nodeTypes') ?? [];
} catch (PropertyNotAccessibleException $exception) {
// We ignore this because the configuration might just not have any constraints, if the childNode was not configured the exception above would have been thrown.
$childNodeConstraintConfiguration = [];
}
$constraints = Arrays::arrayMergeRecursiveOverrule($constraints, $childNodeConstraintConfiguration);
return ConstraintCheck::create($constraints)->isNodeTypeAllowed($nodeType);
}
/**
* Load one node type, if it is not loaded yet.
*
* @param array<string,mixed> $completeNodeTypeConfiguration the full node type configuration for all node types
* @throws NodeConfigurationException
* @throws NodeTypeIsFinalException
* @throws NodeTypeNotFoundException
*/
private function loadNodeType(string $nodeTypeName, array &$completeNodeTypeConfiguration): NodeType
{
if (isset($this->cachedNodeTypes[$nodeTypeName])) {
return $this->cachedNodeTypes[$nodeTypeName];
}
if (!isset($completeNodeTypeConfiguration[$nodeTypeName])) {
throw new NodeTypeNotFoundException('Node type "' . $nodeTypeName . '" does not exist', 1316451800);
}
$nodeTypeConfiguration = $completeNodeTypeConfiguration[$nodeTypeName];
try {
$superTypes = isset($nodeTypeConfiguration['superTypes'])
? $this->evaluateSuperTypesConfiguration(
$nodeTypeConfiguration['superTypes'],
$completeNodeTypeConfiguration
)
: [];
} catch (NodeConfigurationException $exception) {
throw new NodeConfigurationException(
'Node type "' . $nodeTypeName . '" sets super type with a non-string key to NULL.',
1416578395
);
} catch (NodeTypeIsFinalException $exception) {
throw new NodeTypeIsFinalException(
'Node type "' . $nodeTypeName . '" has a super type "' . $exception->getMessage() . '" which is final.',
1316452423
);
}
// Remove unset properties
$properties = [];
if (isset($nodeTypeConfiguration['properties']) && is_array($nodeTypeConfiguration['properties'])) {
$properties = $nodeTypeConfiguration['properties'];
}
$nodeTypeConfiguration['properties'] = array_filter($properties, function ($propertyConfiguration) {
return $propertyConfiguration !== null;
});
if ($nodeTypeConfiguration['properties'] === []) {
unset($nodeTypeConfiguration['properties']);
}
$nodeType = new NodeType(
NodeTypeName::fromString($nodeTypeName),
$superTypes,
$nodeTypeConfiguration,
$this->nodeLabelGeneratorFactory
);
$this->cachedNodeTypes[$nodeTypeName] = $nodeType;
return $nodeType;
}
/**
* Evaluates the given superTypes configuation and returns the array of effective superTypes.
*
* @param array<string,mixed> $superTypesConfiguration
* @param array<string,mixed> $completeNodeTypeConfiguration
* @return array<string,NodeType|null>
*/
private function evaluateSuperTypesConfiguration(
array $superTypesConfiguration,
array $completeNodeTypeConfiguration
): array {
$superTypes = [];
foreach ($superTypesConfiguration as $superTypeName => $enabled) {
if (!is_string($superTypeName)) {
throw new NodeConfigurationException(
'superTypes must be a dictionary; the array format was deprecated since Neos 2.0',
1651821391
);
}
$superTypes[$superTypeName] = $this->evaluateSuperTypeConfiguration(
$superTypeName,
$enabled,
$completeNodeTypeConfiguration
);
}
return $superTypes;
}
/**
* Evaluates a single superType configuration and returns the NodeType if enabled.
*
* @param array<string,mixed> $completeNodeTypeConfiguration
* @throws NodeConfigurationException
* @throws NodeTypeIsFinalException
*/
private function evaluateSuperTypeConfiguration(
string $superTypeName,
?bool $enabled,
array &$completeNodeTypeConfiguration
): ?NodeType {
// Skip unset node types
if ($enabled === false || $enabled === null) {
return null;
}
$superType = $this->loadNodeType($superTypeName, $completeNodeTypeConfiguration);
if ($superType->isFinal() === true) {
throw new NodeTypeIsFinalException($superType->name->value, 1444944148);
}
return $superType;
}
/**
* @internal helper to throw if the NodeType doesn't exit
*/
public function requireNodeType(string|NodeTypeName $nodeTypeName): NodeType
{
return $this->getNodeType($nodeTypeName) ?? throw new NodeTypeNotFoundException(
sprintf(
'The node type "%s" is not available',
$nodeTypeName instanceof NodeTypeName ? $nodeTypeName->value : $nodeTypeName
),
1316598370
);
}
}