CLI: use request context to generate absolute URLs
This commit is contained in:
parent
15c37c2442
commit
766a648ded
@ -13,6 +13,7 @@ namespace Symfony\Bridge\Twig\Extension;
|
||||
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
/**
|
||||
* Twig extension for the Symfony HttpFoundation component.
|
||||
@ -22,10 +23,12 @@ use Symfony\Component\HttpFoundation\Request;
|
||||
class HttpFoundationExtension extends \Twig_Extension
|
||||
{
|
||||
private $requestStack;
|
||||
private $requestContext;
|
||||
|
||||
public function __construct(RequestStack $requestStack)
|
||||
public function __construct(RequestStack $requestStack, RequestContext $requestContext = null)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
$this->requestContext = $requestContext;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -57,6 +60,23 @@ class HttpFoundationExtension extends \Twig_Extension
|
||||
}
|
||||
|
||||
if (!$request = $this->requestStack->getMasterRequest()) {
|
||||
if (null !== $this->requestContext && '' !== $host = $this->requestContext->getHost()) {
|
||||
$scheme = $this->requestContext->getScheme();
|
||||
$port = '';
|
||||
|
||||
if ('http' === $scheme && 80 != $this->requestContext->getHttpPort()) {
|
||||
$port = ':'.$this->requestContext->getHttpPort();
|
||||
} elseif ('https' === $scheme && 443 != $this->requestContext->getHttpsPort()) {
|
||||
$port = ':'.$this->requestContext->getHttpsPort();
|
||||
}
|
||||
|
||||
if ('/' !== $path[0]) {
|
||||
$path = rtrim($this->requestContext->getBaseUrl(), '/').'/'.$path;
|
||||
}
|
||||
|
||||
return $scheme.'://'.$host.$port.$path;
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,7 @@ namespace Symfony\Bridge\Twig\Tests\Extension;
|
||||
use Symfony\Bridge\Twig\Extension\HttpFoundationExtension;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
class HttpFoundationExtensionTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
@ -43,6 +44,49 @@ class HttpFoundationExtensionTest extends \PHPUnit_Framework_TestCase
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getGenerateAbsoluteUrlRequestContextData
|
||||
*/
|
||||
public function testGenerateAbsoluteUrlWithRequestContext($path, $baseUrl, $host, $scheme, $httpPort, $httpsPort, $expected)
|
||||
{
|
||||
if (!class_exists('Symfony\Component\Routing\RequestContext')) {
|
||||
$this->markTestSkipped('The Routing component is needed to run tests that depend on its request context.');
|
||||
}
|
||||
|
||||
$requestContext = new RequestContext($baseUrl, 'GET', $host, $scheme, $httpPort, $httpsPort, $path);
|
||||
$extension = new HttpFoundationExtension(new RequestStack(), $requestContext);
|
||||
|
||||
$this->assertEquals($expected, $extension->generateAbsoluteUrl($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getGenerateAbsoluteUrlRequestContextData
|
||||
*/
|
||||
public function testGenerateAbsoluteUrlWithoutRequestAndRequestContext($path)
|
||||
{
|
||||
if (!class_exists('Symfony\Component\Routing\RequestContext')) {
|
||||
$this->markTestSkipped('The Routing component is needed to run tests that depend on its request context.');
|
||||
}
|
||||
|
||||
$extension = new HttpFoundationExtension(new RequestStack());
|
||||
|
||||
$this->assertEquals($path, $extension->generateAbsoluteUrl($path));
|
||||
}
|
||||
|
||||
public function getGenerateAbsoluteUrlRequestContextData()
|
||||
{
|
||||
return array(
|
||||
array('/foo.png', '/foo', 'localhost', 'http', 80, 443, 'http://localhost/foo.png'),
|
||||
array('foo.png', '/foo', 'localhost', 'http', 80, 443, 'http://localhost/foo/foo.png'),
|
||||
array('foo.png', '/foo/bar/', 'localhost', 'http', 80, 443, 'http://localhost/foo/bar/foo.png'),
|
||||
array('/foo.png', '/foo', 'localhost', 'https', 80, 443, 'https://localhost/foo.png'),
|
||||
array('foo.png', '/foo', 'localhost', 'https', 80, 443, 'https://localhost/foo/foo.png'),
|
||||
array('foo.png', '/foo/bar/', 'localhost', 'https', 80, 443, 'https://localhost/foo/bar/foo.png'),
|
||||
array('/foo.png', '/foo', 'localhost', 'http', 443, 80, 'http://localhost:443/foo.png'),
|
||||
array('/foo.png', '/foo', 'localhost', 'https', 443, 80, 'https://localhost:80/foo.png'),
|
||||
);
|
||||
}
|
||||
|
||||
public function testGenerateAbsoluteUrlWithScriptFileName()
|
||||
{
|
||||
$request = Request::create('http://localhost/app/web/app_dev.php');
|
||||
|
@ -127,6 +127,7 @@
|
||||
|
||||
<service id="twig.extension.httpfoundation" class="Symfony\Bridge\Twig\Extension\HttpFoundationExtension" public="false">
|
||||
<argument type="service" id="request_stack" />
|
||||
<argument type="service" id="router.request_context" on-invalid="ignore" />
|
||||
</service>
|
||||
|
||||
<service id="twig.extension.form" class="%twig.extension.form.class%" public="false">
|
||||
|
Reference in New Issue
Block a user