Skip to content

Commit

Permalink
Merge pull request #3640 from neos/task/cleanupNodeService
Browse files Browse the repository at this point in the history
TASK: cleanup node service
  • Loading branch information
mhsdesign authored Oct 16, 2023
2 parents adc3a9a + 7a0cde7 commit 9dfd8a9
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,54 +21,20 @@
use Neos\Neos\Utility\NodeTypeWithFallbackProvider;

/**
* @internal
* @Flow\Scope("singleton")
*/
class NodeService
class NeosUiNodeService
{
use NodeTypeWithFallbackProvider;

#[Flow\Inject]
protected ContentRepositoryRegistry $contentRepositoryRegistry;

/**
* Helper method to retrieve the closest document for a node
*/
public function getClosestDocument(Node $node): ?Node
{
if ($this->getNodeType($node)->isOfType('Neos.Neos:Document')) {
return $node;
}

$subgraph = $this->contentRepositoryRegistry->subgraphForNode($node);

while ($node instanceof Node) {
if ($this->getNodeType($node)->isOfType('Neos.Neos:Document')) {
return $node;
}
$node = $subgraph->findParentNode($node->nodeAggregateId);
}

return null;
}

/**
* Helper method to check if a given node is a document node.
*
* @param Node $node The node to check
* @return boolean A boolean which indicates if the given node is a document node.
*/
public function isDocument(Node $node): bool
{
return ($this->getClosestDocument($node) === $node);
}

/**
* Converts a given context path to a node object
*/
public function getNodeFromContextPath(string $contextPath, ContentRepositoryId $contentRepositoryId): ?Node
public function findNodeBySerializedNodeAddress(string $serializedNodeAddress, ContentRepositoryId $contentRepositoryId): ?Node
{
$contentRepository = $this->contentRepositoryRegistry->get($contentRepositoryId);
$nodeAddress = NodeAddressFactory::create($contentRepository)->createFromUriString($contextPath);
$nodeAddress = NodeAddressFactory::create($contentRepository)->createFromUriString($serializedNodeAddress);

$subgraph = $contentRepository->getContentGraph()->getSubgraph(
$nodeAddress->contentStreamId,
Expand Down
20 changes: 4 additions & 16 deletions Classes/ContentRepository/Service/WorkspaceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
use Neos\ContentRepository\Core\ContentRepository;
use Neos\ContentRepository\Core\Factory\ContentRepositoryId;
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Command\DiscardIndividualNodesFromWorkspace;
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindClosestNodeFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\Projection\Workspace\Workspace;
use Neos\Neos\Domain\Service\NodeTypeNameFactory;
use Neos\Neos\FrontendRouting\NodeAddress;
use Neos\Neos\FrontendRouting\NodeAddressFactory;
use Neos\ContentRepository\Core\Projection\ContentGraph\VisibilityConstraints;
Expand Down Expand Up @@ -97,7 +99,7 @@ public function getPublishableNodeInfo(WorkspaceName $workspaceName, ContentRepo
$node = $subgraph->findNodeById($change->nodeAggregateId);

if ($node instanceof Node) {
$documentNode = $this->getClosestDocumentNode($node);
$documentNode = $subgraph->findClosestNode($node->nodeAggregateId, FindClosestNodeFilter::create(nodeTypeConstraints: NodeTypeNameFactory::NAME_DOCUMENT));
if ($documentNode instanceof Node) {
$contentRepository = $this->contentRepositoryRegistry->get($documentNode->subgraphIdentity->contentRepositoryId);
$nodeAddressFactory = NodeAddressFactory::create($contentRepository);
Expand Down Expand Up @@ -161,7 +163,7 @@ public function predictRemoveNodeFeedbackFromDiscardIndividualNodesFromWorkspace
): array {
$workspace = $contentRepository->getWorkspaceFinder()->findOneByName($command->workspaceName);
if (is_null($workspace)) {
return Nodes::createEmpty();
return [];
}

$changeFinder = $contentRepository->projectionState(ChangeFinder::class);
Expand Down Expand Up @@ -201,18 +203,4 @@ public function predictRemoveNodeFeedbackFromDiscardIndividualNodesFromWorkspace

return $result;
}

private function getClosestDocumentNode(Node $node): ?Node
{
$subgraph = $this->contentRepositoryRegistry->subgraphForNode($node);

while ($node instanceof Node) {
if ($this->getNodeType($node)->isOfType('Neos.Neos:Document')) {
return $node;
}
$node = $subgraph->findParentNode($node->nodeAggregateId);
}

return null;
}
}
6 changes: 3 additions & 3 deletions Classes/Controller/BackendServiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
use Neos\Neos\FrontendRouting\NodeAddressFactory;
use Neos\Neos\FrontendRouting\SiteDetection\SiteDetectionResult;
use Neos\Neos\Service\UserService;
use Neos\Neos\Ui\ContentRepository\Service\NodeService;
use Neos\Neos\Ui\ContentRepository\Service\NeosUiNodeService;
use Neos\Neos\Ui\ContentRepository\Service\WorkspaceService;
use Neos\Neos\Ui\Domain\Model\ChangeCollection;
use Neos\Neos\Ui\Domain\Model\Feedback\Messages\Error;
Expand Down Expand Up @@ -88,7 +88,7 @@ class BackendServiceController extends ActionController

/**
* @Flow\Inject
* @var NodeService
* @var NeosUiNodeService
*/
protected $nodeService;

Expand Down Expand Up @@ -590,7 +590,7 @@ public function flowQueryAction(array $chain): string
/** @var array<int,mixed> $payload */
$payload = $createContext['payload'] ?? [];
$flowQuery = new FlowQuery(array_map(
fn ($envelope) => $this->nodeService->getNodeFromContextPath($envelope['$node'], $contentRepositoryId),
fn ($envelope) => $this->nodeService->findNodeBySerializedNodeAddress($envelope['$node'], $contentRepositoryId),
$payload
));

Expand Down
17 changes: 4 additions & 13 deletions Classes/Domain/Model/AbstractChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
* source code.
*/

use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindClosestNodeFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Neos\Domain\Service\NodeTypeNameFactory;
use Neos\Neos\Service\UserService;
use Neos\Neos\Ui\Domain\Model\Feedback\Operations\NodeCreated;
use Neos\Neos\Ui\Domain\Model\Feedback\Operations\ReloadDocument;
Expand Down Expand Up @@ -64,7 +66,8 @@ public function getSubject(): ?Node
protected function updateWorkspaceInfo(): void
{
if (!is_null($this->subject)) {
$documentNode = $this->findClosestDocumentNode($this->subject);
$subgraph = $this->contentRepositoryRegistry->subgraphForNode($this->subject);
$documentNode = $subgraph->findClosestNode($this->subject->nodeAggregateId, FindClosestNodeFilter::create(nodeTypeConstraints: NodeTypeNameFactory::NAME_DOCUMENT));
if (!is_null($documentNode)) {
$contentRepository = $this->contentRepositoryRegistry->get($this->subject->subgraphIdentity->contentRepositoryId);
$workspace = $contentRepository->getWorkspaceFinder()->findOneByCurrentContentStreamId(
Expand All @@ -78,18 +81,6 @@ protected function updateWorkspaceInfo(): void
}
}

final protected function findClosestDocumentNode(Node $node): ?Node
{
while ($node instanceof Node) {
if ($this->getNodeType($node)->isOfType('Neos.Neos:Document')) {
return $node;
}
$node = $this->findParentNode($node);
}

return null;
}

protected function findParentNode(Node $node): ?Node
{
return $this->contentRepositoryRegistry->subgraphForNode($node)
Expand Down
6 changes: 3 additions & 3 deletions Classes/Domain/Model/Changes/AbstractStructuralChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Neos\ContentRepository\Core\Projection\ContentGraph\Nodes;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Annotations as Flow;
use Neos\Neos\Ui\ContentRepository\Service\NodeService;
use Neos\Neos\Ui\ContentRepository\Service\NeosUiNodeService;
use Neos\Neos\Ui\Domain\Model\AbstractChange;
use Neos\Neos\Ui\Domain\Model\Feedback\Operations\ReloadDocument;
use Neos\Neos\Ui\Domain\Model\Feedback\Operations\RenderContentOutOfBand;
Expand Down Expand Up @@ -50,7 +50,7 @@ abstract class AbstractStructuralChange extends AbstractChange

/**
* @Flow\Inject
* @var NodeService
* @var NeosUiNodeService
*/
protected $nodeService;

Expand Down Expand Up @@ -111,7 +111,7 @@ public function getSiblingNode(): ?Node
}

if ($this->cachedSiblingNode === null) {
$this->cachedSiblingNode = $this->nodeService->getNodeFromContextPath(
$this->cachedSiblingNode = $this->nodeService->findNodeBySerializedNodeAddress(
$this->siblingDomAddress->getContextPath(),
$this->getSubject()->subgraphIdentity->contentRepositoryId
);
Expand Down
2 changes: 1 addition & 1 deletion Classes/Domain/Model/Changes/CopyInto.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getParentNode(): ?Node
{
if (!isset($this->cachedParentNode)) {
$this->cachedParentNode = $this->parentContextPath
? $this->nodeService->getNodeFromContextPath($this->parentContextPath, $this->getSubject()->subgraphIdentity->contentRepositoryId)
? $this->nodeService->findNodeBySerializedNodeAddress($this->parentContextPath, $this->getSubject()->subgraphIdentity->contentRepositoryId)
: null;
}

Expand Down
2 changes: 1 addition & 1 deletion Classes/Domain/Model/Changes/MoveInto.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getParentNode(): ?Node
return null;
}

return $this->nodeService->getNodeFromContextPath(
return $this->nodeService->findNodeBySerializedNodeAddress(
$this->parentContextPath,
$this->getSubject()->subgraphIdentity->contentRepositoryId
);
Expand Down
5 changes: 4 additions & 1 deletion Classes/Domain/Model/Changes/Remove.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
*/

use Neos\ContentRepository\Core\DimensionSpace\Exception\DimensionSpacePointNotFound;
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindClosestNodeFilter;
use Neos\ContentRepository\Core\SharedModel\Exception\ContentStreamDoesNotExistYet;
use Neos\ContentRepository\Core\SharedModel\Node\NodeVariantSelectionStrategy;
use Neos\ContentRepository\Core\Feature\NodeRemoval\Command\RemoveNodeAggregate;
use Neos\ContentRepository\Core\SharedModel\Exception\NodeAggregatesTypeIsAmbiguous;
use Neos\Flow\Annotations as Flow;
use Neos\Neos\Domain\Service\NodeTypeNameFactory;
use Neos\Neos\Fusion\Cache\ContentCacheFlusher;
use Neos\Neos\Ui\Domain\Model\AbstractChange;
use Neos\Neos\Ui\Domain\Model\Feedback\Operations\RemoveNode;
Expand Down Expand Up @@ -68,7 +70,8 @@ public function apply(): void
// otherwise we cannot find the parent nodes anymore.
$this->updateWorkspaceInfo();

$closestDocumentParentNode = $this->findClosestDocumentNode($subject);
$subgraph = $this->contentRepositoryRegistry->subgraphForNode($this->subject);
$closestDocumentParentNode = $subgraph->findClosestNode($this->subject->nodeAggregateId, FindClosestNodeFilter::create(nodeTypeConstraints: NodeTypeNameFactory::NAME_DOCUMENT));
$command = RemoveNodeAggregate::create(
$subject->subgraphIdentity->contentStreamId,
$subject->nodeAggregateId,
Expand Down
16 changes: 9 additions & 7 deletions Classes/Domain/Model/Feedback/Operations/ReloadDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
* source code.
*/

use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindClosestNodeFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Controller\ControllerContext;
use Neos\Neos\Ui\ContentRepository\Service\NodeService;
use Neos\Neos\Domain\Service\NodeTypeNameFactory;
use Neos\Neos\Ui\Domain\Model\AbstractFeedback;
use Neos\Neos\Ui\Domain\Model\FeedbackInterface;
use Neos\Neos\Ui\Fusion\Helper\NodeInfoHelper;
Expand All @@ -23,11 +25,8 @@ class ReloadDocument extends AbstractFeedback
{
protected ?Node $node;

/**
* @Flow\Inject
* @var NodeService
*/
protected $nodeService;
#[Flow\Inject]
protected ContentRepositoryRegistry $contentRepositoryRegistry;

public function getType(): string
{
Expand Down Expand Up @@ -73,7 +72,10 @@ public function serializePayload(ControllerContext $controllerContext): array
}
$nodeInfoHelper = new NodeInfoHelper();

if ($documentNode = $this->nodeService->getClosestDocument($this->node)) {
$documentNode = $this->contentRepositoryRegistry->subgraphForNode($this->node)
->findClosestNode($this->node->nodeAggregateId, FindClosestNodeFilter::create(nodeTypeConstraints: NodeTypeNameFactory::NAME_DOCUMENT));

if ($documentNode) {
return [
'uri' => $nodeInfoHelper->previewUri($documentNode, $controllerContext)
];
Expand Down
4 changes: 2 additions & 2 deletions Classes/Domain/Service/NodeTreeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Controller\ControllerContext;
use Neos\Neos\Service\LinkingService;
use Neos\Neos\Ui\ContentRepository\Service\NodeService;
use Neos\Neos\Ui\ContentRepository\Service\NeosUiNodeService;

class NodeTreeBuilder
{
Expand Down Expand Up @@ -58,7 +58,7 @@ class NodeTreeBuilder

/**
* @Flow\Inject
* @var NodeService
* @var NeosUiNodeService
*/
protected $nodeService;

Expand Down
8 changes: 4 additions & 4 deletions Classes/TypeConverter/ChangeCollectionConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Neos\Flow\Property\PropertyMappingConfigurationInterface;
use Neos\Flow\Property\TypeConverter\AbstractTypeConverter;
use Neos\Flow\Reflection\ReflectionService;
use Neos\Neos\Ui\ContentRepository\Service\NodeService;
use Neos\Neos\Ui\ContentRepository\Service\NeosUiNodeService;
use Neos\Neos\Ui\Domain\Model\ChangeCollection;
use Neos\Neos\Ui\Domain\Model\ChangeInterface;
use Neos\Neos\Ui\Domain\Model\Changes\Property;
Expand Down Expand Up @@ -76,7 +76,7 @@ class ChangeCollectionConverter

/**
* @Flow\Inject
* @var NodeService
* @var NeosUiNodeService
*/
protected $nodeService;

Expand Down Expand Up @@ -141,7 +141,7 @@ protected function convertChangeData(array $changeData, ContentRepositoryId $con


$subjectContextPath = $changeData['subject'];
$subject = $this->nodeService->getNodeFromContextPath($subjectContextPath, $contentRepositoryId);
$subject = $this->nodeService->findNodeBySerializedNodeAddress($subjectContextPath, $contentRepositoryId);
if (is_null($subject)) {
throw new \RuntimeException('Could not find node for subject "' . $subjectContextPath . '"', 1645657340);
}
Expand All @@ -150,7 +150,7 @@ protected function convertChangeData(array $changeData, ContentRepositoryId $con

if (isset($changeData['reference']) && method_exists($changeClassInstance, 'setReference')) {
$referenceContextPath = $changeData['reference'];
$reference = $this->nodeService->getNodeFromContextPath($referenceContextPath, $contentRepositoryId);
$reference = $this->nodeService->findNodeBySerializedNodeAddress($referenceContextPath, $contentRepositoryId);
$changeClassInstance->setReference($reference);
}

Expand Down

0 comments on commit 9dfd8a9

Please sign in to comment.