Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Asset] CLI: use request context to generate absolute URLs #17503

Merged
merged 1 commit into from Jan 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/Symfony/Bridge/Twig/Extension/HttpFoundationExtension.php
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Request;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be kept (there is a reference in a @see annotation)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, PhpStorm doesn't seem to handle that properly. I changed the annotation to use the FQCN instead. Okay for you?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we are using short class names everywhere else.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, I reverted this one

use Symfony\Component\Routing\RequestContext;

/**
* Twig extension for the Symfony HttpFoundation component.
Expand All @@ -22,10 +23,12 @@
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;
}

/**
Expand Down Expand Up @@ -57,6 +60,23 @@ public function generateAbsoluteUrl($path)
}

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;
}

Expand Down
Expand Up @@ -14,6 +14,7 @@
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
{
Expand Down Expand Up @@ -43,6 +44,49 @@ public function getGenerateAbsoluteUrlData()
);
}

/**
* @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');
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Expand Up @@ -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">
Expand Down