added an absolute_url() Twig function
This commit is contained in:
parent
ea1ac32815
commit
ee27ed8376
@ -1,6 +1,11 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
2.7.0
|
||||
-----
|
||||
|
||||
* added an HttpFoundation extension (provides the `absolute_url` function)
|
||||
|
||||
2.5.0
|
||||
-----
|
||||
|
||||
|
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Bridge\Twig\Extension;
|
||||
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Asset\Packages;
|
||||
|
||||
/**
|
||||
* Twig extension for the Symfony HttpFoundation component.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class HttpFoundationExtension extends \Twig_Extension
|
||||
{
|
||||
private $requestStack;
|
||||
|
||||
public function __construct(RequestStack $requestStack)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFunctions()
|
||||
{
|
||||
return array(
|
||||
new \Twig_SimpleFunction('absolute_url', array($this, 'generateAbsoluteUrl')),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the absolute URL for the given path.
|
||||
*
|
||||
* This method returns the path unchanged if no request is available.
|
||||
*
|
||||
* @param string $path The path
|
||||
*
|
||||
* @return string The absolute URL
|
||||
*/
|
||||
public function generateAbsoluteUrl($path)
|
||||
{
|
||||
if (false !== strpos($path, '://') || '//' === substr($path, 0, 2)) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
if (!$request = $this->requestStack->getMasterRequest()) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
return $request->getUriForPath($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the extension.
|
||||
*
|
||||
* @return string The extension name
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'request';
|
||||
}
|
||||
}
|
@ -42,5 +42,9 @@ class ExtensionPass implements CompilerPassInterface
|
||||
if ($container->has('fragment.handler')) {
|
||||
$container->getDefinition('twig.extension.httpkernel')->addTag('twig.extension');
|
||||
}
|
||||
|
||||
if ($container->has('request_stack')) {
|
||||
$container->getDefinition('twig.extension.httpfoundation')->addTag('twig.extension');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -103,6 +103,9 @@
|
||||
<argument type="service" id="fragment.handler" />
|
||||
</service>
|
||||
|
||||
<service id="twig.extension.httpfoundation" class="Symfony\Bridge\Twig\Extension\HttpFoundationExtension" public="false">
|
||||
<argument type="service" id="request_stack" />
|
||||
</service>
|
||||
|
||||
<service id="twig.extension.form" class="%twig.extension.form.class%" public="false">
|
||||
<argument type="service" id="twig.form.renderer" />
|
||||
|
Reference in New Issue
Block a user