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

Add resolutionHost option #3

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*.DS_Store
.idea/
Documentation/*
!Documentation/*.rst

.idea/
Empty file.
Empty file.
Empty file.
14 changes: 12 additions & 2 deletions Classes/Http/ContentDimensionDetection/SubdomainDimensionPresetDetector.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,25 @@ final class SubdomainDimensionPresetDetector implements ContentDimensionPresetDe
public function detectPreset(string $dimensionName, array $presets, ServerRequestInterface $request, array $overrideOptions = null)
{
$host = $request->getUri()->getHost();

foreach ($presets as $availablePreset) {
if (empty($availablePreset['resolutionValue'])) {
// we leave the decision about how to handle empty values to the detection component
continue;
}

$valueLength = mb_strlen($availablePreset['resolutionValue']);
$value = mb_substr($host, 0, $valueLength);

if (mb_substr($host, 0, $valueLength) === $availablePreset['resolutionValue']) {
return $availablePreset;
if ($value === $availablePreset['resolutionValue']) {
if (array_key_exists('resolutionHost', $availablePreset)) {
$domain = mb_substr($host, $valueLength+1);
if ($domain === $availablePreset['resolutionHost']) {
return $availablePreset;
}
} else {
return $availablePreset;
}
}
}

Expand Down
13 changes: 10 additions & 3 deletions Classes/Http/ContentDimensionDetection/TopLevelDomainDimensionPresetDetector.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Flowpack\Neos\DimensionResolver\Http\ContentDimensionDetection;

/*
Expand Down Expand Up @@ -35,10 +36,16 @@ public function detectPreset(string $dimensionName, array $presets, ServerReques
$host = $request->getUri()->getHost();
$hostLength = mb_strlen($host);
foreach ($presets as $preset) {
$pivot = $hostLength - mb_strlen($preset['resolutionValue']);
if (array_key_exists('resolutionHost', $preset)) {
if ($host === $preset['resolutionHost']) {
return $preset;
}
} else {
$pivot = $hostLength - mb_strlen($preset['resolutionValue']);

if (mb_substr($host, $pivot) === $preset['resolutionValue']) {
return $preset;
if (mb_substr($host, $pivot) === $preset['resolutionValue']) {
return $preset;
}
}
}

Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
6 changes: 5 additions & 1 deletion Classes/Http/ContentDimensionLinking/SubdomainDimensionPresetLinkProcessor.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public function processUriConstraints(Routing\Dto\UriConstraints $uriConstraints
$prefixesToBeReplaced[] = $availablePreset['resolutionValue'] . '.';
}
}
return $uriConstraints->withHostPrefix($preset['resolutionValue'] ? $preset['resolutionValue'] . '.' : '', $prefixesToBeReplaced);

if (array_key_exists('resolutionHost', $preset))
return $uriConstraints->withHost($preset['resolutionValue'].'.'.$preset['resolutionHost']);
else
return $uriConstraints->withHostPrefix($preset['resolutionValue'] ? $preset['resolutionValue'] . '.' : '', $prefixesToBeReplaced);
}
}
5 changes: 4 additions & 1 deletion Classes/Http/ContentDimensionLinking/TopLevelDomainDimensionPresetLinkProcessor.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function processUriConstraints(
$hostSuffixesToBeReplaced[] = '.' . $availablePreset['resolutionValue'];
}

return $uriConstraints->withHostSuffix('.' . $preset['resolutionValue'], $hostSuffixesToBeReplaced);
if (array_key_exists('resolutionHost', $preset))
return $uriConstraints->withHost($preset['resolutionHost']);
else
return $uriConstraints->withHostSuffix('.' . $preset['resolutionValue'], $hostSuffixesToBeReplaced);
}
}
6 changes: 4 additions & 2 deletions Classes/Http/ContentDimensionLinking/UriPathSegmentDimensionPresetLinkProcessor.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public function processUriConstraints(
$options = $overrideOptions ? Arrays::arrayMergeRecursiveOverrule($this->defaultOptions, $overrideOptions) : $this->defaultOptions;
$pathSegmentPart = $options['offset'] > 0 ? $options['delimiter'] : '';
$pathSegmentPart .= ($preset['resolutionValue'] ?? $preset['uriSegment']);

return $uriConstraints->withPathPrefix($pathSegmentPart, true);
if (array_key_exists('resolutionHost', $preset))
return $uriConstraints->withPathPrefix($pathSegmentPart, true)->withHost($preset['resolutionHost']);
else
return $uriConstraints->withPathPrefix($pathSegmentPart, true);
}
}
Empty file modified Classes/Http/ContentDimensionResolutionMode.php
100644 → 100755
Empty file.
Empty file modified Classes/Http/ContentSubgraphUriProcessor.php
100644 → 100755
Empty file.
Empty file modified Classes/Http/ContentSubgraphUriProcessorInterface.php
100644 → 100755
Empty file.
7 changes: 4 additions & 3 deletions Classes/Http/DetectContentSubgraphMiddleware.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$uriPathSegmentUsed = false;
$dimensionValues = $this->detectDimensionSpacePoint($request, $uriPathSegmentUsed);
$workspaceName = $this->detectContentStream($request);

$existingParameters = $request->getAttribute(ServerRequestAttributes::ROUTING_PARAMETERS);
if ($existingParameters === null) {
$existingParameters = RouteParameters::createEmpty();
Expand All @@ -81,7 +80,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$parameters = $existingParameters
->withParameter('dimensionValues', json_encode($dimensionValues))
->withParameter('workspaceName', $workspaceName)
->withParameter('uriPathSegmentUsed', $uriPathSegmentUsed);
->withParameter('uriPathSegmentUsed', $uriPathSegmentUsed)
->withParameter('requestUriHost', $request->getUri()->getHost());


$request = $request->withAttribute(ServerRequestAttributes::ROUTING_PARAMETERS, $parameters);
return $next->handle($request);
Expand Down Expand Up @@ -127,7 +128,7 @@ protected function detectDimensionSpacePoint(ServerRequestInterface $request, bo

$resolutionMode = $presetConfiguration['resolution']['mode'] ?? ContentDimensionResolutionMode::RESOLUTION_MODE_URIPATHSEGMENT;
if ($resolutionMode === ContentDimensionResolutionMode::RESOLUTION_MODE_URIPATHSEGMENT) {
$options['delimiter'] = $this->uriPathSegmentDelimiter;
if (!empty($this->uriPathSegmentDelimiter)) $options['delimiter'] = $this->uriPathSegmentDelimiter;
}
$preset = $detector->detectPreset($dimensionName, $presetConfiguration['presets'], $request, $options);
if ($preset && $resolutionMode === ContentDimensionResolutionMode::RESOLUTION_MODE_URIPATHSEGMENT) {
Expand Down
Empty file.
Empty file.
41 changes: 36 additions & 5 deletions Classes/Routing/FrontendNodeRoutePartHandler.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ protected function matchValue($requestPath)

$tagArray = [$contentContext->getWorkspace()->getName(), $node->getIdentifier()];
$parent = $node->getParent();

while ($parent) {
$tagArray[] = $parent->getIdentifier();
$parent = $parent->getParent();
Expand Down Expand Up @@ -197,8 +198,7 @@ protected function convertRequestPathToNode($requestPath)
if ($requestPathWithoutContext === '') {
$node = $siteNode;
} else {
$relativeNodePath = $this->getRelativeNodePathByUriPathSegmentProperties($siteNode, $requestPathWithoutContext);
$node = ($relativeNodePath !== false) ? $siteNode->getNode($relativeNodePath) : null;
$node = $this->getNodeFromRequestPath($siteNode, $requestPathWithoutContext) ?? null;
Comment on lines -200 to +201
Copy link
Member

@kdambekalns kdambekalns Aug 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will conflict with #11 I think. Just pointing this out already…

}

if (!$node instanceof NodeInterface) {
Expand Down Expand Up @@ -409,8 +409,7 @@ protected function resolveRoutePathForNode(NodeInterface $node)
$nodeContextPathSuffix = ($workspaceName !== 'live') ? substr($nodeContextPath, strpos($nodeContextPath, '@')) : '';

$requestPath = $this->getRequestPathByNode($node);

return trim($requestPath, '/') . $nodeContextPathSuffix;
return $requestPath . $nodeContextPathSuffix;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

}

/**
Expand All @@ -422,6 +421,7 @@ protected function resolveRoutePathForNode(NodeInterface $node)
*
* @param NodeInterface $siteNode The site node, used as a starting point while traversing the tree
* @param string $relativeRequestPath The request path, relative to the site's root path
* @deprecated Use getNodeFromRequestPath() - return only the node
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

* @return string
* @throws \Neos\ContentRepository\Exception\NodeException
*/
Expand All @@ -448,6 +448,37 @@ protected function getRelativeNodePathByUriPathSegmentProperties(NodeInterface $
return implode('/', $relativeNodePathSegments);
}

