Skip to content

Commit

Permalink
TASK: Introduce dedicated Options::withForceAbsolute
Browse files Browse the repository at this point in the history
The filter like signatures dont feel quite right because they only have one argument ... and its a yagni to add new args
  • Loading branch information
mhsdesign committed Jun 16, 2024
1 parent 95fa4cb commit a69fed4
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Neos.Neos/Classes/FrontendRouting/NodeUriBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down
41 changes: 22 additions & 19 deletions Neos.Neos/Classes/FrontendRouting/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* Example:
*
* Options::create(forceAbsolute: true);
* Options::createForceAbsolute()->withCustomFormat('json');
*
* @api for the factory methods; NOT for the inner state.
*/
Expand All @@ -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
);
Expand Down
2 changes: 1 addition & 1 deletion Neos.Neos/Classes/Fusion/ConvertUrisImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Neos.Neos/Classes/Fusion/Helper/LinkHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Neos.Neos/Classes/Fusion/NodeUriImplementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Neos.Neos/Classes/ViewHelpers/Link/NodeViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Neos.Neos/Classes/ViewHelpers/Uri/NodeViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a69fed4

Please sign in to comment.