/
NodeType.php
709 lines (630 loc) · 23.4 KB
/
NodeType.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
<?php
namespace Neos\ContentRepository\Domain\Model;
/*
* 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.
*/
use Neos\ContentRepository\Domain\NodeAggregate\NodeName;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Neos\Utility\ObjectAccess;
use Neos\Utility\Arrays;
use Neos\Utility\PositionalArraySorter;
use Neos\ContentRepository\Domain\Service\NodeTypeManager;
use Neos\ContentRepository\Exception\InvalidNodeTypePostprocessorException;
use Neos\ContentRepository\NodeTypePostprocessor\NodeTypePostprocessorInterface;
use Neos\ContentRepository\Utility;
/**
* A Node Type
*
* Although methods contained in this class belong to the public API, you should
* not need to deal with creating or managing node types manually. New node types
* should be defined in a NodeTypes.yaml file.
*
* @api
*/
class NodeType
{
/**
* Name of this node type. Example: "ContentRepository:Folder"
*
* @var string
*/
protected $name;
/**
* Configuration for this node type, can be an arbitrarily nested array. Does not include inherited configuration.
*
* @var array
*/
protected $localConfiguration;
/**
* Full configuration for this node type, can be an arbitrarily nested array. Includes any inherited configuration.
*
* @var array
*/
protected $fullConfiguration;
/**
* Is this node type marked abstract
*
* @var boolean
*/
protected $abstract = false;
/**
* Is this node type marked final
*
* @var boolean
*/
protected $final = false;
/**
* node types this node type directly inherits from
*
* @var array<\Neos\ContentRepository\Domain\Model\NodeType>
*/
protected $declaredSuperTypes;
/**
* @Flow\Inject
* @var ObjectManagerInterface
*/
protected $objectManager;
/**
* @Flow\Inject
* @var NodeTypeManager
*/
protected $nodeTypeManager;
/**
* @var NodeLabelGeneratorInterface
*/
protected $nodeLabelGenerator;
/**
* Whether or not this node type has been initialized (e.g. if it has been postprocessed)
*
* @var boolean
*/
protected $initialized = false;
/**
* Constructs this node type
*
* @param string $name Name of the node type
* @param array $declaredSuperTypes Parent types of this node type
* @param array $configuration the configuration for this node type which is defined in the schema
* @throws \InvalidArgumentException
*/
public function __construct($name, array $declaredSuperTypes, array $configuration)
{
$this->name = $name;
foreach ($declaredSuperTypes as $type) {
if ($type !== null && !$type instanceof NodeType) {
throw new \InvalidArgumentException('$declaredSuperTypes must be an array of NodeType objects', 1291300950);
}
}
$this->declaredSuperTypes = $declaredSuperTypes;
if (isset($configuration['abstract']) && $configuration['abstract'] === true) {
$this->abstract = true;
unset($configuration['abstract']);
}
if (isset($configuration['final']) && $configuration['final'] === true) {
$this->final = true;
unset($configuration['final']);
}
$this->localConfiguration = $configuration;
}
/**
* Initializes this node type
*
* @return void
* @throws InvalidNodeTypePostprocessorException
* @throws \Exception
*/
protected function initialize()
{
if ($this->initialized === true) {
return;
}
$this->initialized = true;
$this->setFullConfiguration($this->applyPostprocessing($this->buildFullConfiguration()));
}
/**
* Builds the full configuration by merging configuration from the supertypes into the local configuration.
*
* @return array
*/
protected function buildFullConfiguration() : array
{
$mergedConfiguration = [];
$applicableSuperTypes = static::getFlattenedSuperTypes($this);
foreach ($applicableSuperTypes as $key => $superType) {
$mergedConfiguration = Arrays::arrayMergeRecursiveOverrule($mergedConfiguration, $superType->getLocalConfiguration());
}
$this->fullConfiguration = Arrays::arrayMergeRecursiveOverrule($mergedConfiguration, $this->localConfiguration);
if (isset($this->fullConfiguration['childNodes']) && is_array($this->fullConfiguration['childNodes']) && $this->fullConfiguration['childNodes'] !== []) {
$sorter = new PositionalArraySorter($this->fullConfiguration['childNodes']);
$this->fullConfiguration['childNodes'] = $sorter->toArray();
}
return $this->fullConfiguration;
}
/**
* Returns a flat list of super types to inherit from.
*
* @param NodeType $nodeType
*
* @return array
*/
protected static function getFlattenedSuperTypes(NodeType $nodeType) : array
{
$flattenedSuperTypes = [];
foreach ($nodeType->declaredSuperTypes as $superTypeName => $superType) {
if ($superType !== null) {
$flattenedSuperTypes += static::getFlattenedSuperTypes($superType);
$flattenedSuperTypes[$superTypeName] = $superType;
}
}
foreach ($nodeType->declaredSuperTypes as $superTypeName => $superType) {
if ($superType === null) {
unset($flattenedSuperTypes[$superTypeName]);
}
}
return $flattenedSuperTypes;
}
/**
* Iterates through configured postprocessors and invokes them
*
* @param array $fullConfiguration
* @return array
* @throws InvalidNodeTypePostprocessorException
*/
protected function applyPostprocessing($fullConfiguration): array
{
if (!isset($fullConfiguration['postprocessors'])) {
return $fullConfiguration;
}
$sortedPostProcessors = (new PositionalArraySorter($this->fullConfiguration['postprocessors']))->toArray();
foreach ($sortedPostProcessors as $postprocessorConfiguration) {
$postprocessor = new $postprocessorConfiguration['postprocessor']();
if (!$postprocessor instanceof NodeTypePostprocessorInterface) {
throw new InvalidNodeTypePostprocessorException(sprintf('Expected NodeTypePostprocessorInterface but got "%s"', get_class($postprocessor)), 1364759955);
}
$postprocessorOptions = [];
if (isset($postprocessorConfiguration['postprocessorOptions'])) {
$postprocessorOptions = $postprocessorConfiguration['postprocessorOptions'];
}
// TODO: Needs to be made more transparent by returning configuration
$postprocessor->process($this, $fullConfiguration, $postprocessorOptions);
}
return $fullConfiguration;
}
/**
* Returns the name of this node type
*
* @return string
* @api
*/
public function getName()
{
return $this->name;
}
/**
* Return boolean true if marked abstract
*
* @return boolean
*/
public function isAbstract()
{
return $this->abstract;
}
/**
* Return boolean true if marked final
*
* @return boolean
*/
public function isFinal()
{
return $this->final;
}
/**
* Returns the direct, explicitly declared super types
* of this node type.
*
* Note: NULL values are skipped since they are used only internally.
*
* @return array<NodeType>
* @api
*/
public function getDeclaredSuperTypes()
{
return array_filter($this->declaredSuperTypes, function ($value) {
return $value !== null;
});
}
/**
* Returns whether this node type (or any parent type) is an *aggregate*.
*
* The most prominent *aggregate* is a Document and everything which inherits from it, like a Page.
*
* If a node type is marked as aggregate, it means that:
*
* - the node type can "live on its own", i.e. can be part of an external URL
* - when moving this node, all node variants are also moved (across all dimensions)
* - Recursive copying only happens *inside* this aggregate, and stops at nested aggregates.
*
* @return boolean true if the node type is an aggregate
* @api
*/
public function isAggregate()
{
return $this->getConfiguration('aggregate') === true;
}
/**
* If this node type or any of the direct or indirect super types
* has the given name.
*
* @param string $nodeType
* @return boolean true if this node type is of the given kind, otherwise false
* @api
*/
public function isOfType($nodeType)
{
if ($nodeType === $this->name) {
return true;
}
if (array_key_exists($nodeType, $this->declaredSuperTypes) && $this->declaredSuperTypes[$nodeType] === null) {
return false;
}
foreach ($this->declaredSuperTypes as $superType) {
if ($superType !== null && $superType->isOfType($nodeType) === true) {
return true;
}
}
return false;
}
/**
* Get the local configuration of the node type. Should only be used internally.
*
* Note: post processing is not applied to this.
*
* @return array
*/
public function getLocalConfiguration()
{
return $this->localConfiguration;
}
/**
* Get the full configuration of the node type. Should only be used internally.
*
* Instead, use the hasConfiguration()/getConfiguration() methods to check/retrieve single configuration values.
*
* @return array
*/
public function getFullConfiguration()
{
$this->initialize();
return $this->fullConfiguration;
}
/**
* Checks if the configuration of this node type contains a setting for the given $configurationPath
*
* @param string $configurationPath The name of the configuration option to verify
* @return boolean
* @api
*/
public function hasConfiguration($configurationPath)
{
return $this->getConfiguration($configurationPath) !== null;
}
/**
* Returns the configuration option with the specified $configurationPath or NULL if it does not exist
*
* @param string $configurationPath The name of the configuration option to retrieve
* @return mixed
* @api
*/
public function getConfiguration($configurationPath)
{
$this->initialize();
return ObjectAccess::getPropertyPath($this->fullConfiguration, $configurationPath);
}
/**
* Get the human-readable label of this node type
*
* @return string
* @api
*/
public function getLabel()
{
$this->initialize();
return isset($this->fullConfiguration['ui']['label']) ? $this->fullConfiguration['ui']['label'] : '';
}
/**
* Get additional options (if specified)
*
* @return array
* @api
*/
public function getOptions()
{
$this->initialize();
return (isset($this->fullConfiguration['options']) ? $this->fullConfiguration['options'] : []);
}
/**
* Return the node label generator class for the given node
*
* @return NodeLabelGeneratorInterface
*/
public function getNodeLabelGenerator()
{
$this->initialize();
if ($this->nodeLabelGenerator === null) {
if ($this->hasConfiguration('label.generatorClass')) {
$nodeLabelGenerator = $this->objectManager->get($this->getConfiguration('label.generatorClass'));
} elseif ($this->hasConfiguration('label') && is_string($this->getConfiguration('label'))) {
$nodeLabelGenerator = $this->objectManager->get(ExpressionBasedNodeLabelGenerator::class);
$nodeLabelGenerator->setExpression($this->getConfiguration('label'));
} else {
$nodeLabelGenerator = $this->objectManager->get(NodeLabelGeneratorInterface::class);
}
$this->nodeLabelGenerator = $nodeLabelGenerator;
}
return $this->nodeLabelGenerator;
}
/**
* Return the array with the defined properties. The key is the property name,
* the value the property configuration. There are no guarantees on how the
* property configuration looks like.
*
* @return array
* @api
*/
public function getProperties()
{
$this->initialize();
return (isset($this->fullConfiguration['properties']) ? $this->fullConfiguration['properties'] : []);
}
/**
* Returns the configured type of the specified property
*
* @param string $propertyName Name of the property
* @return string
*/
public function getPropertyType($propertyName)
{
$this->initialize();
return $this->fullConfiguration['properties'][$propertyName]['type'] ?? 'string';
}
/**
* Return an array with the defined default values for each property, if any.
*
* The default value is configured for each property under the "default" key.
*
* @return array
* @api
*/
public function getDefaultValuesForProperties()
{
$this->initialize();
if (!isset($this->fullConfiguration['properties'])) {
return [];
}
$defaultValues = [];
foreach ($this->fullConfiguration['properties'] as $propertyName => $propertyConfiguration) {
if (isset($propertyConfiguration['defaultValue'])) {
$type = isset($propertyConfiguration['type']) ? $propertyConfiguration['type'] : '';
switch ($type) {
case 'DateTime':
$defaultValues[$propertyName] = new \DateTime($propertyConfiguration['defaultValue']);
break;
default:
$defaultValues[$propertyName] = $propertyConfiguration['defaultValue'];
}
}
}
return $defaultValues;
}
/**
* Return an array with child nodes which should be automatically created
*
* @return self[] the key of this array is the name of the child, and the value its NodeType.
* @api
*/
public function getAutoCreatedChildNodes()
{
$this->initialize();
if (!isset($this->fullConfiguration['childNodes'])) {
return [];
}
$autoCreatedChildNodes = [];
foreach ($this->fullConfiguration['childNodes'] as $childNodeName => $childNodeConfiguration) {
if (isset($childNodeConfiguration['type'])) {
$autoCreatedChildNodes[Utility::renderValidNodeName($childNodeName)] = $this->nodeTypeManager->getNodeType($childNodeConfiguration['type']);
}
}
return $autoCreatedChildNodes;
}
/**
* @param NodeName $nodeName
* @return bool true if $nodeName is an autocreated child node, false otherwise
*/
public function hasAutoCreatedChildNode(NodeName $nodeName): bool
{
return isset($this->getAutoCreatedChildNodes()[(string)$nodeName]);
}
/**
* @param NodeName $nodeName
* @return NodeType|null
* @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException
*/
public function getTypeOfAutoCreatedChildNode(NodeName $nodeName): ?NodeType
{
return $this->getAutoCreatedChildNodes()[(string)$nodeName] ?? null;
}
/**
* Checks if the given NodeType is acceptable as sub-node with the configured constraints,
* not taking constraints of auto-created nodes into account. Thus, this method only returns
* the correct result if called on NON-AUTO-CREATED nodes!
*
* Otherwise, allowsGrandchildNodeType() needs to be called on the *parent node type*.
*
* @param NodeType $nodeType
* @return boolean true if the $nodeType is allowed as child node, false otherwise.
*/
public function allowsChildNodeType(NodeType $nodeType)
{
$constraints = $this->getConfiguration('constraints.nodeTypes') ?: [];
return $this->isNodeTypeAllowedByConstraints($nodeType, $constraints);
}
/**
* Checks if the given $nodeType is allowed as a childNode of the given $childNodeName
* (which must be auto-created in $this NodeType).
*
* Only allowed to be called if $childNodeName is auto-created.
*
* @param string $childNodeName The name of a configured childNode of this NodeType
* @param NodeType $nodeType The NodeType to check constraints for.
* @return boolean true if the $nodeType is allowed as grandchild node, false otherwise.
* @throws \InvalidArgumentException If the given $childNodeName is not configured to be auto-created in $this.
*/
public function allowsGrandchildNodeType($childNodeName, NodeType $nodeType)
{
$autoCreatedChildNodes = $this->getAutoCreatedChildNodes();
if (!isset($autoCreatedChildNodes[$childNodeName])) {
throw new \InvalidArgumentException('The method "allowsGrandchildNodeType" can only be used on auto-created childNodes, given $childNodeName "' . $childNodeName . '" is not auto-created.', 1403858395);
}
$constraints = $autoCreatedChildNodes[$childNodeName]->getConfiguration('constraints.nodeTypes') ?: [];
$childNodeConfiguration = [];
foreach ($this->getConfiguration('childNodes') as $name => $configuration) {
$childNodeConfiguration[Utility::renderValidNodeName($name)] = $configuration;
}
$childNodeConstraintConfiguration = ObjectAccess::getPropertyPath($childNodeConfiguration, $childNodeName . '.constraints.nodeTypes') ?: [];
$constraints = Arrays::arrayMergeRecursiveOverrule($constraints, $childNodeConstraintConfiguration);
return $this->isNodeTypeAllowedByConstraints($nodeType, $constraints);
}
/**
* Internal method to check whether the passed-in $nodeType is allowed by the $constraints array.
*
* $constraints is an associative array where the key is the Node Type Name. If the value is "true",
* the node type is explicitly allowed. If the value is "false", the node type is explicitly denied.
* If nothing is specified, the fallback "*" is used. If that one is also not specified, we DENY by
* default.
*
* Super types of the given node types are also checked, so if a super type is constrained
* it will also take affect on the inherited node types. The closest constrained super type match is used.
*
* @param NodeType $nodeType
* @param array $constraints
* @return boolean
*/
protected function isNodeTypeAllowedByConstraints(NodeType $nodeType, array $constraints)
{
$directConstraintsResult = $this->isNodeTypeAllowedByDirectConstraints($nodeType, $constraints);
if ($directConstraintsResult !== null) {
return $directConstraintsResult;
}
$inheritanceConstraintsResult = $this->isNodeTypeAllowedByInheritanceConstraints($nodeType, $constraints);
if ($inheritanceConstraintsResult !== null) {
return $inheritanceConstraintsResult;
}
if (isset($constraints['*'])) {
return (boolean)$constraints['*'];
}
return false;
}
/**
* @param NodeType $nodeType
* @param array $constraints
* @return boolean true if the passed $nodeType is allowed by the $constraints
*/
protected function isNodeTypeAllowedByDirectConstraints(NodeType $nodeType, array $constraints)
{
if ($constraints === []) {
return true;
}
if (array_key_exists($nodeType->getName(), $constraints) && $constraints[$nodeType->getName()] === true) {
return true;
}
if (array_key_exists($nodeType->getName(), $constraints) && $constraints[$nodeType->getName()] === false) {
return false;
}
return null;
}
/**
* This method loops over the constraints and finds node types that the given node type inherits from. For all
* matched super types, their super types are traversed to find the closest super node with a constraint which
* is used to evaluated if the node type is allowed. It finds the closest results for true and false, and uses
* the distance to choose which one wins (lowest). If no result is found the node type is allowed.
*
* @param NodeType $nodeType
* @param array $constraints
* @return boolean|NULL if no constraint matched
*/
protected function isNodeTypeAllowedByInheritanceConstraints(NodeType $nodeType, array $constraints)
{
$constraintDistanceForTrue = null;
$constraintDistanceForFalse = null;
foreach ($constraints as $superType => $constraint) {
if ($nodeType->isOfType($superType)) {
$distance = $this->traverseSuperTypes($nodeType, $superType, 0);
if ($constraint === true && ($constraintDistanceForTrue === null || $constraintDistanceForTrue > $distance)) {
$constraintDistanceForTrue = $distance;
}
if ($constraint === false && ($constraintDistanceForFalse === null || $constraintDistanceForFalse > $distance)) {
$constraintDistanceForFalse = $distance;
}
}
}
if ($constraintDistanceForTrue !== null && $constraintDistanceForFalse !== null) {
return $constraintDistanceForTrue < $constraintDistanceForFalse ? true : false;
}
if ($constraintDistanceForFalse !== null) {
return false;
}
if ($constraintDistanceForTrue !== null) {
return true;
}
return null;
}
/**
* This method traverses the given node type to find the first super type that matches the constraint node type.
* In case the hierarchy has more than one way of finding a path to the node type it's not taken into account,
* since the first matched is returned. This is accepted on purpose for performance reasons and due to the fact
* that such hierarchies should be avoided.
*
* @param NodeType $currentNodeType
* @param string $constraintNodeTypeName
* @param integer $distance
* @return integer or NULL if no NodeType matched
*/
protected function traverseSuperTypes(NodeType $currentNodeType, $constraintNodeTypeName, $distance)
{
if ($currentNodeType->getName() === $constraintNodeTypeName) {
return $distance;
}
$distance++;
foreach ($currentNodeType->getDeclaredSuperTypes() as $superType) {
$result = $this->traverseSuperTypes($superType, $constraintNodeTypeName, $distance);
if ($result !== null) {
return $result;
}
}
return null;
}
/**
* @param array $fullConfiguration
*/
protected function setFullConfiguration(array $fullConfiguration): void
{
$this->fullConfiguration = $fullConfiguration;
}
/**
* Alias for getName().
*
* @return string
* @api
*/
public function __toString()
{
return $this->getName();
}
}