-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\ContentRepositoryRegistry; | ||
|
||
use Neos\ContentRepository\Core\Projection\ContentGraph\Node; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\VisibilityConstraints; | ||
use Neos\ContentRepository\Core\SharedModel\Node\NodeIdentity; | ||
use Neos\Flow\Annotations as Flow; | ||
|
||
/** | ||
* Utility to convert the {@see Node} to its {@see NodeIdentity} and reverse. | ||
* | ||
* @api | ||
*/ | ||
#[Flow\Scope('singleton')] | ||
final readonly class NodeSerializer | ||
{ | ||
public function __construct( | ||
private ContentRepositoryRegistry $contentRepositoryRegistry | ||
) { | ||
} | ||
|
||
public function denormalizeNodeFromIdentity(NodeIdentity $identity): Node | ||
{ | ||
$contentRepository = $this->contentRepositoryRegistry->get($identity->contentRepositoryId); | ||
$workspace = $contentRepository->getWorkspaceFinder()->findOneByName($identity->workspaceName); | ||
if (!$workspace) { | ||
throw new \RuntimeException(sprintf('Workspace could not be found while deserializing node NodeIdentity<%s>.', json_encode($identity, JSON_PARTIAL_OUTPUT_ON_ERROR)), 1707757488); | ||
} | ||
$subgraph = $contentRepository->getContentGraph()->getSubgraph( | ||
$workspace->currentContentStreamId, | ||
$identity->dimensionSpacePoint, | ||
// todo policy? Or what to prevent from accidentally showing unwanted nodes. | ||
VisibilityConstraints::withoutRestrictions() | ||
); | ||
$node = $subgraph->findNodeById($identity->nodeAggregateId); | ||
if (!$node) { | ||
throw new \RuntimeException(sprintf('NodeAggregateId could not be found while deserializing node NodeIdentity<%s>.', json_encode($identity, JSON_PARTIAL_OUTPUT_ON_ERROR)), 1707772263); | ||
} | ||
return $node; | ||
} | ||
|
||
public function normalizeNodeToIdentity(Node $node): NodeIdentity | ||
{ | ||
$contentRepository = $this->contentRepositoryRegistry->get($node->subgraphIdentity->contentRepositoryId); | ||
$workspace = $contentRepository->getWorkspaceFinder()->findOneByCurrentContentStreamId($node->subgraphIdentity->contentStreamId); | ||
if (!$workspace) { | ||
throw new \RuntimeException(sprintf('Workspace could not be found for current content stream %s.', $node->subgraphIdentity->contentStreamId->value), 1707757787); | ||
} | ||
return NodeIdentity::create( | ||
$node->subgraphIdentity->contentRepositoryId, | ||
$workspace->workspaceName, | ||
$node->subgraphIdentity->dimensionSpacePoint, | ||
$node->nodeAggregateId | ||
); | ||
} | ||
} |