Skip to content

Commit

Permalink
CLI: use request context to generate absolute URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Jan 23, 2016
1 parent 15c37c2 commit 766a648
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
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;
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

0 comments on commit 766a648

Please sign in to comment.