Skip to content

Commit d2657ec

Browse files
committed
feature #13264 URL manipulations as a Twig extension (fabpot)
This PR was merged into the 2.7 branch. Discussion ---------- URL manipulations as a Twig extension | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | symfony/symfony-docs#4805 While working on the new asset component, I realized that the "absolute URL" feature was misplaced and would benefit from being exposed as a Twig function (composition is always a good thing). Then, I wondered if having a Twig function to generate a relative path (like done by the Routing component would also make sense). And here is the corresponding PR. ```jinja {# generate an absolute URL for the given absolute path #} {{ absolute_url('/me.png') }} {# generate a relative path for the given absolute path (based on the current Request) #} {{ relative_path('/foo/me.png') }} {# compose as you see fit #} {{ absolute_url(asset('me.png')) }} ``` As you can see, we require an absolute path for both functions (and we even add the leading slash if it is omitted), not sure if we want to do otherwise. ping @Tobion Commits ------- 0ec852d added a relative_path Twig function ee27ed8 added an absolute_url() Twig function
2 parents 5d56459 + 8fa9050 commit d2657ec

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
2.7.0
5+
-----
6+
7+
* added an HttpFoundation extension (provides the `absolute_url` and the `relative_path` functions)
8+
49
2.5.0
510
-----
611

Extension/HttpFoundationExtension.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Extension;
13+
14+
use Symfony\Component\HttpFoundation\RequestStack;
15+
use Symfony\Component\HttpFoundation\Request;
16+
17+
/**
18+
* Twig extension for the Symfony HttpFoundation component.
19+
*
20+
* @author Fabien Potencier <[email protected]>
21+
*/
22+
class HttpFoundationExtension extends \Twig_Extension
23+
{
24+
private $requestStack;
25+
26+
public function __construct(RequestStack $requestStack)
27+
{
28+
$this->requestStack = $requestStack;
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function getFunctions()
35+
{
36+
return array(
37+
new \Twig_SimpleFunction('absolute_url', array($this, 'generateAbsoluteUrl')),
38+
new \Twig_SimpleFunction('relative_path', array($this, 'generateRelativePath')),
39+
);
40+
}
41+
42+
/**
43+
* Returns the absolute URL for the given absolute or relative path.
44+
*
45+
* This method returns the path unchanged if no request is available.
46+
*
47+
* @param string $path The path
48+
*
49+
* @return string The absolute URL
50+
*
51+
* @see Request::getUriForPath()
52+
*/
53+
public function generateAbsoluteUrl($path)
54+
{
55+
if (false !== strpos($path, '://') || '//' === substr($path, 0, 2)) {
56+
return $path;
57+
}
58+
59+
if (!$request = $this->requestStack->getMasterRequest()) {
60+
return $path;
61+
}
62+
63+
if (!$path || '/' !== $path[0]) {
64+
$prefix = $request->getPathInfo();
65+
$last = strlen($prefix) - 1;
66+
if ($last !== $pos = strrpos($prefix, '/')) {
67+
$prefix = substr($prefix, 0, $pos).'/';
68+
}
69+
70+
$path = $prefix.$path;
71+
}
72+
73+
return $request->getUriForPath($path);
74+
}
75+
76+
/**
77+
* Returns a relative path based on the current Request.
78+
*
79+
* This method returns the path unchanged if no request is available.
80+
*
81+
* @param string $path The path
82+
*
83+
* @return string The relative path
84+
*
85+
* @see Request::getRelativeUriForPath()
86+
*/
87+
public function generateRelativePath($path)
88+
{
89+
if (false !== strpos($path, '://') || '//' === substr($path, 0, 2)) {
90+
return $path;
91+
}
92+
93+
if (!$request = $this->requestStack->getMasterRequest()) {
94+
return $path;
95+
}
96+
97+
return $request->getRelativeUriForPath($path);
98+
}
99+
100+
/**
101+
* Returns the name of the extension.
102+
*
103+
* @return string The extension name
104+
*/
105+
public function getName()
106+
{
107+
return 'request';
108+
}
109+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Tests\Extension;
13+
14+
use Symfony\Bridge\Twig\Extension\HttpFoundationExtension;
15+
use Symfony\Component\HttpFoundation\RequestStack;
16+
use Symfony\Component\HttpFoundation\Request;
17+
18+
class HttpFoundationExtensionTest extends \PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* @dataProvider getGenerateAbsoluteUrlData()
22+
*/
23+
public function testGenerateAbsoluteUrl($expected, $path, $pathinfo)
24+
{
25+
$stack = new RequestStack();
26+
$stack->push(Request::create($pathinfo));
27+
$extension = new HttpFoundationExtension($stack);
28+
29+
$this->assertEquals($expected, $extension->generateAbsoluteUrl($path));
30+
}
31+
32+
public function getGenerateAbsoluteUrlData()
33+
{
34+
return array(
35+
array('http://localhost/foo.png', '/foo.png', '/foo/bar.html'),
36+
array('http://localhost/foo/foo.png', 'foo.png', '/foo/bar.html'),
37+
array('http://localhost/foo/foo.png', 'foo.png', '/foo/bar'),
38+
array('http://localhost/foo/bar/foo.png', 'foo.png', '/foo/bar/'),
39+
40+
array('http://example.com/baz', 'http://example.com/baz', '/'),
41+
array('https://example.com/baz', 'https://example.com/baz', '/'),
42+
array('//example.com/baz', '//example.com/baz', '/'),
43+
);
44+
}
45+
46+
/**
47+
* @dataProvider getGenerateRelativePathData()
48+
*/
49+
public function testGenerateRelativePath($expected, $path, $pathinfo)
50+
{
51+
if (!method_exists('Symfony\Component\HttpFoundation\Request', 'getRelativeUriForPath')) {
52+
$this->markTestSkipped('Your version of Symfony HttpFoundation is too old.');
53+
}
54+
55+
$stack = new RequestStack();
56+
$stack->push(Request::create($pathinfo));
57+
$extension = new HttpFoundationExtension($stack);
58+
59+
$this->assertEquals($expected, $extension->generateRelativePath($path));
60+
}
61+
62+
public function getGenerateRelativePathData()
63+
{
64+
return array(
65+
array('../foo.png', '/foo.png', '/foo/bar.html'),
66+
array('../baz/foo.png', '/baz/foo.png', '/foo/bar.html'),
67+
array('baz/foo.png', 'baz/foo.png', '/foo/bar.html'),
68+
69+
array('http://example.com/baz', 'http://example.com/baz', '/'),
70+
array('https://example.com/baz', 'https://example.com/baz', '/'),
71+
array('//example.com/baz', '//example.com/baz', '/'),
72+
);
73+
}
74+
}

0 commit comments

Comments
 (0)