4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Bridge\Twig\Extension
;
14 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
17 * Provides integration of the Routing component with Twig.
19 * @author Fabien Potencier <fabien@symfony.com>
21 class RoutingExtension
extends \Twig_Extension
25 public function __construct(UrlGeneratorInterface
$generator)
27 $this->generator
= $generator;
31 * Returns a list of functions to add to the existing list.
33 * @return array An array of functions
35 public function getFunctions()
38 'url' => new \
Twig_Function_Method($this, 'getUrl', array('is_safe_callback' => array($this, 'isUrlGenerationSafe'))),
39 'path' => new \
Twig_Function_Method($this, 'getPath', array('is_safe_callback' => array($this, 'isUrlGenerationSafe'))),
43 public function getPath($name, $parameters = array(), $relative = false)
45 return $this->generator
->generate($name, $parameters, $relative ? UrlGeneratorInterface
::RELATIVE_PATH
: UrlGeneratorInterface
::ABSOLUTE_PATH
);
48 public function getUrl($name, $parameters = array(), $schemeRelative = false)
50 return $this->generator
->generate($name, $parameters, $schemeRelative ? UrlGeneratorInterface
::NETWORK_PATH
: UrlGeneratorInterface
::ABSOLUTE_URL
);
54 * Determines at compile time whether the generated URL will be safe and thus
55 * saving the unneeded automatic escaping for performance reasons.
57 * The URL generation process percent encodes non-alphanumeric characters. So there is no risk
58 * that malicious/invalid characters are part of the URL. The only character within an URL that
59 * must be escaped in html is the ampersand ("&") which separates query params. So we cannot mark
60 * the URL generation as always safe, but only when we are sure there won't be multiple query
61 * params. This is the case when there are none or only one constant parameter given.
62 * E.g. we know beforehand this will be safe:
64 * - path('route', {'param': 'value'})
65 * But the following may not:
66 * - path('route', var)
67 * - path('route', {'param': ['val1', 'val2'] }) // a sub-array
68 * - path('route', {'param1': 'value1', 'param2': 'value2'})
69 * If param1 and param2 reference placeholder in the route, it would still be safe. But we don't know.
71 * @param \Twig_Node $argsNode The arguments of the path/url function
73 * @return array An array with the contexts the URL is safe
75 public function isUrlGenerationSafe(\Twig_Node
$argsNode)
77 // support named arguments
78 $paramsNode = $argsNode->hasNode('parameters') ? $argsNode->getNode('parameters') : (
79 $argsNode->hasNode(1) ? $argsNode->getNode(1) : null
82 if (null === $paramsNode || $paramsNode instanceof \Twig_Node_Expression_Array
&& count($paramsNode) <= 2 &&
83 (!$paramsNode->hasNode(1) || $paramsNode->getNode(1) instanceof \Twig_Node_Expression_Constant
)
92 * Returns the name of the extension.
94 * @return string The extension name
96 public function getName()