Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 99 lines (86 sloc) 3.561 kB
d2e7ac8 @fabpot moved integration between Routing and Twig to a Symfony Bridge
fabpot authored
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
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
258c33c @fabpot [Routing] changed interface of the router twig extension as we don't …
fabpot authored
14 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
d2e7ac8 @fabpot moved integration between Routing and Twig to a Symfony Bridge
fabpot authored
15
16 /**
17 * Provides integration of the Routing component with Twig.
18 *
19 * @author Fabien Potencier <fabien@symfony.com>
20 */
21 class RoutingExtension extends \Twig_Extension
22 {
258c33c @fabpot [Routing] changed interface of the router twig extension as we don't …
fabpot authored
23 private $generator;
d2e7ac8 @fabpot moved integration between Routing and Twig to a Symfony Bridge
fabpot authored
24
258c33c @fabpot [Routing] changed interface of the router twig extension as we don't …
fabpot authored
25 public function __construct(UrlGeneratorInterface $generator)
d2e7ac8 @fabpot moved integration between Routing and Twig to a Symfony Bridge
fabpot authored
26 {
258c33c @fabpot [Routing] changed interface of the router twig extension as we don't …
fabpot authored
27 $this->generator = $generator;
d2e7ac8 @fabpot moved integration between Routing and Twig to a Symfony Bridge
fabpot authored
28 }
29
30 /**
31 * Returns a list of functions to add to the existing list.
32 *
33 * @return array An array of functions
34 */
35 public function getFunctions()
36 {
37 return array(
e6debba @fabpot Merge branch '2.2' into 2.3
fabpot authored
38 new \Twig_SimpleFunction('url', array($this, 'getUrl'), array('is_safe_callback' => array($this, 'isUrlGenerationSafe'))),
39 new \Twig_SimpleFunction('path', array($this, 'getPath'), array('is_safe_callback' => array($this, 'isUrlGenerationSafe'))),
d2e7ac8 @fabpot moved integration between Routing and Twig to a Symfony Bridge
fabpot authored
40 );
41 }
42
fbfb103 @Tobion [Routing] add support for path-relative and scheme-relative URL gener…
Tobion authored
43 public function getPath($name, $parameters = array(), $relative = false)
d2e7ac8 @fabpot moved integration between Routing and Twig to a Symfony Bridge
fabpot authored
44 {
fbfb103 @Tobion [Routing] add support for path-relative and scheme-relative URL gener…
Tobion authored
45 return $this->generator->generate($name, $parameters, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH);
d2e7ac8 @fabpot moved integration between Routing and Twig to a Symfony Bridge
fabpot authored
46 }
47
fbfb103 @Tobion [Routing] add support for path-relative and scheme-relative URL gener…
Tobion authored
48 public function getUrl($name, $parameters = array(), $schemeRelative = false)
d2e7ac8 @fabpot moved integration between Routing and Twig to a Symfony Bridge
fabpot authored
49 {
fbfb103 @Tobion [Routing] add support for path-relative and scheme-relative URL gener…
Tobion authored
50 return $this->generator->generate($name, $parameters, $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
d2e7ac8 @fabpot moved integration between Routing and Twig to a Symfony Bridge
fabpot authored
51 }
52
53 /**
a6d7edf @Tobion save auto-escaping of generated URLs when possible for performance re…
Tobion authored
54 * Determines at compile time whether the generated URL will be safe and thus
55 * saving the unneeded automatic escaping for performance reasons.
56 *
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:
63 * - path('route')
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.
70 *
71 * @param \Twig_Node $argsNode The arguments of the path/url function
72 *
73 * @return array An array with the contexts the URL is safe
74 */
75 public function isUrlGenerationSafe(\Twig_Node $argsNode)
76 {
77 // support named arguments
78 $paramsNode = $argsNode->hasNode('parameters') ? $argsNode->getNode('parameters') : (
79 $argsNode->hasNode(1) ? $argsNode->getNode(1) : null
80 );
81
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)
84 ) {
85 return array('html');
86 }
87
88 return array();
89 }
90
91 /**
d6beadf @stloyd Fix some phpdocs for Twig extensions & templating helpers
stloyd authored
92 * {@inheritdoc}
d2e7ac8 @fabpot moved integration between Routing and Twig to a Symfony Bridge
fabpot authored
93 */
94 public function getName()
95 {
96 return 'routing';
97 }
98 }
Something went wrong with that request. Please try again.