Skip to content

Commit

Permalink
Merge pull request #8 from MarcoPNS/only-neos-7-update
Browse files Browse the repository at this point in the history
Switched to PSR-15 middleware for Neos 7.0
  • Loading branch information
albe authored Mar 30, 2021
2 parents 275e037 + 5450423 commit b3f8e4f
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 63 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Documentation/*
!Documentation/*.rst

.idea/
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

use Neos\ContentRepository\Domain\Utility\NodePaths;
use Neos\Flow\Http;
use Psr\Http\Message\ServerRequestInterface;

/**
* Backend URI based dimension preset detector
Expand All @@ -27,13 +27,13 @@ final class BackendUriDimensionPresetDetector implements ContentDimensionPresetD
/**
* @param string $dimensionName
* @param array $presets
* @param Http\Component\ComponentContext $componentContext
* @param ServerRequestInterface $request
* @param array|null $overrideOptions
* @return array|null
*/
public function detectPreset(string $dimensionName, array $presets, Http\Component\ComponentContext $componentContext, array $overrideOptions = null)
public function detectPreset(string $dimensionName, array $presets, ServerRequestInterface $request, array $overrideOptions = null)
{
$path = $componentContext->getHttpRequest()->getUri()->getPath();
$path = $request->getUri()->getPath();
$path = '/' . mb_substr($path, mb_strpos($path, '@'));
if (mb_strpos($path, '.') !== false) {
$path = mb_substr($path, 0, mb_strrpos($path, '.'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* source code.
*/

use Neos\Flow\Http;
use Psr\Http\Message\ServerRequestInterface;

/**
* Interface to detect the current request's dimension preset
Expand All @@ -31,9 +31,9 @@ interface ContentDimensionPresetDetectorInterface
*
* @param string $dimensionName
* @param array $presets
* @param Http\Component\ComponentContext $componentContext
* @param ServerRequestInterface $request
* @param array $overrideOptions
* @return array|null
*/
public function detectPreset(string $dimensionName, array $presets, Http\Component\ComponentContext $componentContext, array $overrideOptions = null);
public function detectPreset(string $dimensionName, array $presets, ServerRequestInterface $request, array $overrideOptions = null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* source code.
*/

use Neos\Flow\Http;
use Psr\Http\Message\ServerRequestInterface;

/**
* Subdomain based dimension preset detector
Expand All @@ -26,13 +26,13 @@ final class SubdomainDimensionPresetDetector implements ContentDimensionPresetDe
/**
* @param string $dimensionName
* @param array $presets
* @param Http\Component\ComponentContext $componentContext
* @param ServerRequestInterface $request
* @param array|null $overrideOptions
* @return array|null
*/
public function detectPreset(string $dimensionName, array $presets, Http\Component\ComponentContext $componentContext, array $overrideOptions = null)
public function detectPreset(string $dimensionName, array $presets, ServerRequestInterface $request, array $overrideOptions = null)
{
$host = $componentContext->getHttpRequest()->getUri()->getHost();
$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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* source code.
*/

use Neos\Flow\Http;
use Psr\Http\Message\ServerRequestInterface;

/**
* Top level domain based dimension preset detector
Expand All @@ -26,13 +26,13 @@ final class TopLevelDomainDimensionPresetDetector implements ContentDimensionPre
/**
* @param string $dimensionName
* @param array $presets
* @param Http\Component\ComponentContext $componentContext
* @param ServerRequestInterface $request
* @param array|null $overrideOptions
* @return array|null
*/
public function detectPreset(string $dimensionName, array $presets, Http\Component\ComponentContext $componentContext, array $overrideOptions = null)
public function detectPreset(string $dimensionName, array $presets, ServerRequestInterface $request, array $overrideOptions = null)
{
$host = $componentContext->getHttpRequest()->getUri()->getHost();
$host = $request->getUri()->getHost();
$hostLength = mb_strlen($host);
foreach ($presets as $preset) {
$pivot = $hostLength - mb_strlen($preset['resolutionValue']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
* source code.
*/

use Neos\Flow\Http;
use Neos\Utility\Arrays;
use Psr\Http\Message\ServerRequestInterface;

/**
* URI path segment based dimension preset detector
Expand All @@ -30,14 +30,14 @@ final class UriPathSegmentDimensionPresetDetector implements ContentDimensionPre
/**
* @param string $dimensionName
* @param array $presets
* @param Http\Component\ComponentContext $componentContext
* @param ServerRequestInterface $request
* @param array|null $overrideOptions
* @return array|null
*/
public function detectPreset(string $dimensionName, array $presets, Http\Component\ComponentContext $componentContext, array $overrideOptions = null)
public function detectPreset(string $dimensionName, array $presets, ServerRequestInterface $request, array $overrideOptions = null)
{
$options = $overrideOptions ? Arrays::arrayMergeRecursiveOverrule($this->defaultOptions, $overrideOptions) : $this->defaultOptions;
$requestPath = $componentContext->getHttpRequest()->getUri()->getPath();
$requestPath = $request->getUri()->getPath();

if (!empty($requestPath) && $requestPath !== '/' && mb_strpos($requestPath, '/') !== false) {
$pathSegments = explode('/', ($requestPath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public function processUriConstraints(
$pathSegmentPart = $options['offset'] > 0 ? $options['delimiter'] : '';
$pathSegmentPart .= ($preset['resolutionValue'] ?? $preset['uriSegment']);

return $uriConstraints->withPathPrefix($pathSegmentPart . '/', true);
return $uriConstraints->withPathPrefix($pathSegmentPart, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface;
use Neos\ContentRepository\Domain\Utility\NodePaths;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Http;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

use Neos\Flow\Mvc\Routing\Dto\RouteParameters;
use Neos\Flow\Mvc\Routing\RoutingComponent;
use Neos\Flow\Http\ServerRequestAttributes;

use Flowpack\Neos\DimensionResolver\Http\ContentDimensionDetection\DimensionPresetDetectorResolver;

/**
* The HTTP component for detecting the requested dimension space point
*/
final class DetectContentSubgraphComponent implements Http\Component\ComponentInterface
final class DetectContentSubgraphMiddleware implements MiddlewareInterface
{
/**
* @Flow\Inject
Expand Down Expand Up @@ -56,33 +62,42 @@ final class DetectContentSubgraphComponent implements Http\Component\ComponentIn
protected $uriPathSegmentDelimiter;

/**
* @param Http\Component\ComponentContext $componentContext
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $next
* @throws Exception\InvalidDimensionPresetDetectorException
* @return ResponseInterface
*/
public function handle(Http\Component\ComponentContext $componentContext)
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
{
$uriPathSegmentUsed = false;
$dimensionValues = $this->detectDimensionSpacePoint($componentContext, $uriPathSegmentUsed);
$workspaceName = $this->detectContentStream($componentContext);
$dimensionValues = $this->detectDimensionSpacePoint($request, $uriPathSegmentUsed);
$workspaceName = $this->detectContentStream($request);

$existingParameters = $request->getAttribute(ServerRequestAttributes::ROUTING_PARAMETERS);
if ($existingParameters === null) {
$existingParameters = RouteParameters::createEmpty();
}

$existingParameters = $componentContext->getParameter(RoutingComponent::class, 'parameters') ?? RouteParameters::createEmpty();
$parameters = $existingParameters
->withParameter('dimensionValues', json_encode($dimensionValues))
->withParameter('workspaceName', $workspaceName)
->withParameter('uriPathSegmentUsed', $uriPathSegmentUsed);
$componentContext->setParameter(RoutingComponent::class, 'parameters', $parameters);

$request = $request->withAttribute(ServerRequestAttributes::ROUTING_PARAMETERS, $parameters);
return $next->handle($request);
}

/**
* @param Http\Component\ComponentContext $componentContext
* @param ServerRequestInterface $request
* @param bool $uriPathSegmentUsed
* @return array
* @throws Exception\InvalidDimensionPresetDetectorException
*/
protected function detectDimensionSpacePoint(Http\Component\ComponentContext $componentContext, bool &$uriPathSegmentUsed): array
protected function detectDimensionSpacePoint(ServerRequestInterface $request, bool &$uriPathSegmentUsed): array
{
$coordinates = [];
$path = $componentContext->getHttpRequest()->getUri()->getPath();

$path = $request->getUri()->getPath();

$isContextPath = NodePaths::isContextPath($path);
$backendUriDimensionPresetDetector = new ContentDimensionDetection\BackendUriDimensionPresetDetector();
Expand All @@ -96,12 +111,12 @@ protected function detectDimensionSpacePoint(Http\Component\ComponentContext $co
$options['defaultPresetIdentifier'] = $presetConfiguration['defaultPreset'];

if ($isContextPath) {
$preset = $backendUriDimensionPresetDetector->detectPreset($dimensionName, $presetConfiguration['presets'], $componentContext);
$preset = $backendUriDimensionPresetDetector->detectPreset($dimensionName, $presetConfiguration['presets'], $request);
if ($preset) {
$coordinates[$dimensionName] = $preset['values'];
if ($detector instanceof ContentDimensionDetection\UriPathSegmentDimensionPresetDetector) {
// we might have to remove the uri path segment anyway
$uriPathSegmentPreset = $detector->detectPreset($dimensionName, $presetConfiguration['presets'], $componentContext, $options);
$uriPathSegmentPreset = $detector->detectPreset($dimensionName, $presetConfiguration['presets'], $request, $options);
if ($uriPathSegmentPreset) {
$uriPathSegmentUsed = true;
}
Expand All @@ -114,7 +129,7 @@ protected function detectDimensionSpacePoint(Http\Component\ComponentContext $co
if ($resolutionMode === ContentDimensionResolutionMode::RESOLUTION_MODE_URIPATHSEGMENT) {
$options['delimiter'] = $this->uriPathSegmentDelimiter;
}
$preset = $detector->detectPreset($dimensionName, $presetConfiguration['presets'], $componentContext, $options);
$preset = $detector->detectPreset($dimensionName, $presetConfiguration['presets'], $request, $options);
if ($preset && $resolutionMode === ContentDimensionResolutionMode::RESOLUTION_MODE_URIPATHSEGMENT) {
$uriPathSegmentUsed = true;
$uriPathSegmentOffset++;
Expand Down Expand Up @@ -175,14 +190,14 @@ protected function generateOptionsFromLegacyConfiguration(array $presetConfigura
}

/**
* @param Http\Component\ComponentContext $componentContext
* @param ServerRequestInterface $request
* @return string
*/
protected function detectContentStream(Http\Component\ComponentContext $componentContext): string
protected function detectContentStream(ServerRequestInterface $request): string
{
$contentStreamIdentifier = 'live';

$requestPath = $componentContext->getHttpRequest()->getUri()->getPath();
$requestPath = $request->getUri()->getPath();
$requestPath = mb_substr($requestPath, mb_strrpos($requestPath, '/'));
if ($requestPath !== '' && NodePaths::isContextPath($requestPath)) {
try {
Expand Down
6 changes: 3 additions & 3 deletions Classes/Routing/FrontendNodeRoutePartHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use Flowpack\Neos\DimensionResolver\Http;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Log\PsrSystemLoggerInterface;
use Psr\Log\LoggerInterface;
use Neos\Flow\Mvc\Routing\Dto\MatchResult;
use Neos\Flow\Mvc\Routing\Dto\ResolveResult;
use Neos\Flow\Mvc\Routing\Dto\RouteTags;
Expand All @@ -37,8 +37,8 @@
class FrontendNodeRoutePartHandler extends DynamicRoutePart implements FrontendNodeRoutePartHandlerInterface
{
/**
* @Flow\Inject
* @var PsrSystemLoggerInterface
* @Flow\Inject(name="Neos.Flow:SystemLogger")
* @var LoggerInterface
*/
protected $systemLogger;

Expand Down
10 changes: 5 additions & 5 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Flowpack:
contentDimensions:
resolution:
uriPathSegmentDelimiter: '_'

Neos:
Flow:
http:
chain:
preprocess:
chain:
detectContentSubgraph:
component: Flowpack\Neos\DimensionResolver\Http\DetectContentSubgraphComponent
middlewares:
'detectContentSubgraph':
position: 'before routing'
middleware: 'Flowpack\Neos\DimensionResolver\Http\DetectContentSubgraphMiddleware'
4 changes: 2 additions & 2 deletions Documentation/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ Those complex scenarios are better served using individual implementations than

To enable developers to deal with this in a nice way, there are predefined ways to deal with both detection and link processing.

Detection is done via an HTTP component that can be replaced via configuration:
Detection is done via an HTTP middleware that can be replaced via configuration:

.. code-block:: yaml
Expand All @@ -252,7 +252,7 @@ Detection is done via an HTTP component that can be replaced via configuration:
preprocess:
chain:
detectContentSubgraph:
component: Flowpack\Neos\DimensionResolver\Http\DetectContentSubgraphComponent
component: Flowpack\Neos\DimensionResolver\Http\DetectContentSubgraphMiddleware
Link processing is done by the ``Flowpack\Neos\DimensionResolver\Http\ContentSubgraphUriProcessorInterface``. To introduce your custom behaviour,
implement the interface and declare it in ``Objects.yaml`` as usual in Flow.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
use Neos\Flow\Mvc\Routing\RoutingComponent;
use Neos\Flow\Tests\FunctionalTestCase;
use Flowpack\Neos\DimensionResolver\Http\ContentDimensionResolutionMode;
use Flowpack\Neos\DimensionResolver\Http\DetectContentSubgraphComponent;
use Flowpack\Neos\DimensionResolver\Http\DetectContentSubgraphMiddleware;

/**
* Test case for the BackendUriDimensionPresetDetector
*/
class DetectContentSubgraphComponentTest extends FunctionalTestCase
class DetectContentSubgraphMiddlewareTest extends FunctionalTestCase
{
/**
* @var array
Expand Down Expand Up @@ -110,9 +110,9 @@ public function handleAddsCorrectSubgraphIdentityToComponentContextWithAllDimens

$dimensionPresetSource = $this->objectManager->get(ContentDimensionPresetSourceInterface::class);
$dimensionPresetSource->setConfiguration($this->dimensionPresets);
$detectSubgraphComponent = new DetectContentSubgraphComponent();
$detectSubgraphMiddleware = new DetectContentSubgraphMiddleware();

$detectSubgraphComponent->handle($componentContext);
$detectSubgraphMiddleware->handle($componentContext);
/** @var RouteParameters $routeParameters */
$routeParameters = $componentContext->getParameter(RoutingComponent::class, 'parameters');

Expand All @@ -135,11 +135,11 @@ public function handleAddsCorrectSubgraphIdentityToComponentContextWithAllDimens

$dimensionPresetSource = $this->objectManager->get(ContentDimensionPresetSourceInterface::class);
$dimensionPresetSource->setConfiguration($this->dimensionPresets);
$detectSubgraphComponent = new DetectContentSubgraphComponent();
$detectSubgraphMiddleware = new DetectContentSubgraphMiddleware();

$this->inject($detectSubgraphComponent, 'uriPathSegmentDelimiter', '-');
$this->inject($detectSubgraphMiddleware, 'uriPathSegmentDelimiter', '-');

$detectSubgraphComponent->handle($componentContext);
$detectSubgraphMiddleware->handle($componentContext);
/** @var RouteParameters $routeParameters */
$routeParameters = $componentContext->getParameter(RoutingComponent::class, 'parameters');

Expand All @@ -162,9 +162,9 @@ public function handleAddsCorrectSubgraphIdentityToComponentContextWithMinimalDi

$dimensionPresetSource = $this->objectManager->get(ContentDimensionPresetSourceInterface::class);
$dimensionPresetSource->setConfiguration($this->dimensionPresets);
$detectSubgraphComponent = new DetectContentSubgraphComponent();
$detectSubgraphMiddleware = new DetectContentSubgraphMiddleware();

$detectSubgraphComponent->handle($componentContext);
$detectSubgraphMiddleware->handle($componentContext);
/** @var RouteParameters $routeParameters */
$routeParameters = $componentContext->getParameter(RoutingComponent::class, 'parameters');

Expand All @@ -188,9 +188,9 @@ public function handleAddsCorrectSubgraphIdentityToComponentContextWithDimension

$dimensionPresetSource = $this->objectManager->get(ContentDimensionPresetSourceInterface::class);
$dimensionPresetSource->setConfiguration($this->dimensionPresets);
$detectSubgraphComponent = new DetectContentSubgraphComponent();
$detectSubgraphMiddleware = new DetectContentSubgraphMiddleware();

$detectSubgraphComponent->handle($componentContext);
$detectSubgraphMiddleware->handle($componentContext);
/** @var RouteParameters $routeParameters */
$routeParameters = $componentContext->getParameter(RoutingComponent::class, 'parameters');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Flowpack\Neos\DimensionResolver\Tests\Unit\Http\ContentDimensionDetection\Fixtures\ValidDummyDimensionPresetDetector;

/**
* Test case for the DetectContentSubgraphComponent
* Test case for the DetectContentSubgraphMiddleware
*/
class DimensionPresetDetectorResolverTest extends UnitTestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Flowpack\Neos\DimensionResolver\Tests\Unit\Http\ContentDimensionLinking\Fixtures\ValidDummyDimensionPresetLinkProcessor;

/**
* Test case for the DetectContentSubgraphComponent
* Test case for the DetectContentSubgraphMiddleware
*/
class DimensionPresetLinkProcessorResolverTest extends UnitTestCase
{
Expand Down
Loading

0 comments on commit b3f8e4f

Please sign in to comment.