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
0 commit comments