Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUGFIX: Restore Neos.Neos:NodeUri, neos:link.node and LinkingService #4886

Merged
merged 25 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f836312
TASK: Introduce `LegacyNodePathNormalizer`
mhsdesign Feb 15, 2024
e21b037
TASK: Introduce `NodeAddressNormalizer::resolveNodeAddressFromPath`
mhsdesign Jul 2, 2024
33c6277
TASK: Simplify `NodeViewHelper`
mhsdesign Jul 2, 2024
509ea9a
BUGFIX: Correctly use `baseNodeName` in `NodeViewHelper`'s
mhsdesign Jul 2, 2024
e1b438c
!!! TASK: Remove dead option `resolveShortcuts` from `neos:uri.node`
mhsdesign Jul 3, 2024
bf04129
BUGFIX: Reintroduce relative node paths for `Neos.Neos:NodeUri`
mhsdesign Jul 3, 2024
7fa5a9f
TASK: Deprecate `LinkingService` and correct its logic
mhsdesign Jul 3, 2024
4040ae5
TASK: Fix `NodeAddressNormalizer` to handle empty paths
mhsdesign Jul 3, 2024
8295c3b
TASK: Improve exceptions
mhsdesign Jul 3, 2024
e7ab19d
TASK: Simplify LinkHelper and deprecate `resolveNodeUri`
mhsdesign Jul 3, 2024
e770476
TASK: Use the `LinkHelper` als b/c layer in the `LinkingService`
mhsdesign Jul 3, 2024
7531271
TASK: Adjust phpstan types
mhsdesign Jul 3, 2024
a191ff1
TASK: Simplify regex of `PATTERN_SUPPORTED_URIS`
mhsdesign Jul 3, 2024
2b2564f
TASK: Document `NodeAddressNormalizer`
mhsdesign Jul 3, 2024
829e47c
Merge remote-tracking branch 'origin/9.0' into bugfix/restoreLinkingS…
mhsdesign Oct 10, 2024
c2d54c5
Merge remote-tracking branch 'origin/9.0' into bugfix/restoreLinkingS…
mhsdesign Oct 10, 2024
b0fd72f
TASK: Add Neos.Neos:NodeUri behat test for Neos9 :D
mhsdesign Oct 10, 2024
881127b
Merge remote-tracking branch 'origin/9.0' into bugfix/restoreLinkingS…
mhsdesign Oct 10, 2024
38f6834
TASK: Revert rename of `Neos.Neos/Tests/Behavior/behat.yml`
mhsdesign Oct 10, 2024
22993c5
TASK: Remove `node://` protocol handling from `NodeAddressNormalizer`
mhsdesign Oct 11, 2024
d3a3013
TASK: Remove absolute nodepath feature from `nearestContentCollection`
mhsdesign Oct 11, 2024
774d489
TASK: Adjust doc comment
mhsdesign Oct 11, 2024
418bc0e
TASK: Rename `NodeAddressNormalizer` to `NodePathResolver`
mhsdesign Oct 14, 2024
c0f424a
TASK: Adjust deprecation notice
mhsdesign Oct 14, 2024
dc4df57
TASK: Fix hidden state evaluation in LinkingService and document new …
mhsdesign Oct 14, 2024
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
4 changes: 2 additions & 2 deletions .composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@test:behat-cli -c Neos.ContentRepository.Export/Tests/Behavior/behat.yml.dist",
"@test:behat-cli -c Neos.TimeableNodeVisibility/Tests/Behavior/behat.yml.dist",
"../../flow doctrine:migrate --quiet; ../../flow cr:setup",
"@test:behat-cli -c Neos.Neos/Tests/Behavior/behat.yml"
"@test:behat-cli -c Neos.Neos/Tests/Behavior/behat.yml.dist"
],
"test:behavioral:stop-on-failure": [
"@test:behat-cli -vvv --stop-on-failure -c Neos.ContentRepository.BehavioralTests/Tests/Behavior/behat.yml.dist",
Expand All @@ -47,7 +47,7 @@
"@test:behat-cli -vvv --stop-on-failure -c Neos.ContentRepository.Export/Tests/Behavior/behat.yml.dist",
"@test:behat-cli -vvv --stop-on-failure -c Neos.TimeableNodeVisibility/Tests/Behavior/behat.yml.dist",
"../../flow doctrine:migrate --quiet; ../../flow cr:setup",
"@test:behat-cli -vvv --stop-on-failure -c Neos.Neos/Tests/Behavior/behat.yml"
"@test:behat-cli -vvv --stop-on-failure -c Neos.Neos/Tests/Behavior/behat.yml.dist"
],
"test": [
"@test:unit",
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/postgresql-versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,5 @@ jobs:
FLOW_CONTEXT=Testing/Behat ./flow behat:setup
FLOW_CONTEXT=Testing/Behat ./flow doctrine:create
FLOW_CONTEXT=Testing/Behat ./flow doctrine:migrationversion --add --version all
bin/behat --stop-on-failure -f progress -c Packages/Neos/Neos.Neos/Tests/Behavior/behat.yml
bin/behat --stop-on-failure -f progress -c Packages/Neos/Neos.Neos/Tests/Behavior/behat.yml.dist
bin/behat --stop-on-failure -f progress -c Packages/Neos/Neos.ContentRepository/Tests/Behavior/behat.yml.dist
1 change: 1 addition & 0 deletions Neos.Neos/Classes/FrontendRouting/NodeUriBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public function __construct(
* @api
* @throws NoMatchingRouteException
* The exception is thrown for various unlike cases in which uri building fails:
* - the node is disabled in the live workspace (a preview url should be built instead)
* - the default route definitions are misconfigured
* - the custom uri building options don't macht a route
* - the shortcut points to an invalid target
Expand Down
3 changes: 1 addition & 2 deletions Neos.Neos/Classes/Fusion/ConvertUrisImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@
*/
class ConvertUrisImplementation extends AbstractFusionObject
{
public const PATTERN_SUPPORTED_URIS
= '/(node|asset):\/\/([a-z0-9\-]+|([a-f0-9]){8}-([a-f0-9]){4}-([a-f0-9]){4}-([a-f0-9]){4}-([a-f0-9]){12})/';
private const PATTERN_SUPPORTED_URIS = '/(node|asset):\/\/([a-z0-9\-]+)/';
kitsunet marked this conversation as resolved.
Show resolved Hide resolved

/**
* @Flow\Inject
Expand Down
136 changes: 53 additions & 83 deletions Neos.Neos/Classes/Fusion/Helper/LinkHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,24 @@

use GuzzleHttp\Psr7\Uri;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAddress;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Flow\Mvc\Controller\ControllerContext;
use Neos\Flow\Mvc\Exception\NoMatchingRouteException;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Repository\AssetRepository;
use Neos\Neos\FrontendRouting\NodeUriBuilderFactory;
use Neos\Neos\FrontendRouting\Options;
use Neos\Neos\Fusion\ConvertUrisImplementation;
use Neos\Neos\Service\LinkingService;
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;

class LinkHelper implements ProtectedContextAwareInterface
{
private const NODE_SCHEME = 'node';
private const ASSET_SCHEME = 'asset';

/**
* @Flow\Inject
* @var LoggerInterface
Expand Down Expand Up @@ -66,77 +65,55 @@ class LinkHelper implements ProtectedContextAwareInterface
protected $nodeUriBuilderFactory;

/**
* @param string|Uri $uri
* @return boolean
* @Flow\Inject
* @var LinkingService
*/
public function hasSupportedScheme($uri): bool
{
return in_array($this->getScheme($uri), ['node', 'asset'], true);
}
protected $linkingService;

/**
* @param string|UriInterface $uri
* @return string
*/
public function getScheme($uri): string
public function hasSupportedScheme(string|UriInterface|null $uri): bool
{
if ($uri instanceof UriInterface) {
return $uri->getScheme();
}

if (is_string($uri) && preg_match(ConvertUrisImplementation::PATTERN_SUPPORTED_URIS, $uri, $matches) === 1) {
return $matches[1];
}

return '';
$scheme = $this->getScheme($uri);
return $scheme === self::NODE_SCHEME || $scheme === self::ASSET_SCHEME;
}

public function resolveNodeUri(
string|Uri $uri,
Node $contextNode,
ControllerContext $controllerContext
): ?string {
$targetNode = $this->convertUriToObject($uri, $contextNode);
if (!$targetNode instanceof Node) {
$this->systemLogger->info(
sprintf(
'Could not resolve "%s" to an existing node; The node was probably deleted.',
$uri
),
LogEnvironment::fromMethodName(__METHOD__)
);
public function getScheme(string|UriInterface|null $uri): ?string
{
if ($uri === null || $uri === '') {
return null;
}

$nodeUriBuilder = $this->nodeUriBuilderFactory->forActionRequest($controllerContext->getRequest());

$options = Options::createEmpty();
$format = $controllerContext->getRequest()->getFormat();
if ($format && $format !== 'html') {
$options = $options->withCustomFormat($format);
}
try {
$targetUri = $nodeUriBuilder->uriFor(NodeAddress::fromNode($targetNode), $options);
} catch (NoMatchingRouteException $e) {
$this->systemLogger->info(sprintf(
'Failed to build URI for node "%s": %e',
$targetNode->aggregateId->value,
$e->getMessage()
), LogEnvironment::fromMethodName(__METHOD__));
return null;
if (is_string($uri)) {
$uri = new Uri($uri);
}
return $uri->getScheme();
}

return (string)$targetUri;
/**
* @param string|UriInterface $uri
* @param Node $contextNode
* @param ControllerContext $controllerContext
* @return string|null
* @deprecated with Neos 9 as the linking service is deprecated and this helper cannot be invoked from Fusion either way as the $controllerContext is not available.
*/
public function resolveNodeUri(string|UriInterface $uri, Node $contextNode, ControllerContext $controllerContext): ?string
{
return $this->linkingService->resolveNodeUri((string)$uri, $contextNode, $controllerContext);
}

public function resolveAssetUri(string|Uri $uri): string
public function resolveAssetUri(string|UriInterface $uri): string
{
if (!$uri instanceof UriInterface) {
$uri = new Uri($uri);
}
if ($uri->getScheme() !== self::ASSET_SCHEME) {
throw new \RuntimeException(sprintf(
'Invalid asset uri "%s" provided. It must start with asset://',
$uri
), 1720003716);
}

$asset = $this->assetRepository->findByIdentifier($uri->getHost());
if (!$asset instanceof AssetInterface) {
throw new \InvalidArgumentException(sprintf(
throw new \RuntimeException(sprintf(
'Failed to resolve asset from URI "%s", probably the corresponding asset was deleted',
$uri
), 1601373937);
Expand All @@ -148,33 +125,26 @@ public function resolveAssetUri(string|Uri $uri): string
}

public function convertUriToObject(
string|Uri $uri,
string|UriInterface $uri,
Node $contextNode = null
): Node|AssetInterface|null {
if (empty($uri)) {
return null;
}
if ($uri instanceof UriInterface) {
$uri = (string)$uri;
if (is_string($uri)) {
$uri = new Uri($uri);
}

if (preg_match(ConvertUrisImplementation::PATTERN_SUPPORTED_URIS, $uri, $matches) === 1) {
switch ($matches[1]) {
case 'node':
if ($contextNode === null) {
throw new \RuntimeException(
'node:// URI conversion requires a context node to be passed',
1409734235
);
}
return $this->contentRepositoryRegistry->subgraphForNode($contextNode)
->findNodeById(NodeAggregateId::fromString($matches[2]));
case 'asset':
/** @var AssetInterface|null $asset */
/** @noinspection OneTimeUseVariablesInspection */
$asset = $this->assetRepository->findByIdentifier($matches[2]);
return $asset;
}
switch ($uri->getScheme()) {
case self::NODE_SCHEME:
if ($contextNode === null) {
throw new \RuntimeException(
sprintf('node:// URI conversion like "%s" requires a context node to be passed', $uri),
1409734235
);
}
return $this->contentRepositoryRegistry->subgraphForNode($contextNode)
->findNodeById(NodeAggregateId::fromString($uri->getHost()));
case self::ASSET_SCHEME:
/** @var AssetInterface|null $asset */
$asset = $this->assetRepository->findByIdentifier($uri->getHost());
return $asset;
}
return null;
}
Expand Down
8 changes: 2 additions & 6 deletions Neos.Neos/Classes/Fusion/Helper/NodeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,10 @@ public function nearestContentCollection(Node $node, string $nodePath): Node
$contentCollectionType
), 1409300545);
}
$nodePath = AbsoluteNodePath::patternIsMatchedByString($nodePath)
? AbsoluteNodePath::fromString($nodePath)
: NodePath::fromString($nodePath);
$nodePath = NodePath::fromString($nodePath);
$subgraph = $this->contentRepositoryRegistry->subgraphForNode($node);

$subNode = $nodePath instanceof AbsoluteNodePath
? $subgraph->findNodeByAbsolutePath($nodePath)
: $subgraph->findNodeByPath($nodePath, $node->aggregateId);
$subNode = $subgraph->findNodeByPath($nodePath, $node->aggregateId);

if ($subNode !== null && $this->isOfType($subNode, $contentCollectionType)) {
return $subNode;
Expand Down
87 changes: 56 additions & 31 deletions Neos.Neos/Classes/Fusion/NodeUriImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,30 @@
use Neos\Fusion\FusionObjects\AbstractFusionObject;
use Neos\Neos\FrontendRouting\NodeUriBuilderFactory;
use Neos\Neos\FrontendRouting\Options;
use Neos\Neos\Utility\LegacyNodePathNormalizer;
use Neos\Neos\Utility\NodePathResolver;
use Psr\Log\LoggerInterface;

/**
* Create a link to a node
*
* If the node is passed as string the base node is required.
* Following string syntax is allowed:
*
* - /<Neos.Neos:Sites>/my-site/main
* - some/relative/path
*
* Deprecated syntax:
*
* - /sites/site/absolute/path
* - ~/site-relative/path
* - ~
*
* Not supported syntax:
*
* - ./neos/info
* - ../foo/../../bar
*
*/
class NodeUriImplementation extends AbstractFusionObject
{
Expand All @@ -51,12 +71,16 @@ class NodeUriImplementation extends AbstractFusionObject
protected $nodeUriBuilderFactory;

/**
* A node object or a string node path or NULL to resolve the current document node
* @Flow\Inject
* @var NodePathResolver
*/
public function getNode(): Node|string|null
{
return $this->fusionValue('node');
}
protected $nodeAddressNormalizer;

/**
* @Flow\Inject
* @var LegacyNodePathNormalizer
*/
protected $legacyNodePathNormalizer;

/**
* The requested format, for example "html"
Expand Down Expand Up @@ -103,42 +127,43 @@ public function isAbsolute()
*
* @return string
*/
public function getBaseNodeName()
public function getBaseNodeName(): string
{
return $this->fusionValue('baseNodeName');
return $this->fusionValue('baseNodeName') ?: 'documentNode';
}

/**
* Render the Uri.
*
* @return string The rendered URI or NULL if no URI could be resolved for the given node
* @throws \Neos\Flow\Mvc\Routing\Exception\MissingActionNameException
*/
public function evaluate()
{
$baseNode = null;
$baseNodeName = $this->getBaseNodeName() ?: 'documentNode';
$currentContext = $this->runtime->getCurrentContext();
if (isset($currentContext[$baseNodeName])) {
$baseNode = $currentContext[$baseNodeName];
$node = $this->fusionValue('node');
if (is_string($node)) {
$currentContext = $this->runtime->getCurrentContext();
$baseNode = $currentContext[$this->getBaseNodeName()] ?? null;
if (!$baseNode instanceof Node) {
throw new \RuntimeException(sprintf(
'If "node" is passed as string a base node in must be set in "%s". Given: %s',
$this->getBaseNodeName(),
get_debug_type($baseNode)
), 1719996392);
}

$possibleAbsoluteNodePath = $this->legacyNodePathNormalizer->tryResolveLegacyPathSyntaxToAbsoluteNodePath($node, $baseNode);
$nodeAddress = $this->nodeAddressNormalizer->resolveNodeAddressByPath(
$possibleAbsoluteNodePath ?? $node,
$baseNode
);
} elseif ($node instanceof Node) {
$nodeAddress = NodeAddress::fromNode($node);
} else {
throw new \RuntimeException(sprintf('Could not find a node instance in Fusion context with name "%s" and no node instance was given to the node argument. Set a node instance in the Fusion context or pass a node object to resolve the URI.', $baseNodeName), 1373100400);
}
$node = $this->getNode();
if (!$node instanceof Node) {
throw new \RuntimeException(sprintf('Passing node as %s is not supported yet.', get_debug_type($node)));
throw new \RuntimeException(sprintf(
'The "node" argument can only be a string or an instance of `Node`. Given: %s',
get_debug_type($node)
), 1719996456);
}
/* TODO implement us see https://github.com/neos/neos-development-collection/issues/4524 {@see \Neos\Neos\ViewHelpers\Uri\NodeViewHelper::resolveNodeAddressFromString} for an example implementation
elseif ($node === '~') {
$nodeAddress = $this->nodeAddressFactory->createFromNode($node);
$nodeAddress = $nodeAddress->withNodeAggregateId(
$siteNode->nodeAggregateId
);
} elseif (is_string($node) && substr($node, 0, 7) === 'node://') {
$nodeAddress = $this->nodeAddressFactory->createFromNode($node);
$nodeAddress = $nodeAddress->withNodeAggregateId(
NodeAggregateId::fromString(\mb_substr($node, 7))
);*/

$possibleRequest = $this->runtime->fusionGlobals->get('request');
if ($possibleRequest instanceof ActionRequest) {
Expand All @@ -160,10 +185,10 @@ public function evaluate()
}

try {
$resolvedUri = $nodeUriBuilder->uriFor(NodeAddress::fromNode($node), $options);
$resolvedUri = $nodeUriBuilder->uriFor($nodeAddress, $options);
} catch (NoMatchingRouteException) {
// todo log arguments?
$this->systemLogger->warning(sprintf('Could not resolve "%s" to a node uri.', $node->aggregateId->value), LogEnvironment::fromMethodName(__METHOD__));
$this->systemLogger->warning(sprintf('Could not resolve "%s" to a node uri.', $nodeAddress->aggregateId->value), LogEnvironment::fromMethodName(__METHOD__));
return '';
}

Expand Down
Loading
Loading