Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 16 additions & 24 deletions Classes/NodeSignalInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,39 @@
*/

use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Exception\NodeException;
use Neos\Flow\Annotations as Flow;
use PunktDe\NodeReplicator\Replicator\NodeReplicator;

/**
* @Flow\Scope("singleton")
*/
class NodeSignalInterceptor
{
/**
* @Flow\Inject
* @var NodeReplicator
*/
protected $nodeReplicator;

/**
* @param NodeInterface $node
*/
public static function nodeAdded(NodeInterface $node): void
public function nodeAdded(NodeInterface $node): void
{
if (self::hasReplicationConfiguration($node) && (self::nodeCreateReplicationEnabled($node) || self::nodeCreateHiddenEnabled($node))) {
// The nodedAdded signal is called twice. When it is called the first time,
// The nodeAdded signal is called twice. When it is called the first time,
// the node is not created completely (the child nodes are not created yet), so we skip that call.
if (count($node->getNodeType()->getAutoCreatedChildNodes()) > 0 && count($node->findChildNodes()) === 0) {
return;
}
self::getNodeReplicator()->createNodeVariants($node, self::nodeCreateHiddenEnabled($node));
$this->nodeReplicator->createNodeVariants($node, self::nodeCreateHiddenEnabled($node));
}
}

public function nodeRemoved(NodeInterface $node): void
{
if (self::hasReplicationConfiguration($node) && self::nodeRemoveReplicationEnabled($node)) {
self::getNodeReplicator()->removeNodeVariants($node);
$this->nodeReplicator->removeNodeVariants($node);
}
}

Expand All @@ -43,12 +52,12 @@ public function nodePropertyChanged(NodeInterface $node, string $propertyName, $
}

if ($node->getNodeType()->getConfiguration('properties.' . $propertyName . '.options.replication.updateEmptyOnly')) {
self::getNodeReplicator()->updateContent($node, $propertyName, $newValue, true);
$this->nodeReplicator->updateContent($node, $propertyName, $newValue, true);
return;
}

if ($node->getNodeType()->getConfiguration('properties.' . $propertyName . '.options.replication.update')) {
self::getNodeReplicator()->updateContent($node, $propertyName, $newValue, false);
$this->nodeReplicator->updateContent($node, $propertyName, $newValue, false);
}
}

Expand All @@ -71,21 +80,4 @@ protected static function nodeCreateHiddenEnabled(NodeInterface $node): bool
{
return $node->getNodeType()->hasConfiguration('options.replication.structure.createHidden') && $node->getNodeType()->getConfiguration('options.replication.structure.createHidden');
}

/**
* @return Replicator\NodeReplicator
*/
protected static function getNodeReplicator(): NodeReplicator
{
return new Replicator\NodeReplicator();
}

/**
* @param NodeInterface $node
* @return array|null
*/
protected static function getExcludedProperties(NodeInterface $node): ?array

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ist das korrekt, dass die rausfliegt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ich denke, weil den config key "excludeProperties" gibts in der neuen version eigentlich nicht mehr.

{
return $node->getNodeType()->getConfiguration('options.replication.excludeProperties');
}
}
6 changes: 3 additions & 3 deletions Classes/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class Package extends BasePackage
public function boot(Bootstrap $bootstrap)
{
$dispatcher = $bootstrap->getSignalSlotDispatcher();
$dispatcher->connect(Node::class, 'nodeAdded', NodeSignalInterceptor::class, '::nodeAdded');
$dispatcher->connect(Node::class, 'nodePropertyChanged', NodeSignalInterceptor::class, '::nodePropertyChanged');
$dispatcher->connect(Node::class, 'nodeRemoved', NodeSignalInterceptor::class, '::nodeRemoved');
$dispatcher->connect(Node::class, 'nodeAdded', NodeSignalInterceptor::class, 'nodeAdded');
$dispatcher->connect(Node::class, 'nodePropertyChanged', NodeSignalInterceptor::class, 'nodePropertyChanged');
$dispatcher->connect(Node::class, 'nodeRemoved', NodeSignalInterceptor::class, 'nodeRemoved');
}
}
26 changes: 21 additions & 5 deletions Classes/Replicator/NodeReplicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
*/

use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Exception\NodeException;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Log\Utility\LogEnvironment;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Neos\Flow\Persistence\PersistenceManagerInterface;

/**
* @Flow\Scope("singleton")
Expand All @@ -27,6 +25,11 @@ class NodeReplicator
*/
protected $logger;

/**
* @var boolean
*/
protected $isReplicating = false;

public function createNodeVariants(NodeInterface $node, bool $createHidden = false): void
{
foreach ($this->getParentVariants($node) as $parentVariant) {
Expand Down Expand Up @@ -61,6 +64,13 @@ public function removeNodeVariants(NodeInterface $node): void

public function updateContent(NodeInterface $node, string $propertyName, $newValue, bool $updateEmptyOnly)
{
// Setting properties on nodes trigger the nodePropertyChanged signal so we need to prevent this function to keep
// running in a loop (or at least more often as it would need). The solution here introduces state but it is
// most likely okay because there is no concurrency.
if ($this->isReplicating === true) {
return;
}

foreach ($this->getParentVariants($node) as $parentVariant) {
$variantNode = $parentVariant->getContext()->getNodeByIdentifier($node->getIdentifier());

Expand All @@ -73,8 +83,14 @@ public function updateContent(NodeInterface $node, string $propertyName, $newVal
continue;
}

$variantNode->setProperty($propertyName, $newValue);
$this->logReplicationAction($node, sprintf('Property %s of the node was updated', $propertyName), __METHOD__);
$this->isReplicating = true;
try {
$variantNode->setProperty($propertyName, $newValue);
} finally {
$this->isReplicating = false;
}

$this->logReplicationAction($variantNode, sprintf('Property %s of the node was updated', $propertyName), __METHOD__);
}
}

Expand All @@ -91,7 +107,7 @@ protected function getParentVariants(NodeInterface $node): array
if ($node->getParent() === null) {
return [];
}

return $node->getParent()->getOtherNodeVariants();
}

Expand Down