diff options
author | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-03 19:26:54 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-03 19:26:54 +0200 |
commit | 4f5b44bd3bd490309eb2ba7b44df4769816ba729 (patch) | |
tree | 6cefe170dfe0a5a361cb1e2d1fc4d580a3316d02 /vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Extension/RoutingExtension.php | |
parent | 2b840e0cfb63a453bea67a98541f3df9c273c5f5 (diff) | |
download | wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.tar.gz wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.tar.zst wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.zip |
twig implementation
Diffstat (limited to 'vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Extension/RoutingExtension.php')
-rw-r--r-- | vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Extension/RoutingExtension.php | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Extension/RoutingExtension.php b/vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Extension/RoutingExtension.php new file mode 100644 index 00000000..8274abf3 --- /dev/null +++ b/vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Extension/RoutingExtension.php | |||
@@ -0,0 +1,100 @@ | |||
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 | |||
14 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
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 | { | ||
23 | private $generator; | ||
24 | |||
25 | public function __construct(UrlGeneratorInterface $generator) | ||
26 | { | ||
27 | $this->generator = $generator; | ||
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( | ||
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'))), | ||
40 | ); | ||
41 | } | ||
42 | |||
43 | public function getPath($name, $parameters = array(), $relative = false) | ||
44 | { | ||
45 | return $this->generator->generate($name, $parameters, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH); | ||
46 | } | ||
47 | |||
48 | public function getUrl($name, $parameters = array(), $schemeRelative = false) | ||
49 | { | ||
50 | return $this->generator->generate($name, $parameters, $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL); | ||
51 | } | ||
52 | |||
53 | /** | ||
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 | /** | ||
92 | * Returns the name of the extension. | ||
93 | * | ||
94 | * @return string The extension name | ||
95 | */ | ||
96 | public function getName() | ||
97 | { | ||
98 | return 'routing'; | ||
99 | } | ||
100 | } | ||