From a69fed4d7f41736ecc7be3c8f295c28a0f6b994d Mon Sep 17 00:00:00 2001 From: mhsdesign <85400359+mhsdesign@users.noreply.github.com> Date: Sun, 16 Jun 2024 16:30:13 +0200 Subject: [PATCH] TASK: Introduce dedicated `Options::withForceAbsolute` The filter like signatures dont feel quite right because they only have one argument ... and its a yagni to add new args --- .../FrontendRouting/NodeUriBuilder.php | 4 +- Neos.Neos/Classes/FrontendRouting/Options.php | 41 ++++++++++--------- .../Fusion/ConvertUrisImplementation.php | 2 +- .../Classes/Fusion/Helper/LinkHelper.php | 2 +- .../Classes/Fusion/NodeUriImplementation.php | 2 +- .../ViewHelpers/Link/NodeViewHelper.php | 2 +- .../ViewHelpers/Uri/NodeViewHelper.php | 2 +- 7 files changed, 29 insertions(+), 26 deletions(-) diff --git a/Neos.Neos/Classes/FrontendRouting/NodeUriBuilder.php b/Neos.Neos/Classes/FrontendRouting/NodeUriBuilder.php index a56b5b9a874..71fbd46e4d0 100644 --- a/Neos.Neos/Classes/FrontendRouting/NodeUriBuilder.php +++ b/Neos.Neos/Classes/FrontendRouting/NodeUriBuilder.php @@ -94,7 +94,7 @@ public function __construct( * These options will not be considered when building a preview uri {@see previewUriFor} * * - forceAbsolute: - * Absolute urls for non cross-linked nodes can be enforced via {@see Options::$forceAbsolute}. + * Absolute urls for non cross-linked nodes can be enforced via {@see Options::withForceAbsolute()}. * In which case the base uri determined by the request is used as host * instead of a possibly configured site domain's host. * @@ -123,7 +123,7 @@ public function __construct( */ public function uriFor(NodeAddress $nodeAddress, Options $options = null): UriInterface { - $options ??= Options::create(); + $options ??= Options::createEmpty(); if (!$nodeAddress->workspaceName->isLive()) { // we cannot build a human-readable uri using the showAction as diff --git a/Neos.Neos/Classes/FrontendRouting/Options.php b/Neos.Neos/Classes/FrontendRouting/Options.php index d82cd8a1a61..2a7d7a2b87b 100644 --- a/Neos.Neos/Classes/FrontendRouting/Options.php +++ b/Neos.Neos/Classes/FrontendRouting/Options.php @@ -9,7 +9,7 @@ * * Example: * - * Options::create(forceAbsolute: true); + * Options::createForceAbsolute()->withCustomFormat('json'); * * @api for the factory methods; NOT for the inner state. */ @@ -27,32 +27,35 @@ private function __construct( } /** - * Creates an instance with the specified options + * Creates empty options. Chain any of the with* methods to create a new option set with different values. + */ + public static function createEmpty(): self + { + return new self(false, '', []); + } + + /** + * Creates options with option to enforced absolute urls for non cross-linked nodes. + * + * Alias for: + * + * Options::createEmpty()->withForceAbsolute(); * - * Note: The signature of this method might be extended in the future, so it should always be used with named arguments - * @see https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments */ - public static function create( - bool $forceAbsolute = null - ): self { - return new self( - $forceAbsolute ?? false, - '', - [] - ); + public static function createForceAbsolute(): self + { + return new self(true, '', []); } /** - * Returns a new instance with the specified additional options + * Option to enforce absolute urls for non cross-linked nodes. * - * Note: The signature of this method might be extended in the future, so it should always be used with named arguments - * @see https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments + * Absolute urls are fully qualified with protocol and host. */ - public function with( - bool $forceAbsolute = null - ): self { + public function withForceAbsolute(): self + { return new self( - $forceAbsolute ?? $this->forceAbsolute, + true, $this->format, $this->routingArguments ); diff --git a/Neos.Neos/Classes/Fusion/ConvertUrisImplementation.php b/Neos.Neos/Classes/Fusion/ConvertUrisImplementation.php index fc58a77a0e6..fc27ab0b5c9 100644 --- a/Neos.Neos/Classes/Fusion/ConvertUrisImplementation.php +++ b/Neos.Neos/Classes/Fusion/ConvertUrisImplementation.php @@ -142,7 +142,7 @@ public function evaluate() $nodeAddress = NodeAddress::fromNode($node); $unresolvedUris = []; - $options = Options::create(forceAbsolute: $this->fusionValue('absolute')); + $options = $this->fusionValue('absolute') ? Options::createForceAbsolute() : Options::createEmpty(); $possibleRequest = $this->runtime->fusionGlobals->get('request'); if ($possibleRequest instanceof ActionRequest) { diff --git a/Neos.Neos/Classes/Fusion/Helper/LinkHelper.php b/Neos.Neos/Classes/Fusion/Helper/LinkHelper.php index dd04c0d2b65..d6c92994f74 100644 --- a/Neos.Neos/Classes/Fusion/Helper/LinkHelper.php +++ b/Neos.Neos/Classes/Fusion/Helper/LinkHelper.php @@ -110,7 +110,7 @@ public function resolveNodeUri( $nodeUriBuilder = $this->nodeUriBuilderFactory->forActionRequest($controllerContext->getRequest()); - $options = Options::create(); + $options = Options::createEmpty(); $format = $controllerContext->getRequest()->getFormat(); if ($format && $format !== 'html') { $options = $options->withCustomFormat($format); diff --git a/Neos.Neos/Classes/Fusion/NodeUriImplementation.php b/Neos.Neos/Classes/Fusion/NodeUriImplementation.php index a8327fee3db..69ad10218f8 100644 --- a/Neos.Neos/Classes/Fusion/NodeUriImplementation.php +++ b/Neos.Neos/Classes/Fusion/NodeUriImplementation.php @@ -150,7 +150,7 @@ public function evaluate() $nodeUriBuilder = $this->nodeUriBuilderFactory->forActionRequest(ActionRequest::fromHttpRequest(ServerRequest::fromGlobals())); } - $options = Options::create(forceAbsolute: $this->isAbsolute()); + $options = $this->isAbsolute() ? Options::createForceAbsolute() : Options::createEmpty(); $format = $this->getFormat() ?: $possibleRequest->getFormat(); if ($format && $format !== 'html') { $options = $options->withCustomFormat($format); diff --git a/Neos.Neos/Classes/ViewHelpers/Link/NodeViewHelper.php b/Neos.Neos/Classes/ViewHelpers/Link/NodeViewHelper.php index 040a976c51a..d7990646735 100644 --- a/Neos.Neos/Classes/ViewHelpers/Link/NodeViewHelper.php +++ b/Neos.Neos/Classes/ViewHelpers/Link/NodeViewHelper.php @@ -301,7 +301,7 @@ public function render(): string $nodeUriBuilder = $this->nodeUriBuilderFactory->forActionRequest($this->controllerContext->getRequest()); - $options = Options::create(forceAbsolute: $this->arguments['absolute']); + $options = $this->arguments['absolute'] ? Options::createForceAbsolute() : Options::createEmpty(); $format = $this->arguments['format'] ?: $this->controllerContext->getRequest()->getFormat(); if ($format && $format !== 'html') { $options = $options->withCustomFormat($format); diff --git a/Neos.Neos/Classes/ViewHelpers/Uri/NodeViewHelper.php b/Neos.Neos/Classes/ViewHelpers/Uri/NodeViewHelper.php index bdbb7c42d6d..79e7e997a26 100644 --- a/Neos.Neos/Classes/ViewHelpers/Uri/NodeViewHelper.php +++ b/Neos.Neos/Classes/ViewHelpers/Uri/NodeViewHelper.php @@ -213,7 +213,7 @@ public function render(): string $nodeUriBuilder = $this->nodeUriBuilderFactory->forActionRequest($this->controllerContext->getRequest()); - $options = Options::create(forceAbsolute: $this->arguments['absolute']); + $options = $this->arguments['absolute'] ? Options::createForceAbsolute() : Options::createEmpty(); $format = $this->arguments['format'] ?: $this->controllerContext->getRequest()->getFormat(); if ($format && $format !== 'html') { $options = $options->withCustomFormat($format);