/**
* Return Node from RequestPath
*
* @param NodeInterface $siteNode The site node, used as a starting point while traversing the tree
* @param string $relativeRequestPath The request path, relative to the site's root path
* @return NodeInterface
* @throws \Neos\ContentRepository\Exception\NodeException
*/
protected function getNodeFromRequestPath(NodeInterface $siteNode, $relativeRequestPath)
{
$matchedNode = false;
$node = $siteNode;

foreach (explode('/', $relativeRequestPath) as $pathSegment) {
$foundNodeInThisSegment = false;
foreach ($node->getChildNodes('Neos.Neos:Document') as $node) {
/** @var NodeInterface $node */
if ($node->getProperty('uriPathSegment') === $pathSegment) {
$matchedNode = $node;
$foundNodeInThisSegment = true;
break;
}
}
if (!$foundNodeInThisSegment) {
return false;
}
}

return $matchedNode;
}

/**
* Renders a request path based on the "uriPathSegment" properties of the nodes leading to the given node.
*
Expand Down Expand Up @@ -482,6 +513,6 @@ protected function getRequestPathByNode(NodeInterface $node)
$currentNode = $currentNode->getParent();
}

return implode('/', array_reverse($requestPathSegments));
return '/' . implode('/', array_reverse($requestPathSegments));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

}
}
Loading