Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0a78a2a

Browse files
Axel Venetnicolas-grekas
Axel Venet
authored andcommitted
[HttpKernel] Add option to render Surrogate fragment with absolute URIs
Allow usage of `absolute_uri` option when using `render_esi` or `render_ssi` in order to get absolute URIs.
1 parent5bf31c5 commit0a78a2a

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

‎src/Symfony/Component/HttpKernel/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Add constructor argument`bool $catchThrowable` to`HttpKernel`
88
* Add`ControllerEvent::getAttributes()` to handle attributes on controllers
99
* Add`#[Cache]` to describe the default HTTP cache headers on controllers
10+
* Add`absolute_uri` option to surrogate fragment renderers
1011

1112
6.1
1213
---

‎src/Symfony/Component/HttpKernel/Fragment/AbstractSurrogateFragmentRenderer.php‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function __construct(SurrogateInterface $surrogate = null, FragmentRender
5151
*
5252
* * alt: an alternative URI to render in case of an error
5353
* * comment: a comment to add when returning the surrogate tag
54+
* * absolute_uri: whether to generate an absolute URI or not. Default is false
5455
*
5556
* Note, that not all surrogate strategies support all options. For now
5657
* 'alt' and 'comment' are only supported by ESI.
@@ -67,23 +68,25 @@ public function render(string|ControllerReference $uri, Request $request, array
6768
return$this->inlineStrategy->render($uri,$request,$options);
6869
}
6970

71+
$absolute =$options['absolute_uri'] ??false;
72+
7073
if ($uriinstanceof ControllerReference) {
71-
$uri =$this->generateSignedFragmentUri($uri,$request);
74+
$uri =$this->generateSignedFragmentUri($uri,$request,$absolute);
7275
}
7376

7477
$alt =$options['alt'] ??null;
7578
if ($altinstanceof ControllerReference) {
76-
$alt =$this->generateSignedFragmentUri($alt,$request);
79+
$alt =$this->generateSignedFragmentUri($alt,$request,$absolute);
7780
}
7881

7982
$tag =$this->surrogate->renderIncludeTag($uri,$alt,$options['ignore_errors'] ??false,$options['comment'] ??'');
8083

8184
returnnewResponse($tag);
8285
}
8386

84-
privatefunctiongenerateSignedFragmentUri(ControllerReference$uri,Request$request):string
87+
privatefunctiongenerateSignedFragmentUri(ControllerReference$uri,Request$request,bool$absolute):string
8588
{
86-
return (newFragmentUriGenerator($this->fragmentPath,$this->signer))->generate($uri,$request);
89+
return (newFragmentUriGenerator($this->fragmentPath,$this->signer))->generate($uri,$request,$absolute);
8790
}
8891

8992
privatefunctioncontainsNonScalars(array$values):bool

‎src/Symfony/Component/HttpKernel/Tests/Fragment/EsiFragmentRendererTest.php‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,30 @@ public function testRenderControllerReference()
6060
$reference =newControllerReference('main_controller', [], []);
6161
$altReference =newControllerReference('alt_controller', [], []);
6262

63-
$this->assertEquals(
63+
$this->assertSame(
6464
'<esi:include src="/_fragment?_hash=Jz1P8NErmhKTeI6onI1EdAXTB85359MY3RIk5mSJ60w%3D&_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dmain_controller" alt="/_fragment?_hash=iPJEdRoUpGrM1ztqByiorpfMPtiW%2FOWwdH1DBUXHhEc%3D&_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dalt_controller" />',
6565
$strategy->render($reference,$request, ['alt' =>$altReference])->getContent()
6666
);
6767
}
6868

69+
publicfunctiontestRenderControllerReferenceWithAbsoluteUri()
70+
{
71+
$signer =newUriSigner('foo');
72+
$strategy =newEsiFragmentRenderer(newEsi(),$this->getInlineStrategy(),$signer);
73+
74+
$request = Request::create('http://localhost/');
75+
$request->setLocale('fr');
76+
$request->headers->set('Surrogate-Capability','ESI/1.0');
77+
78+
$reference =newControllerReference('main_controller', [], []);
79+
$altReference =newControllerReference('alt_controller', [], []);
80+
81+
$this->assertSame(
82+
'<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" />',
83+
$strategy->render($reference,$request, ['alt' =>$altReference,'absolute_uri' =>true])->getContent()
84+
);
85+
}
86+
6987
publicfunctiontestRenderControllerReferenceWithoutSignerThrowsException()
7088
{
7189
$this->expectException(\LogicException::class);

‎src/Symfony/Component/HttpKernel/Tests/Fragment/SsiFragmentRendererTest.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ public function testRenderControllerReference()
5757
);
5858
}
5959

60+
publicfunctiontestRenderControllerReferenceWithAbsoluteUri()
61+
{
62+
$signer =newUriSigner('foo');
63+
$strategy =newSsiFragmentRenderer(newSsi(),$this->getInlineStrategy(),$signer);
64+
65+
$request = Request::create('http://localhost/');
66+
$request->setLocale('fr');
67+
$request->headers->set('Surrogate-Capability','SSI/1.0');
68+
69+
$reference =newControllerReference('main_controller', [], []);
70+
$altReference =newControllerReference('alt_controller', [], []);
71+
72+
$this->assertEquals(
73+
'<!--#include virtual="http://localhost/_fragment?_hash=Jz1P8NErmhKTeI6onI1EdAXTB85359MY3RIk5mSJ60w%3D&_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dmain_controller" -->',
74+
$strategy->render($reference,$request, ['alt' =>$altReference,'absolute_uri' =>true])->getContent()
75+
);
76+
}
77+
6078
publicfunctiontestRenderControllerReferenceWithoutSignerThrowsException()
6179
{
6280
$this->expectException(\LogicException::class);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp