Skip to content

Commit 487dcc2

Browse files
author
Bernhard Schmitt
committed
Initialize UriService::controllerContext from view if possible
1 parent 1c5f136 commit 487dcc2

4 files changed

Lines changed: 67 additions & 26 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PackageFactory.AtomicFusion.PresentationObjects package.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace PackageFactory\AtomicFusion\PresentationObjects\Aspect;
10+
11+
use Neos\Flow\Annotations as Flow;
12+
use Neos\Flow\Aop\JoinPointInterface;
13+
use Neos\Flow\Mvc\Controller\ControllerContext;
14+
use PackageFactory\AtomicFusion\PresentationObjects\Infrastructure\UriService;
15+
16+
#[Flow\Aspect]
17+
#[Flow\Scope('singleton')]
18+
final class UriServiceInitializationAspect
19+
{
20+
#[Flow\Inject]
21+
protected UriService $uriService;
22+
23+
#[Flow\Before('method(Neos\Flow\Mvc\View\AbstractView->setControllerContext())')]
24+
public function relayControllerContext(JoinPointInterface $joinPoint): void
25+
{
26+
/** @var ControllerContext $controllerContext */
27+
$controllerContext = $joinPoint->getMethodArgument('controllerContext');
28+
$this->uriService->useControllerContext($controllerContext);
29+
}
30+
}

Classes/Fusion/UriServiceInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public function getNodeUri(
2323
?string $format = null
2424
): UriInterface;
2525

26+
public function useControllerContext(ControllerContext $controllerContext): void;
27+
2628
public function getResourceUri(string $packageKey, string $resourcePath): UriInterface;
2729

2830
public function getPersistentResourceUri(PersistentResource $resource): ?UriInterface;

Classes/Infrastructure/UriService.php

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,21 @@
3333
* The URI service
3434
*/
3535
#[Flow\Scope('singleton')]
36-
final readonly class UriService implements UriServiceInterface
36+
final class UriService implements UriServiceInterface
3737
{
38-
public ControllerContext $controllerContext;
38+
private ?ControllerContext $controllerContext = null;
3939

4040
public function __construct(
41-
private ContentRepositoryRegistry $contentRepositoryRegistry,
42-
private ResourceManager $resourceManager,
43-
private AssetRepository $assetRepository,
44-
Bootstrap $bootstrap
41+
private readonly ContentRepositoryRegistry $contentRepositoryRegistry,
42+
private readonly ResourceManager $resourceManager,
43+
private readonly AssetRepository $assetRepository,
44+
private readonly Bootstrap $bootstrap
4545
) {
46-
$requestHandler = $bootstrap->getActiveRequestHandler();
47-
if ($requestHandler instanceof Http\RequestHandler) {
48-
$request = $requestHandler->getHttpRequest();
49-
} else {
50-
$request = ServerRequest::fromGlobals();
51-
}
52-
$actionRequest = Mvc\ActionRequest::fromHttpRequest($request);
53-
$uriBuilder = new Mvc\Routing\UriBuilder();
54-
$uriBuilder->setRequest($actionRequest);
55-
$this->controllerContext = new Mvc\Controller\ControllerContext(
56-
$actionRequest,
57-
new Mvc\ActionResponse(),
58-
new Mvc\Controller\Arguments(),
59-
$uriBuilder
60-
);
46+
}
47+
48+
public function useControllerContext(ControllerContext $controllerContext): void
49+
{
50+
$this->controllerContext = $controllerContext;
6151
}
6252

6353
public function getNodeUri(Node $documentNode, bool $absolute = false, ?string $format = null): UriInterface
@@ -67,13 +57,15 @@ public function getNodeUri(Node $documentNode, bool $absolute = false, ?string $
6757
);
6858
$nodeAddressFactory = NodeAddressFactory::create($contentRepository);
6959
$nodeAddress = $nodeAddressFactory->createFromNode($documentNode);
60+
7061
$uriBuilder = new UriBuilder();
71-
$uriBuilder->setRequest($this->controllerContext->getRequest());
62+
$uriBuilder->setRequest($this->getControllerContext()->getRequest());
7263
$uriBuilder
7364
->setCreateAbsoluteUri($absolute)
7465
->setFormat($format ?: 'html');
7566

76-
return NodeUriBuilder::fromUriBuilder($uriBuilder)->uriFor($nodeAddress);
67+
return NodeUriBuilder::fromUriBuilder($uriBuilder)
68+
->uriFor($nodeAddress);
7769
}
7870

7971
public function getResourceUri(string $packageKey, string $resourcePath): UriInterface
@@ -99,7 +91,7 @@ public function getAssetUri(AssetInterface $asset): UriInterface
9991

10092
public function getDummyImageBaseUri(): UriInterface
10193
{
102-
$uriBuilder = $this->controllerContext->getUriBuilder();
94+
$uriBuilder = $this->getControllerContext()->getUriBuilder();
10395

10496
return new Uri($uriBuilder->uriFor(
10597
'image',
@@ -111,6 +103,23 @@ public function getDummyImageBaseUri(): UriInterface
111103

112104
public function getControllerContext(): ControllerContext
113105
{
106+
if (!$this->controllerContext) {
107+
$requestHandler = $this->bootstrap->getActiveRequestHandler();
108+
if ($requestHandler instanceof Http\RequestHandler) {
109+
$request = $requestHandler->getHttpRequest();
110+
} else {
111+
$request = ServerRequest::fromGlobals();
112+
}
113+
$actionRequest = Mvc\ActionRequest::fromHttpRequest($request);
114+
$uriBuilder = new Mvc\Routing\UriBuilder();
115+
$uriBuilder->setRequest($actionRequest);
116+
$this->controllerContext = new Mvc\Controller\ControllerContext(
117+
$actionRequest,
118+
new Mvc\ActionResponse(),
119+
new Mvc\Controller\Arguments(),
120+
$uriBuilder
121+
);
122+
}
114123
return $this->controllerContext;
115124
}
116125

Tests/Unit/Infrastructure/UriServiceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ public function testProvidesADummyImageUri(): void
146146
public function testProvidesAControllerContext(): void
147147
{
148148
$this->markTestSkipped('Cannot mock the content repository registry yet');
149-
$controllerContext = $this->uriService->controllerContext;
149+
$controllerContext = $this->uriService->getControllerContext();
150150

151151
$this->assertTrue($controllerContext instanceof ControllerContext);
152-
$this->assertSame($controllerContext, $this->uriService->controllerContext);
152+
$this->assertSame($controllerContext, $this->uriService->getControllerContext());
153153
}
154154

155155
public function testResolvesLinkUrisWithNodeProtocolToHashIfNodeCannotBeFound(): void

0 commit comments

Comments
 (0)