Skip to content

Commit e72f7d0

Browse files
committed
CLI: use request context to generate absolute URLs
1 parent 15c37c2 commit e72f7d0

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

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

+22-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
namespace Symfony\Bridge\Twig\Extension;
1313

1414
use Symfony\Component\HttpFoundation\RequestStack;
15-
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\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 HttpFoundationExtension extends \Twig_Extension
2324
{
2425
private $requestStack;
26+
private $requestContext;
2527

26-
public function __construct(RequestStack $requestStack)
28+
public function __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

+44
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bridge\Twig\Extension\HttpFoundationExtension;
1515
use Symfony\Component\HttpFoundation\RequestStack;
1616
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\Routing\RequestContext;
1718

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

47+
/**
48+
* @dataProvider getGenerateAbsoluteUrlRequestContextData
49+
*/
50+
public function testGenerateAbsoluteUrlWithRequestContext($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 = new RequestContext($baseUrl, 'GET', $host, $scheme, $httpPort, $httpsPort, $path);
57+
$extension = new HttpFoundationExtension(new RequestStack(), $requestContext);
58+
59+
$this->assertEquals($expected, $extension->generateAbsoluteUrl($path));
60+
}
61+
62+
/**
63+
* @dataProvider getGenerateAbsoluteUrlRequestContextData
64+
*/
65+
public function testGenerateAbsoluteUrlWithoutRequestAndRequestContext($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 = new HttpFoundationExtension(new RequestStack());
72+
73+
$this->assertEquals($path, $extension->generateAbsoluteUrl($path));
74+
}
75+
76+
public function getGenerateAbsoluteUrlRequestContextData()
77+
{
78+
return array(
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
public function testGenerateAbsoluteUrlWithScriptFileName()
4791
{
4892
$request = Request::create('http://localhost/app/web/app_dev.php');

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

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127

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

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

0 commit comments

Comments
 (0)