diff --git a/src/Symfony/Component/HttpKernel/CHANGELOG.md b/src/Symfony/Component/HttpKernel/CHANGELOG.md index 9ccb0c10e0770..1ed8f3a7108d1 100644 --- a/src/Symfony/Component/HttpKernel/CHANGELOG.md +++ b/src/Symfony/Component/HttpKernel/CHANGELOG.md @@ -7,6 +7,7 @@ CHANGELOG * Add constructor argument `bool $catchThrowable` to `HttpKernel` * Add `ControllerEvent::getAttributes()` to handle attributes on controllers * Add `#[Cache]` to describe the default HTTP cache headers on controllers + * Add `absolute_uri` option to surrogate fragment renderers 6.1 --- diff --git a/src/Symfony/Component/HttpKernel/Fragment/AbstractSurrogateFragmentRenderer.php b/src/Symfony/Component/HttpKernel/Fragment/AbstractSurrogateFragmentRenderer.php index 6135c86e0d8ec..9f48889e418e6 100644 --- a/src/Symfony/Component/HttpKernel/Fragment/AbstractSurrogateFragmentRenderer.php +++ b/src/Symfony/Component/HttpKernel/Fragment/AbstractSurrogateFragmentRenderer.php @@ -51,6 +51,7 @@ public function __construct(SurrogateInterface $surrogate = null, FragmentRender * * * alt: an alternative URI to render in case of an error * * comment: a comment to add when returning the surrogate tag + * * absolute_uri: whether to generate an absolute URI or not. Default is false * * Note, that not all surrogate strategies support all options. For now * 'alt' and 'comment' are only supported by ESI. @@ -67,13 +68,15 @@ public function render(string|ControllerReference $uri, Request $request, array return $this->inlineStrategy->render($uri, $request, $options); } + $absolute = $options['absolute_uri'] ?? false; + if ($uri instanceof ControllerReference) { - $uri = $this->generateSignedFragmentUri($uri, $request); + $uri = $this->generateSignedFragmentUri($uri, $request, $absolute); } $alt = $options['alt'] ?? null; if ($alt instanceof ControllerReference) { - $alt = $this->generateSignedFragmentUri($alt, $request); + $alt = $this->generateSignedFragmentUri($alt, $request, $absolute); } $tag = $this->surrogate->renderIncludeTag($uri, $alt, $options['ignore_errors'] ?? false, $options['comment'] ?? ''); @@ -81,9 +84,9 @@ public function render(string|ControllerReference $uri, Request $request, array return new Response($tag); } - private function generateSignedFragmentUri(ControllerReference $uri, Request $request): string + private function generateSignedFragmentUri(ControllerReference $uri, Request $request, bool $absolute): string { - return (new FragmentUriGenerator($this->fragmentPath, $this->signer))->generate($uri, $request); + return (new FragmentUriGenerator($this->fragmentPath, $this->signer))->generate($uri, $request, $absolute); } private function containsNonScalars(array $values): bool diff --git a/src/Symfony/Component/HttpKernel/Tests/Fragment/EsiFragmentRendererTest.php b/src/Symfony/Component/HttpKernel/Tests/Fragment/EsiFragmentRendererTest.php index 3817f4897efbd..b90e8002267d4 100644 --- a/src/Symfony/Component/HttpKernel/Tests/Fragment/EsiFragmentRendererTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/Fragment/EsiFragmentRendererTest.php @@ -66,6 +66,24 @@ public function testRenderControllerReference() ); } + public function testRenderControllerReferenceWithAbsoluteUri() + { + $signer = new UriSigner('foo'); + $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(), $signer); + + $request = Request::create('http://localhost/'); + $request->setLocale('fr'); + $request->headers->set('Surrogate-Capability', 'ESI/1.0'); + + $reference = new ControllerReference('main_controller', [], []); + $altReference = new ControllerReference('alt_controller', [], []); + + $this->assertSame( + '', + $strategy->render($reference, $request, ['alt' => $altReference, 'absolute_uri' => true])->getContent() + ); + } + public function testRenderControllerReferenceWithoutSignerThrowsException() { $this->expectException(\LogicException::class); diff --git a/src/Symfony/Component/HttpKernel/Tests/Fragment/SsiFragmentRendererTest.php b/src/Symfony/Component/HttpKernel/Tests/Fragment/SsiFragmentRendererTest.php index 63a92028f4234..55e73e2fcb245 100644 --- a/src/Symfony/Component/HttpKernel/Tests/Fragment/SsiFragmentRendererTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/Fragment/SsiFragmentRendererTest.php @@ -57,6 +57,24 @@ public function testRenderControllerReference() ); } + public function testRenderControllerReferenceWithAbsoluteUri() + { + $signer = new UriSigner('foo'); + $strategy = new SsiFragmentRenderer(new Ssi(), $this->getInlineStrategy(), $signer); + + $request = Request::create('http://localhost/'); + $request->setLocale('fr'); + $request->headers->set('Surrogate-Capability', 'SSI/1.0'); + + $reference = new ControllerReference('main_controller', [], []); + $altReference = new ControllerReference('alt_controller', [], []); + + $this->assertSame( + '', + $strategy->render($reference, $request, ['alt' => $altReference, 'absolute_uri' => true])->getContent() + ); + } + public function testRenderControllerReferenceWithoutSignerThrowsException() { $this->expectException(\LogicException::class);