Skip to content

Commit

Permalink
Merge pull request #8 from punktDe/bugfix/use-condtion-from-node-move…
Browse files Browse the repository at this point in the history
…Into

TASK: Use Feedback objects to signal the frontend what actually happened
  • Loading branch information
daniellienert authored Feb 14, 2020
2 parents 960841a + 7da61ea commit 9d0fadd
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 12 deletions.
40 changes: 40 additions & 0 deletions Classes/AffectedNodeStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);

namespace PunktDe\Archivist;

/*
* This file is part of the PunktDe.Archivist package.
*
* 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\Model\NodeInterface;
use Neos\Flow\Annotations as Flow;

/**
* @Flow\Scope("singleton")
*/
class AffectedNodeStorage
{

/**
* @var array
*/
protected $affectedNodePaths = [];

public function addNode(NodeInterface $node): void
{
$this->affectedNodePaths[] = (string)$node->getContextPath();
}

/**
* @param NodeInterface $node
* @return bool
*/
public function hasNode(NodeInterface $node): bool
{
return in_array($node->getContextPath(), $this->affectedNodePaths);
}
}
58 changes: 51 additions & 7 deletions Classes/Archivist.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
use Neos\Eel\FlowQuery\FlowQuery;
use Neos\Flow\Annotations as Flow;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Flow\Log\ThrowableStorageInterface;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Neos\Ui\Domain\Model\Feedback\Operations\NodeCreated;
use Neos\Neos\Ui\Domain\Model\Feedback\Operations\UpdateNodeInfo;
use Neos\Neos\Ui\Domain\Model\FeedbackCollection;
use Psr\Log\LoggerInterface;
use PunktDe\Archivist\Exception\ArchivistConfigurationException;
use PunktDe\Archivist\Service\EelEvaluationService;
Expand Down Expand Up @@ -52,6 +56,12 @@ class Archivist
*/
protected $logger;

/**
* @Flow\Inject
* @var ThrowableStorageInterface
*/
protected $throwableStorage;

/**
* @var array
*/
Expand All @@ -72,14 +82,26 @@ class Archivist
*/
protected $nodesInProcessing = [];

/**
* @Flow\Inject
* @var FeedbackCollection
*/
protected $feedbackCollection;

/**
* @Flow\Inject
* @var AffectedNodeStorage
*/
protected $affectedNodeStorage;

