bug #17503 [Asset] CLI: use request context to generate absolute URLs (xabbuh)
This PR was merged into the 2.7 branch.
Discussion
----------
[Asset] CLI: use request context to generate absolute URLs
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #15448
| License | MIT
| Doc PR |
Commits
-------
766a648
CLI: use request context to generate absolute URLs
This commit is contained in:
commit
7afb784e9e
@ -13,6 +13,7 @@ namespace Symfony\Bridge\Twig\Extension;
|
|||||||
|
|
||||||
use Symfony\Component\HttpFoundation\RequestStack;
|
use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\Routing\RequestContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Twig extension for the Symfony HttpFoundation component.
|
* Twig extension for the Symfony HttpFoundation component.
|
||||||
@ -22,10 +23,12 @@ use Symfony\Component\HttpFoundation\Request;
|
|||||||
class HttpFoundationExtension extends \Twig_Extension
|
class HttpFoundationExtension extends \Twig_Extension
|
||||||
{
|
{
|
||||||
private $requestStack;
|
private $requestStack;
|
||||||
|
private $requestContext;
|
||||||
|
|
||||||
public function __construct(RequestStack $requestStack)
|
public function __construct(RequestStack $requestStack, RequestContext $requestContext = null)
|
||||||
{
|
{
|
||||||
$this->requestStack = $requestStack;
|
$this->requestStack = $requestStack;
|
||||||
|
$this->requestContext = $requestContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,6 +60,23 @@ class HttpFoundationExtension extends \Twig_Extension
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!$request = $this->requestStack->getMasterRequest()) {
|
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;
|
return $path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ namespace Symfony\Bridge\Twig\Tests\Extension;
|
|||||||
use Symfony\Bridge\Twig\Extension\HttpFoundationExtension;
|
use Symfony\Bridge\Twig\Extension\HttpFoundationExtension;
|
||||||
use Symfony\Component\HttpFoundation\RequestStack;
|
use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\Routing\RequestContext;
|
||||||
|
|
||||||
class HttpFoundationExtensionTest extends \PHPUnit_Framework_TestCase
|
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()
|
public function testGenerateAbsoluteUrlWithScriptFileName()
|
||||||
{
|
{
|
||||||
$request = Request::create('http://localhost/app/web/app_dev.php');
|
$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">
|
<service id="twig.extension.httpfoundation" class="Symfony\Bridge\Twig\Extension\HttpFoundationExtension" public="false">
|
||||||
<argument type="service" id="request_stack" />
|
<argument type="service" id="request_stack" />
|
||||||
|
<argument type="service" id="router.request_context" on-invalid="ignore" />
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<service id="twig.extension.form" class="%twig.extension.form.class%" public="false">
|
<service id="twig.extension.form" class="%twig.extension.form.class%" public="false">
|
||||||
|
Reference in New Issue
Block a user