Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel] Add option to render Surrogate fragment with absolute URIs #46514

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

Merged
merged 1 commit into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -67,23 +68,25 @@ 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'] ?? '');

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<esi:include src="http://localhost/_fragment?_hash=Jz1P8NErmhKTeI6onI1EdAXTB85359MY3RIk5mSJ60w%3D&_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dmain_controller" alt="http://localhost/_fragment?_hash=iPJEdRoUpGrM1ztqByiorpfMPtiW%2FOWwdH1DBUXHhEc%3D&_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dalt_controller" />',
$strategy->render($reference, $request, ['alt' => $altReference, 'absolute_uri' => true])->getContent()
);
}

public function testRenderControllerReferenceWithoutSignerThrowsException()
{
$this->expectException(\LogicException::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<!--#include virtual="http://localhost/_fragment?_hash=Jz1P8NErmhKTeI6onI1EdAXTB85359MY3RIk5mSJ60w%3D&_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dmain_controller" -->',
$strategy->render($reference, $request, ['alt' => $altReference, 'absolute_uri' => true])->getContent()
);
}

public function testRenderControllerReferenceWithoutSignerThrowsException()
{
$this->expectException(\LogicException::class);
Expand Down