-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Node.php
2333 lines (2088 loc) · 83.2 KB
/
Node.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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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\DimensionSpace\DimensionSpace\DimensionSpacePoint;
use Neos\ContentRepository\Domain\Projection\Content\TraversableNodeInterface;
use Neos\ContentRepository\Domain\Projection\Content\TraversableNodes;
use Neos\ContentRepository\Domain\ContentStream\ContentStreamIdentifier;
use Neos\ContentRepository\Domain\NodeAggregate\NodeAggregateIdentifier;
use Neos\ContentRepository\Domain\NodeAggregate\NodeName;
use Neos\ContentRepository\Domain\ContentSubgraph\NodePath;
use Neos\ContentRepository\Domain\NodeType\NodeTypeConstraints;
use Neos\ContentRepository\Domain\NodeType\NodeTypeName;
use Neos\ContentRepository\Domain\Projection\Content\PropertyCollectionInterface;
use Neos\ContentRepository\Domain\Service\NodeMoveIntegrityCheckService;
use Neos\ContentRepository\Exception\NodeConfigurationException;
use Neos\ContentRepository\Exception\NodeTypeNotFoundException;
use Neos\ContentRepository\Exception\NodeMethodIsUnsupported;
use Neos\EventSourcedContentRepository\Domain\Context\NodeAggregate\OriginDimensionSpacePoint;
use Neos\EventSourcedContentRepository\Domain\ValueObject\PropertyName;
use Neos\Flow\Annotations as Flow;
use Neos\Cache\CacheAwareInterface;
use Neos\Flow\Property\PropertyMapper;
use Neos\Utility\ObjectAccess;
use Neos\ContentRepository\Domain\Factory\NodeFactory;
use Neos\ContentRepository\Domain\Repository\NodeDataRepository;
use Neos\ContentRepository\Domain\Service\Context;
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface;
use Neos\ContentRepository\Domain\Service\NodeServiceInterface;
use Neos\ContentRepository\Domain\Utility\NodePaths;
use Neos\ContentRepository\Exception\NodeConstraintException;
use Neos\ContentRepository\Exception\NodeException;
use Neos\ContentRepository\Exception\NodeExistsException;
use Neos\ContentRepository\Utility;
/**
* This is the main API for storing and retrieving content in the system.
*
* @Flow\Scope("prototype")
* @api
*/
class Node implements NodeInterface, CacheAwareInterface, TraversableNodeInterface
{
/**
* The NodeData entity this version is for.
*
* @var NodeData
*/
protected $nodeData;
/**
* @var Context
*/
protected $context;
/**
* Defines if the NodeData represented by this Node is already
* in the same context or if it is currently just "shining through".
*
* @var boolean
*/
protected $nodeDataIsMatchingContext = null;
/**
* @Flow\Inject
* @var NodeDataRepository
*/
protected $nodeDataRepository;
/**
* @Flow\Inject
* @var NodeFactory
*/
protected $nodeFactory;
/**
* @Flow\Inject
* @var PropertyMapper
*/
protected $propertyMapper;
/**
* @Flow\Inject
* @var ContextFactoryInterface
*/
protected $contextFactory;
/**
* @Flow\Inject
* @var NodeServiceInterface
*/
protected $nodeService;
/**
* @Flow\Inject
* @var NodeMoveIntegrityCheckService
*/
protected $nodeMoveIntegrityCheckService;
/**
* @param NodeData $nodeData
* @param Context $context
* @Flow\Autowiring(false)
*/
public function __construct(NodeData $nodeData, Context $context)
{
$this->nodeData = $nodeData;
$this->context = $context;
}
/**
* Returns the absolute path of this node with additional context information (such as the workspace name).
*
* Example: /sites/mysitecom/homepage/about@user-admin
*
* NOTE: This method will probably be removed at some point. Code should never rely on the exact format of the context path
* since that might change in the future.
*
* @return string Node path with context information
*/
public function getContextPath()
{
return NodePaths::generateContextPath($this->getPath(), $this->context->getWorkspaceName(), $this->context->getDimensions());
}
/**
* Set the name of the node to $newName, keeping its position as it is.
*
* @param string $newName
* @return void
* @throws NodeException if you try to set the name of the root node.
* @throws \InvalidArgumentException if $newName is invalid
* @throws NodeTypeNotFoundException
* @api
*/
public function setName($newName): void
{
if ($this->getName() === $newName) {
return;
}
if (!is_string($newName) || preg_match(NodeInterface::MATCH_PATTERN_NAME, $newName) !== 1) {
throw new \InvalidArgumentException('Invalid node name "' . $newName . '" (a node name must only contain lowercase characters, numbers and the "-" sign).', 1364290748);
}
if ($this->isRoot()) {
throw new NodeException('The root node cannot be renamed.', 1346778388);
}
$this->setPath(NodePaths::addNodePathSegment($this->getParentPath(), $newName));
$this->nodeDataRepository->persistEntities();
$this->context->getFirstLevelNodeCache()->flush();
$this->emitNodeUpdated($this);
}
/**
* Sets the absolute path of this node.
*
* This method is only for internal use by the content repository or node methods. Changing
* the path of a node manually may lead to unexpected behavior.
*
* To achieve a correct behavior when changing the path (moving the node) in a workspace, a shadow node data that will
* hide the node data in the base workspace will be created. Thus queries do not need to worry about moved nodes.
* Through a movedTo reference the shadow node data will be removed when publishing the moved node.
*
* @param string $path
* @param boolean $checkForExistence Checks for existence at target path, internally used for recursions and shadow nodes.
* @return void
* @throws NodeException
* @throws NodeTypeNotFoundException
*/
protected function setPath(string $path, bool $checkForExistence = true): void
{
$originalPath = $this->nodeData->getPath();
if ($originalPath === $path) {
return;
}
$pathAvailable = $checkForExistence ? $this->isNodePathAvailable($path) : true;
if (!$pathAvailable) {
throw new NodeException(sprintf('Can not rename the node "%s" as a node already exists on path "%s"', $this->getPath(), $path), 1414436551);
}
if ($this->getNodeType()->isAggregate()) {
$changedNodePathsCollection = $this->setPathInternalForAggregate($path, !$checkForExistence);
} else {
$changedNodePathsCollection = $this->setPathInternal($path, !$checkForExistence);
}
$this->nodeDataRepository->persistEntities();
array_walk($changedNodePathsCollection, function ($changedNodePathInformation) {
call_user_func_array([
$this,
'emitNodePathChanged'
], $changedNodePathInformation);
});
}
/**
* Checks if the given node path is available for this node, so either no node with this path exists or an existing node has the same identifier.
*
* @param string $path
* @return boolean
*/
protected function isNodePathAvailable(string $path): bool
{
$existingNodeDataArray = $this->nodeDataRepository->findByPathWithoutReduce($path, $this->context->getWorkspace());
$nonMatchingNodeData = array_filter($existingNodeDataArray, function (NodeData $nodeData) {
return ($nodeData->getIdentifier() !== $this->getIdentifier());
});
return ($nonMatchingNodeData === []);
}
/**
* Moves a node and sub nodes to the new path.
* ONLY CALLED FOR Non-Aggregate Nodes, (= non-Document Nodes) -> CONTENT Nodes.
*
* For Document Nodes, {@see setPathInternalForAggregate} is called (in {@see setPath}
*
* @param string $destinationPath the new node path
* @param boolean $recursiveCall is this a recursive call
* @return array NodeVariants and old and new paths
* @throws NodeException
* @throws NodeTypeNotFoundException
*/
protected function setPathInternal(string $destinationPath, bool $recursiveCall): array
{
$originalPath = $this->nodeData->getPath();
/** @var Node $childNode */
foreach ($this->getChildNodes() as $childNode) {
$childNode->setPath(NodePaths::addNodePathSegment($destinationPath, $childNode->getName()), false);
}
$this->moveNodeToDestinationPath($this, $destinationPath);
return [
[$this, $originalPath, $this->getNodeData()->getPath(), $recursiveCall]
];
}
/**
* Moves a node and sub nodes to the new path given with special logic for aggregate node types.
*
* @param string $destinationPath the new node path
* @param boolean $recursiveCall is this a recursive call
* @return array of arrays with NodeVariant and old and new path and if this was a recursive call
*/
protected function setPathInternalForAggregate(string $destinationPath, bool $recursiveCall): array
{
// This method is NOT called recursively, but just ONCE for the root of the moved node tree.
// we need to figure out what other NodeData (in other dimensions) we can move. We want to move ALL POSSIBLE dimensions,
// but WITHOUT creating disconnected nodes due to the move.
// A disconnected node exists if, for EVERY allowed Dimension Combination,
// the node had an existing parent before the move, but looses the parent after the move.
$this->nodeMoveIntegrityCheckService->ensureIntegrityForDocumentNodeMove($this, $destinationPath);
$originalPath = $this->nodeData->getPath();
$nodeDataVariantsAndChildren = $this->nodeDataRepository->findByPathWithoutReduce($originalPath, $this->context->getWorkspace(), true, true);
$changedNodePathsCollection = array_map(function ($nodeData) use ($destinationPath, $originalPath, $recursiveCall) {
return $this->moveNodeData($nodeData, $originalPath, $destinationPath, $recursiveCall);
}, $nodeDataVariantsAndChildren);
return array_filter($changedNodePathsCollection);
}
/**
* Moves a NodeData object that is either a variant or child node to the given destination path.
*
* @param NodeData $nodeData
* @param string $originalPath
* @param string $destinationPath
* @param boolean $recursiveCall
* @return array|null
* @throws NodeConfigurationException
*/
protected function moveNodeData(NodeData $nodeData, string $originalPath, string $destinationPath, bool $recursiveCall)
{
$recursiveCall = $recursiveCall || ($this->nodeData !== $nodeData);
$nodeVariant = null;
// $nodeData at this point could contain *our own NodeData reference* ($this->nodeData), as we find all NodeData objects
// (across all dimensions) with the same path.
//
// We need to ensure that our own Node object's nodeData reference ($this->nodeData) is also updated correctly if a new NodeData object
// is returned; as we rely on the fact that $this->getPath() will return the new node path in all circumstances.
//
// However, $this->createNodeForVariant() only returns $this if the Context object is the same as $this->context; which is not
// the case if $this->context contains dimension fallbacks such as "Language: EN, DE".
//
// The "if" statement below is actually a workaround to ensure that if the NodeData object is our own one, we update *ourselves* correctly,
// and thus return the correct (new) Node Path when calling $this->getPath() afterwards.
// FIXME: This is dangerous and probably the NodeFactory should take care of globally tracking usage of NodeData objects and replacing them in Node objects
if ($this->nodeData === $nodeData) {
$nodeVariant = $this;
}
if ($nodeVariant === null) {
$nodeVariant = $this->createNodeForVariant($nodeData);
}
$moveVariantResult = $nodeVariant === null ? null : $this->moveVariantOrChild($originalPath, $destinationPath, $nodeVariant);
if ($moveVariantResult !== null) {
array_push($moveVariantResult, $recursiveCall);
}
return $moveVariantResult;
}
/**
* Create a node for the given NodeData, given that it is a variant of the current node
*
* @param NodeData $nodeData
* @return NodeInterface|null
* @throws NodeConfigurationException
*/
protected function createNodeForVariant(NodeData $nodeData): ?NodeInterface
{
$contextProperties = $this->context->getProperties();
$contextProperties['dimensions'] = $nodeData->getDimensionValues();
unset($contextProperties['targetDimensions']);
$adjustedContext = $this->contextFactory->create($contextProperties);
return $this->nodeFactory->createFromNodeData($nodeData, $adjustedContext);
}
/**
* Moves the given variant or child node to the destination defined by the given path which is
* the new path for the originally moved (parent|variant) node
*
* @param string $aggregateOriginalPath
* @param string $aggregateDestinationPath
* @param NodeInterface $nodeToMove
* @return array NodeVariant and old and new path
*/
protected function moveVariantOrChild(string $aggregateOriginalPath, string $aggregateDestinationPath, NodeInterface $nodeToMove = null): ?array
{
if ($nodeToMove === null) {
return null;
}
$variantOriginalPath = $nodeToMove->getPath();
$relativePathSegment = NodePaths::getRelativePathBetween($aggregateOriginalPath, $variantOriginalPath);
$variantDestinationPath = NodePaths::addNodePathSegment($aggregateDestinationPath, $relativePathSegment);
$this->moveNodeToDestinationPath($nodeToMove, $variantDestinationPath);
return [$nodeToMove, $variantOriginalPath, $nodeToMove->getPath()];
}
/**
* Moves the given node to the destination path by modifying the underlaying NodeData object.
*
* @param NodeInterface $node
* @param string $destinationPath
* @return void
*/
protected function moveNodeToDestinationPath(NodeInterface $node, $destinationPath)
{
$nodeData = $node->getNodeData();
$possibleShadowedNodeData = $nodeData->move($destinationPath, $this->context->getWorkspace());
if ($node instanceof Node) {
$node->setNodeData($possibleShadowedNodeData);
}
}
/**
* {@inheritdoc}
*/
public function getOtherNodeVariants(): array
{
return array_filter(
$this->context->getNodeVariantsByIdentifier($this->getIdentifier()),
function ($node) {
return ($node instanceof NodeInterface && $node->getNodeData() !== $this->nodeData);
}
);
}
/**
* @return \DateTimeInterface
*/
public function getCreationDateTime(): \DateTimeInterface
{
return $this->nodeData->getCreationDateTime();
}
/**
* @return \DateTimeInterface
*/
public function getLastModificationDateTime(): \DateTimeInterface
{
return $this->nodeData->getLastModificationDateTime();
}
/**
* @param \DateTimeInterface $lastModificationDateTime
* @return void
*/
public function setLastPublicationDateTime(\DateTimeInterface $lastModificationDateTime)
{
$this->nodeData->setLastPublicationDateTime($lastModificationDateTime);
}
/**
* @return \DateTimeInterface|null Date of last publication or null if the node was not published yet
*/
public function getLastPublicationDateTime(): ?\DateTimeInterface
{
return $this->nodeData->getLastPublicationDateTime();
}
/**
* Returns the path of this node
*
* @return string
* @deprecated with version 4.3, use TraversableNodeInterface::findNodePath() instead.
*/
public function getPath()
{
return $this->nodeData->getPath();
}
/**
* Returns the level at which this node is located.
* Counting starts with 0 for "/", 1 for "/foo", 2 for "/foo/bar" etc.
*
* @return integer
* @deprecated with version 4.3 - Use TraversableNodeInterface::findNodePath()->getDepth() instead
*/
public function getDepth()
{
return $this->nodeData->getDepth();
}
/**
* Returns the name of this node
*
* @return string
* @deprecated with version 4.3, use TraversableNodeInterface::getNodeName() instead.
*/
public function getName()
{
return $this->nodeData->getName();
}
/**
* Returns the node label as generated by the configured node label generator
*
* @return string
* @throws NodeTypeNotFoundException
*/
public function getLabel(): string
{
return $this->getNodeType()->getNodeLabelGenerator()->getLabel($this);
}
/**
* Sets the workspace of this node.
*
* This method is only for internal use by the content repository. Changing
* the workspace of a node manually may lead to unexpected behavior.
*
* @param Workspace $workspace
* @return void
* @throws NodeException
* @throws NodeTypeNotFoundException
*/
public function setWorkspace(Workspace $workspace): void
{
if ($this->getWorkspace()->getName() === $workspace->getName()) {
return;
}
if (!$this->isNodeDataMatchingContext()) {
$this->materializeNodeData();
}
$this->nodeData->setWorkspace($workspace);
$this->context->getFirstLevelNodeCache()->flush();
$this->emitNodeUpdated($this);
}
/**
* Returns the workspace this node is contained in
*
* @return Workspace
*/
public function getWorkspace()
{
return $this->nodeData->getWorkspace();
}
/**
* Returns the identifier of this node
*
* @return string the node's UUID (unique within the workspace)
* @deprecated with version 4.3, use getNodeAggregateIdentifier() instead.
*/
public function getIdentifier()
{
return $this->nodeData->getIdentifier();
}
/**
* Sets the index of this node
*
* NOTE: This method is meant for internal use and must only be used by other nodes.
*
* @param integer $index The new index
* @return void
* @throws NodeException
* @throws NodeTypeNotFoundException
*/
public function setIndex($index): void
{
if ($this->getIndex() === $index) {
return;
}
if (!$this->isNodeDataMatchingContext()) {
$this->materializeNodeData();
}
$this->nodeData->setIndex($index);
$this->context->getFirstLevelNodeCache()->flush();
$this->emitNodeUpdated($this);
}
/**
* Returns the index of this node which determines the order among siblings
* with the same parent node.
*
* @return integer
*/
public function getIndex()
{
return $this->nodeData->getIndex();
}
/**
* Returns the parent node of this node
*
* @return NodeInterface|null The parent node or NULL if this is the root node
* @deprecated with version 4.3, use TraversableNodeInterface::findParentNode() instead.
* Beware that findParentNode() is not fully equivalent to this method.
* It has a different root node handling:
* - findParentNode() throws an exception for the root node
* - getParent() returns <code>null</code> for the root node
*/
public function getParent()
{
if ($this->isRoot()) {
return null;
}
$parentPath = $this->getParentPath();
$node = $this->context->getFirstLevelNodeCache()->getByPath($parentPath);
if ($node !== false) {
return $node;
}
$node = $this->nodeDataRepository->findOneByPathInContext($parentPath, $this->context);
$this->context->getFirstLevelNodeCache()->setByPath($parentPath, $node);
return $node;
}
/**
* Returns the parent node path
*
* @return string Absolute node path of the parent node
* @deprecated with version 4.3, use TraversableNodeInterface::findParentNode()->findNodePath() instead.
*/
public function getParentPath(): string
{
return $this->nodeData->getParentPath();
}
/**
* Whether or not the node is the root node (i.e. has no parent node)
*
* @return bool
*/
public function isRoot(): bool
{
return $this->getPath() === '/';
}
/**
* Moves this node before the given node
*
* @param NodeInterface $referenceNode
* @param string $newName
* @throws NodeConstraintException if a node constraint prevents moving the node
* @throws NodeException if you try to move the root node
* @throws NodeExistsException
* @throws NodeTypeNotFoundException
* @api
*/
public function moveBefore(NodeInterface $referenceNode, string $newName = null): void
{
if ($referenceNode === $this) {
return;
}
if ($this->isRoot()) {
throw new NodeException('The root node cannot be moved.', 1285005924);
}
$name = $newName !== null ? $newName : $this->getName();
$referenceParentNode = $referenceNode->getParent();
if ($referenceParentNode !== $this->getParent() && $referenceParentNode->getNode($name) !== null) {
throw new NodeExistsException(sprintf('Node with path "%s" already exists.', $name), 1292503468);
}
if (($referenceParentNode instanceof Node && !$referenceParentNode->willChildNodeBeAutoCreated($name)) && !$referenceParentNode->isNodeTypeAllowedAsChildNode($this->getNodeType())) {
throw new NodeConstraintException(sprintf('Cannot move %s before %s', $this, $referenceNode), 1400782413);
}
$this->emitBeforeNodeMove($this, $referenceNode, NodeDataRepository::POSITION_BEFORE);
if ($referenceNode->getParentPath() !== $this->getParentPath()) {
$this->setPath(NodePaths::addNodePathSegment($referenceNode->getParentPath(), $name));
} else {
if (!$this->isNodeDataMatchingContext()) {
$this->materializeNodeData();
}
}
$this->nodeDataRepository->setNewIndex($this->nodeData, NodeDataRepository::POSITION_BEFORE, $referenceNode);
$this->context->getFirstLevelNodeCache()->flush();
$this->emitAfterNodeMove($this, $referenceNode, NodeDataRepository::POSITION_BEFORE);
$this->emitNodeUpdated($this);
}
/**
* Moves this node after the given node
*
* @param NodeInterface $referenceNode
* @param string $newName
* @throws NodeConstraintException if a node constraint prevents moving the node
* @throws NodeException
* @throws NodeExistsException
* @throws NodeTypeNotFoundException
* @api
*/
public function moveAfter(NodeInterface $referenceNode, string $newName = null): void
{
if ($referenceNode === $this) {
return;
}
if ($this->isRoot()) {
throw new NodeException('The root node cannot be moved.', 1316361483);
}
$name = $newName !== null ? $newName : $this->getName();
$referenceParentNode = $referenceNode->getParent();
if ($referenceParentNode !== $this->getParent() && $referenceParentNode->getNode($name) !== null) {
throw new NodeExistsException(sprintf('Node with path "%s" already exists.', $name), 1292503469);
}
if (($referenceParentNode instanceof Node && !$referenceParentNode->willChildNodeBeAutoCreated($name)) && !$referenceParentNode->isNodeTypeAllowedAsChildNode($this->getNodeType())) {
throw new NodeConstraintException(sprintf('Cannot move %s after %s', $this, $referenceNode), 1404648100);
}
$this->emitBeforeNodeMove($this, $referenceNode, NodeDataRepository::POSITION_AFTER);
if ($referenceNode->getParentPath() !== $this->getParentPath()) {
$this->setPath(NodePaths::addNodePathSegment($referenceNode->getParentPath(), $name));
} else {
if (!$this->isNodeDataMatchingContext()) {
$this->materializeNodeData();
}
}
$this->nodeDataRepository->setNewIndex($this->nodeData, NodeDataRepository::POSITION_AFTER, $referenceNode);
$this->context->getFirstLevelNodeCache()->flush();
$this->emitAfterNodeMove($this, $referenceNode, NodeDataRepository::POSITION_AFTER);
$this->emitNodeUpdated($this);
}
/**
* Moves this node into the given node
*
* @param NodeInterface $referenceNode
* @param string $newName
* @throws NodeConstraintException
* @throws NodeException
* @throws NodeExistsException
* @throws NodeTypeNotFoundException
* @api
*/
public function moveInto(NodeInterface $referenceNode, string $newName = null): void
{
if ($referenceNode === $this || $referenceNode === $this->getParent()) {
return;
}
if ($this->isRoot()) {
throw new NodeException('The root node cannot be moved.', 1346769001);
}
$name = $newName !== null ? $newName : $this->getName();
if ($referenceNode !== $this->getParent() && $referenceNode->getNode($name) !== null) {
throw new NodeExistsException(sprintf('Node with path "%s" already exists.', $name), 1292503470);
}
if (($referenceNode instanceof Node && !$referenceNode->willChildNodeBeAutoCreated($name)) && !$referenceNode->isNodeTypeAllowedAsChildNode($this->getNodeType())) {
throw new NodeConstraintException(sprintf('Cannot move %s into %s', $this, $referenceNode), 1404648124);
}
$this->emitBeforeNodeMove($this, $referenceNode, NodeDataRepository::POSITION_LAST);
$this->setPath(NodePaths::addNodePathSegment($referenceNode->getPath(), $name));
$this->nodeDataRepository->setNewIndex($this->nodeData, NodeDataRepository::POSITION_LAST);
$this->context->getFirstLevelNodeCache()->flush();
$this->emitAfterNodeMove($this, $referenceNode, NodeDataRepository::POSITION_LAST);
$this->emitNodeUpdated($this);
}
/**
* Copies this node before the given node
*
* @param NodeInterface $referenceNode
* @param string $nodeName
* @return NodeInterface
* @throws NodeConstraintException
* @throws NodeException
* @throws NodeExistsException
* @throws NodeTypeNotFoundException
* @api
*/
public function copyBefore(NodeInterface $referenceNode, $nodeName): NodeInterface
{
if ($referenceNode->getParent()->getNode($nodeName) !== null) {
throw new NodeExistsException(sprintf('Node with path "%s/%s" already exists.', $referenceNode->getParent()->getPath(), $nodeName), 1292503465);
}
if (!$referenceNode->getParent()->isNodeTypeAllowedAsChildNode($this->getNodeType())) {
throw new NodeConstraintException(sprintf('Cannot copy %s before %s', $this, $referenceNode), 1402050232);
}
$this->emitBeforeNodeCopy($this, $referenceNode->getParent());
$copiedNode = $this->createRecursiveCopy($referenceNode->getParent(), $nodeName, $this->getNodeType()->isAggregate());
$copiedNode->moveBefore($referenceNode);
$this->context->getFirstLevelNodeCache()->flush();
$this->emitNodeAdded($copiedNode);
$this->emitAfterNodeCopy($copiedNode, $referenceNode->getParent());
return $copiedNode;
}
/**
* Copies this node after the given node
*
* @param NodeInterface $referenceNode
* @param string $nodeName
* @return NodeInterface
* @throws NodeConstraintException
* @throws NodeException
* @throws NodeExistsException
* @throws NodeTypeNotFoundException
* @api
*/
public function copyAfter(NodeInterface $referenceNode, $nodeName): NodeInterface
{
if ($referenceNode->getParent()->getNode($nodeName) !== null) {
throw new NodeExistsException(sprintf('Node with path "%s/%s" already exists.', $referenceNode->getParent()->getPath(), $nodeName), 1292503466);
}
if (!$referenceNode->getParent()->isNodeTypeAllowedAsChildNode($this->getNodeType())) {
throw new NodeConstraintException(sprintf('Cannot copy %s after %s', $this, $referenceNode), 1404648170);
}
$this->emitBeforeNodeCopy($this, $referenceNode->getParent());
$copiedNode = $this->createRecursiveCopy($referenceNode->getParent(), $nodeName, $this->getNodeType()->isAggregate());
$copiedNode->moveAfter($referenceNode);
$this->context->getFirstLevelNodeCache()->flush();
$this->emitNodeAdded($copiedNode);
$this->emitAfterNodeCopy($copiedNode, $referenceNode->getParent());
return $copiedNode;
}
/**
* Copies this node into the given node
*
* @param NodeInterface $referenceNode
* @param string $nodeName
* @return NodeInterface
* @throws NodeConstraintException
* @throws NodeException
* @throws NodeExistsException
* @throws NodeTypeNotFoundException
* @api
*/
public function copyInto(NodeInterface $referenceNode, $nodeName): NodeInterface
{
$this->emitBeforeNodeCopy($this, $referenceNode);
$copiedNode = $this->copyIntoInternal($referenceNode, $nodeName, $this->getNodeType()->isAggregate());
$this->emitAfterNodeCopy($copiedNode, $referenceNode);
return $copiedNode;
}
/**
* Internal method to do the actual copying.
*
* For behavior of the $detachedCopy parameter, see method Node::createRecursiveCopy().
*
* @param NodeInterface $referenceNode
* @param string $nodeName
* @param boolean $detachedCopy
* @return NodeInterface
* @throws NodeConstraintException
* @throws NodeException
* @throws NodeExistsException
* @throws NodeTypeNotFoundException
*/
protected function copyIntoInternal(NodeInterface $referenceNode, string $nodeName, bool $detachedCopy): NodeInterface
{
if ($referenceNode->getNode($nodeName) !== null) {
throw new NodeExistsException('Node with path "' . $referenceNode->getPath() . '/' . $nodeName . '" already exists.', 1292503467);
}
// On copy we basically re-recreate an existing node on a new location. As we skip the constraints check on
// node creation we should do the same while writing the node on the new location.
if (($referenceNode instanceof Node && !$referenceNode->willChildNodeBeAutoCreated($nodeName)) && !$referenceNode->isNodeTypeAllowedAsChildNode($this->getNodeType())) {
throw new NodeConstraintException(sprintf('Cannot copy "%s" into "%s" due to node type constraints.', $this->__toString(), $referenceNode->__toString()), 1404648177);
}
$copiedNode = $this->createRecursiveCopy($referenceNode, $nodeName, $detachedCopy);
$this->context->getFirstLevelNodeCache()->flush();
$this->emitNodeAdded($copiedNode);
return $copiedNode;
}
/**
* Sets the specified property.
*
* If the node has a content object attached, the property will be set there
* if it is settable.
*
* @param string $propertyName Name of the property
* @param mixed $value Value of the property
* @return mixed
* @throws NodeException
* @throws NodeTypeNotFoundException
* @throws \Neos\Flow\Property\Exception
* @throws \Neos\Flow\Security\Exception
* @api
*/
public function setProperty($propertyName, $value): void
{
$this->materializeNodeDataAsNeeded();
// Arrays could potentially contain entities and objects could be entities. In that case even if the object is the same it needs to be persisted in NodeData.
if (!is_object($value) && !is_array($value) && $this->getProperty($propertyName) === $value) {
return;
}
$oldValue = $this->hasProperty($propertyName) ? $this->getProperty($propertyName) : null;
$this->emitBeforeNodePropertyChange($this, $propertyName, $oldValue, $value);
$this->nodeData->setProperty($propertyName, $value);
$this->context->getFirstLevelNodeCache()->flush();
$this->emitNodePropertyChanged($this, $propertyName, $oldValue, $value);
$this->emitNodeUpdated($this);
}
/**
* If this node has a property with the given name.
*
* If the node has a content object attached, the property will be checked
* there.
*
* @param string $propertyName
* @return boolean
* @api
*/
public function hasProperty($propertyName): bool
{
return $this->nodeData->hasProperty($propertyName);
}
/**
* Returns the specified property.
*
* If the node has a content object attached, the property will be fetched
* there if it is gettable.
*
* @param string $propertyName Name of the property
* @param boolean $returnNodesAsIdentifiers If enabled, references to nodes are returned as node identifiers instead of NodeInterface instances
* @return mixed value of the property
* @throws NodeException
* @throws \Neos\Flow\Property\Exception
* @throws \Neos\Flow\Security\Exception
* @throws NodeTypeNotFoundException
* @api
*/
public function getProperty($propertyName, bool $returnNodesAsIdentifiers = false)
{
$value = $this->nodeData->getProperty($propertyName);
$nodeType = $this->getNodeType();
$expectedPropertyType = null;
if ($nodeType !== null) {
$expectedPropertyType = $nodeType->getPropertyType($propertyName);
}
if (
/** @phpstan-ignore-next-line i will not touch this code */
isset($expectedPropertyType) &&
$expectedPropertyType === 'Neos\Media\Domain\Model\ImageInterface' &&
empty($value)
) {
return null;
}
if (empty($value)) {
return $value;
}
if (!$nodeType->hasConfiguration('properties.' . $propertyName)) {
return $value;
}
if ($expectedPropertyType === 'references') {
return ($returnNodesAsIdentifiers ? $value : $this->resolvePropertyReferences($value));
}
if ($expectedPropertyType === 'reference') {
return ($returnNodesAsIdentifiers ? $value : $this->context->getNodeByIdentifier($value));
}
try {
/**
* In case the value is a value object it _will_ already be deserialized due to the feature in flow_json_array
* {@see \Neos\Flow\Persistence\Doctrine\DataTypes\JsonArrayType::deserializeValueObject}
*/
return $this->propertyMapper->convert($value, $expectedPropertyType);
} catch (\Neos\Flow\Property\Exception $exception) {
throw new NodeException(sprintf('Failed to convert property "%s" of node "%s" to the expected type of "%s": %s', $propertyName, $this->getIdentifier(), $expectedPropertyType, $exception->getMessage()), 1630675703, $exception);
}
}
/**
* Maps the property value (an array of node identifiers) to the Node objects if needed.
*
* @param array $value
* @return array
*/
protected function resolvePropertyReferences(array $value = []): array
{
$nodes = [];
foreach ($value as $nodeIdentifier) {
$node = $this->context->getNodeByIdentifier($nodeIdentifier);
// $node can be NULL if the node is not visible (or removed) according to the current content context:
if ($node !== null) {
$nodes[] = $node;
}
}
return $nodes;
}
/**
* Removes the specified property.
*
* If the node has a content object attached, the property will not be removed on
* that object if it exists.
*
* @param string $propertyName Name of the property
* @return void
* @throws NodeException if the node does not contain the specified property
* @throws NodeTypeNotFoundException
*/
public function removeProperty($propertyName): void
{
if (!$this->hasProperty($propertyName)) {
return;
}
$this->materializeNodeDataAsNeeded();
$this->nodeData->removeProperty($propertyName);
$this->context->getFirstLevelNodeCache()->flush();
$this->emitNodeUpdated($this);
}