/**
* @param NodeInterface $triggeringNode
* @param array $sortingInstructions
* @throws ArchivistConfigurationException
* @throws \Neos\ContentRepository\Exception\NodeTypeNotFoundException
* @throws \Neos\Eel\Exception
*/
public function organizeNode(NodeInterface $triggeringNode, array $sortingInstructions)
public function organizeNode(NodeInterface $triggeringNode, array $sortingInstructions): void
{
if (isset($sortingInstructions['condition'])) {
$condition = $this->eelEvaluationService->evaluate($sortingInstructions['condition'], ['node' => $triggeringNode]);
Expand Down Expand Up @@ -112,11 +134,14 @@ public function organizeNode(NodeInterface $triggeringNode, array $sortingInstru
if (isset($sortingInstructions['hierarchy']) && is_array($sortingInstructions['hierarchy'])) {
$hierarchyNode = $this->hierarchyService->buildHierarchy($sortingInstructions['hierarchy'], $context, $sortingInstructions['publishHierarchy'] ?? false);

if ($affectedNode->getParent()->getPath() !== $hierarchyNode->getPath()) {
$affectedNode->moveInto($hierarchyNode);
if ($hierarchyNode !== $affectedNode->getParent() && $hierarchyNode->getNode($affectedNode->getName()) === null) {

$this->affectedNodeStorage->addNode($affectedNode);
$affectedNode->moveInto($hierarchyNode);
$this->organizedNodeParents[$affectedNode->getIdentifier()] = $affectedNode->getParent();

$this->sendNodeMovedFeedback($affectedNode, $hierarchyNode);

$this->logger->debug(sprintf('Moved affected node %s to path %s', $affectedNode->getNodeType()->getName(), $affectedNode->getPath()), LogEnvironment::fromMethodName(__METHOD__));
}
}
Expand Down Expand Up @@ -147,7 +172,7 @@ public function organizeNode(NodeInterface $triggeringNode, array $sortingInstru
public function restorePathIfOrganizedDuringThisRequest(NodeInterface $node): bool
{
if (isset($this->organizedNodeParents[$node->getIdentifier()])) {
if ($node->getParent() === $this->organizedNodeParents[$node->getIdentifier()]) {
if ($this->organizedNodeParents[$node->getIdentifier()] !== $node->getParent() && $this->organizedNodeParents[$node->getIdentifier()]->getNode($node->getName()) !== null) {
return true;
}

Expand All @@ -168,23 +193,23 @@ public function restorePathIfOrganizedDuringThisRequest(NodeInterface $node): bo
* @param NodeInterface $node
* @return bool
*/
public function isNodeInProcess(NodeInterface $node)
public function isNodeInProcess(NodeInterface $node): bool
{
return isset($this->nodesInProcessing[$node->getIdentifier()]);
}

/**
* @param NodeInterface $node
*/
protected function lockNodeForProcessing(NodeInterface $node)
protected function lockNodeForProcessing(NodeInterface $node): void
{
$this->nodesInProcessing[$node->getIdentifier()] = true;
}

/**
* @param NodeInterface $node
*/
protected function releaseNodeProcessingLock(NodeInterface $node)
protected function releaseNodeProcessingLock(NodeInterface $node): void
{
unset($this->nodesInProcessing[$node->getIdentifier()]);
}
Expand Down Expand Up @@ -232,6 +257,25 @@ protected function buildCustomContext(array $baseContext, array $contextConfigur
}
return $customContext;
}

/**
* @param NodeInterface $affectedNode
* @param NodeInterface $hierarchyNode
*/
private function sendNodeMovedFeedback(NodeInterface $affectedNode, NodeInterface $hierarchyNode): void
{
$created = new NodeCreated();
$created->setNode($affectedNode);
$this->feedbackCollection->add($created);

$updateNodeInfo = new UpdateNodeInfo();
$updateNodeInfo->setNode($hierarchyNode);
$this->feedbackCollection->add($updateNodeInfo);

$updateNodeInfo = new UpdateNodeInfo();
$updateNodeInfo->setNode($affectedNode);
$this->feedbackCollection->add($updateNodeInfo);
}
}


55 changes: 55 additions & 0 deletions Classes/Aspect/FeedbackCreationAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);

namespace PunktDe\Archivist\Aspect;

/*
* This file is part of the PunktDe.Archivist package.
*
* 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\Model\NodeInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Aop\JoinPointInterface;
use Neos\Flow\Log\Utility\LogEnvironment;
use Psr\Log\LoggerInterface;
use PunktDe\Archivist\AffectedNodeStorage;

/**
* @Flow\Scope("singleton")
* @Flow\Aspect
*/
class FeedbackCreationAspect
{
/**
* @Flow\Inject
* @var AffectedNodeStorage
*/
protected $affectedNodeStorage;

/**
* @Flow\Inject
* @var LoggerInterface
*/
protected $logger;

/**
* @Flow\Around("method(Neos\Neos\Ui\Domain\Model\AbstractChange->addNodeCreatedFeedback())")
* @param JoinPointInterface $joinPoint The current join point
* @return void
*/
public function addNodeCreatedFeedback(JoinPointInterface $joinPoint): void
{
/** @var NodeInterface $subject */
$subject = $joinPoint->getMethodArgument('subject');
if($subject instanceof NodeInterface && $this->affectedNodeStorage->hasNode($subject)) {
$this->logger->debug('Prevented the original node created feedback, as it would return the wrong node path.', LogEnvironment::fromMethodName(__METHOD__));
return;
}

$joinPoint->getAdviceChain()->proceed($joinPoint);
}
}
4 changes: 2 additions & 2 deletions Classes/NodeSignalInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class NodeSignalInterceptor
* @throws NodeTypeNotFoundException
* @throws \Neos\Eel\Exception
*/
public function nodeAdded(NodeInterface $node)
public function nodeAdded(NodeInterface $node): void
{
if (!array_key_exists($node->getNodeType()->getName(), $this->sortingInstructions ?? [])) {
return;
Expand All @@ -51,7 +51,7 @@ public function nodeAdded(NodeInterface $node)
* @throws NodeTypeNotFoundException
* @throws \Neos\Eel\Exception
*/
public function nodeUpdated(NodeInterface $node)
public function nodeUpdated(NodeInterface $node): void
{
if($this->createArchivist()->isNodeInProcess($node)) {
return;
Expand Down
31 changes: 28 additions & 3 deletions Classes/Service/HierarchyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
use Neos\Eel\FlowQuery\FlowQuery;
use Neos\ContentRepository\Utility as NodeUtility;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Neos\Ui\Domain\Model\Feedback\Operations\NodeCreated;
use Neos\Neos\Ui\Domain\Model\Feedback\Operations\UpdateNodeInfo;
use Neos\Neos\Ui\Domain\Model\FeedbackCollection;
use Psr\Log\LoggerInterface;
use PunktDe\Archivist\Exception\ArchivistConfigurationException;

Expand Down Expand Up @@ -60,6 +63,12 @@ class HierarchyService
*/
protected $publishingService;

/**
* @Flow\Inject
* @var FeedbackCollection
*/
protected $feedbackCollection;

/**
* @Flow\Inject
* @var LoggerInterface
Expand Down Expand Up @@ -115,11 +124,10 @@ protected function buildHierarchyLevel(NodeInterface $parentNode, array $hierarc
$hierarchyLevelNodeTemplate = new NodeTemplate();
$hierarchyLevelNodeTemplate->setNodeType($hierarchyLevelNodeType);


if (isset($hierarchyLevelConfiguration['properties']['name'])) {
$hierarchyLevelNodeName = (string)$this->eelEvaluationService->evaluateIfValidEelExpression($hierarchyLevelConfiguration['properties']['name'], $context);

if($hierarchyLevelNodeName !== '') {
if ($hierarchyLevelNodeName !== '') {
$hierarchyLevelNodeTemplate->setName(NodeUtility::renderValidNodeName($hierarchyLevelNodeName));
} else {
$hierarchyLevelNodeName = null;
Expand Down Expand Up @@ -152,6 +160,8 @@ protected function buildHierarchyLevel(NodeInterface $parentNode, array $hierarc

$this->nodeDataRepository->persistEntities();

$this->sendNodeCreatedFeedback($parentNode, $hierarchyLevelNode);

return $hierarchyLevelNode;
}

Expand All @@ -173,7 +183,7 @@ protected function applyProperties(NodeTemplate $node, array $properties, array
* @param array $hierarchyLevelConfiguration
* @throws ArchivistConfigurationException
*/
protected function evaluateHierarchyLevelConfiguration(array $hierarchyLevelConfiguration)
protected function evaluateHierarchyLevelConfiguration(array $hierarchyLevelConfiguration): void
{
if (!isset($hierarchyLevelConfiguration['type'])) {
throw new ArchivistConfigurationException('Missing "type" setting for archivist hierarchy', 1516371948);
Expand Down Expand Up @@ -232,4 +242,19 @@ protected function publishNodeAndChildContent(NodeInterface $node): void
$this->logger->debug('Publishing node ' . $node->__toString(), LogEnvironment::fromMethodName(__METHOD__));
$this->publishingService->publishNode($node);
}

/**
* @param NodeInterface $parentNode
* @param NodeInterface $hierarchyLevelNode
*/
private function sendNodeCreatedFeedback(NodeInterface $parentNode, NodeInterface $hierarchyLevelNode): void
{
$createdNodeInfo = new NodeCreated();
$createdNodeInfo->setNode($hierarchyLevelNode);
$this->feedbackCollection->add($createdNodeInfo);

$updateNodeInfo = new UpdateNodeInfo();
$updateNodeInfo->setNode($parentNode);
$this->feedbackCollection->add($updateNodeInfo);
}
}

0 comments on commit 9d0fadd

Please sign in to comment.