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

Commite72f7d0

Browse files
committed
CLI: use request context to generate absolute URLs
1 parent15c37c2 commite72f7d0

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

‎src/Symfony/Bridge/Twig/Extension/HttpFoundationExtension.php‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
namespaceSymfony\Bridge\Twig\Extension;
1313

1414
useSymfony\Component\HttpFoundation\RequestStack;
15-
useSymfony\Component\HttpFoundation\Request;
15+
useSymfony\Component\HttpFoundation\Request
16+
use Symfony\Component\Routing\RequestContext;
1617

1718
/**
1819
* Twig extension for the Symfony HttpFoundation component.
@@ -22,10 +23,12 @@
2223
class HttpFoundationExtensionextends \Twig_Extension
2324
{
2425
private$requestStack;
26+
private$requestContext;
2527

26-
publicfunction__construct(RequestStack$requestStack)
28+
publicfunction__construct(RequestStack$requestStack,RequestContext$requestContext =null)
2729
{
2830
$this->requestStack =$requestStack;
31+
$this->requestContext =$requestContext;
2932
}
3033

3134
/**
@@ -57,6 +60,23 @@ public function generateAbsoluteUrl($path)
5760
}
5861

5962
if (!$request =$this->requestStack->getMasterRequest()) {
63+
if (null !==$this->requestContext &&'' !==$host =$this->requestContext->getHost()) {
64+
$scheme =$this->requestContext->getScheme();
65+
$port ='';
66+
67+
if ('http' ===$scheme &&80 !=$this->requestContext->getHttpPort()) {
68+
$port =':'.$this->requestContext->getHttpPort();
69+
}elseif ('https' ===$scheme &&443 !=$this->requestContext->getHttpsPort()) {
70+
$port =':'.$this->requestContext->getHttpsPort();
71+
}
72+
73+
if ('/' !==$path[0]) {
74+
$path =rtrim($this->requestContext->getBaseUrl(),'/').'/'.$path;
75+
}
76+
77+
return$scheme.'://'.$host.$port.$path;
78+
}
79+
6080
return$path;
6181
}
6282

‎src/Symfony/Bridge/Twig/Tests/Extension/HttpFoundationExtensionTest.php‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
useSymfony\Bridge\Twig\Extension\HttpFoundationExtension;
1515
useSymfony\Component\HttpFoundation\RequestStack;
1616
useSymfony\Component\HttpFoundation\Request;
17+
useSymfony\Component\Routing\RequestContext;
1718

1819
class HttpFoundationExtensionTestextends \PHPUnit_Framework_TestCase
1920
{
@@ -43,6 +44,49 @@ public function getGenerateAbsoluteUrlData()
4344
);
4445
}
4546

47+
/**
48+
* @dataProvider getGenerateAbsoluteUrlRequestContextData
49+
*/
50+
publicfunctiontestGenerateAbsoluteUrlWithRequestContext($path,$baseUrl,$host,$scheme,$httpPort,$httpsPort,$expected)
51+
{
52+
if (!class_exists('Symfony\Component\Routing\RequestContext')) {
53+
$this->markTestSkipped('The Routing component is needed to run tests that depend on its request context.');
54+
}
55+
56+
$requestContext =newRequestContext($baseUrl,'GET',$host,$scheme,$httpPort,$httpsPort,$path);
57+
$extension =newHttpFoundationExtension(newRequestStack(),$requestContext);
58+
59+
$this->assertEquals($expected,$extension->generateAbsoluteUrl($path));
60+
}
61+
62+
/**
63+
* @dataProvider getGenerateAbsoluteUrlRequestContextData
64+
*/
65+
publicfunctiontestGenerateAbsoluteUrlWithoutRequestAndRequestContext($path)
66+
{
67+
if (!class_exists('Symfony\Component\Routing\RequestContext')) {
68+
$this->markTestSkipped('The Routing component is needed to run tests that depend on its request context.');
69+
}
70+
71+
$extension =newHttpFoundationExtension(newRequestStack());
72+
73+
$this->assertEquals($path,$extension->generateAbsoluteUrl($path));
74+
}
75+
76+
publicfunctiongetGenerateAbsoluteUrlRequestContextData()
77+
{
78+
returnarray(
79+
array('/foo.png','/foo','localhost','http',80,443,'http://localhost/foo.png'),
80+
array('foo.png','/foo','localhost','http',80,443,'http://localhost/foo/foo.png'),
81+
array('foo.png','/foo/bar/','localhost','http',80,443,'http://localhost/foo/bar/foo.png'),
82+
array('/foo.png','/foo','localhost','https',80,443,'https://localhost/foo.png'),
83+
array('foo.png','/foo','localhost','https',80,443,'https://localhost/foo/foo.png'),
84+
array('foo.png','/foo/bar/','localhost','https',80,443,'https://localhost/foo/bar/foo.png'),
85+
array('/foo.png','/foo','localhost','http',443,80,'http://localhost:443/foo.png'),
86+
array('/foo.png','/foo','localhost','https',443,80,'https://localhost:80/foo.png'),
87+
);
88+
}
89+
4690
publicfunctiontestGenerateAbsoluteUrlWithScriptFileName()
4791
{
4892
$request = Request::create('http://localhost/app/web/app_dev.php');

‎src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127

128128
<serviceid="twig.extension.httpfoundation"class="Symfony\Bridge\Twig\Extension\HttpFoundationExtension"public="false">
129129
<argumenttype="service"id="request_stack" />
130+
<argumenttype="service"id="router.request_context"on-invalid="ignore" />
130131
</service>
131132

132133
<serviceid="twig.extension.form"class="%twig.extension.form.class%"public="false">

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp