[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
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 use Twig\Extension\AbstractExtension; 16 use Twig\Node\Expression\ArrayExpression; 17 use Twig\Node\Expression\ConstantExpression; 18 use Twig\Node\Node; 19 use Twig\TwigFunction; 20 21 /** 22 * Provides integration of the Routing component with Twig. 23 * 24 * @author Fabien Potencier <fabien@symfony.com> 25 */ 26 class RoutingExtension extends AbstractExtension 27 { 28 private $generator; 29 30 public function __construct(UrlGeneratorInterface $generator) 31 { 32 $this->generator = $generator; 33 } 34 35 /** 36 * {@inheritdoc} 37 */ 38 public function getFunctions() 39 { 40 return [ 41 new TwigFunction('url', [$this, 'getUrl'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]), 42 new TwigFunction('path', [$this, 'getPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]), 43 ]; 44 } 45 46 /** 47 * @param string $name 48 * @param array $parameters 49 * @param bool $relative 50 * 51 * @return string 52 */ 53 public function getPath($name, $parameters = [], $relative = false) 54 { 55 return $this->generator->generate($name, $parameters, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH); 56 } 57 58 /** 59 * @param string $name 60 * @param array $parameters 61 * @param bool $schemeRelative 62 * 63 * @return string 64 */ 65 public function getUrl($name, $parameters = [], $schemeRelative = false) 66 { 67 return $this->generator->generate($name, $parameters, $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL); 68 } 69 70 /** 71 * Determines at compile time whether the generated URL will be safe and thus 72 * saving the unneeded automatic escaping for performance reasons. 73 * 74 * The URL generation process percent encodes non-alphanumeric characters. So there is no risk 75 * that malicious/invalid characters are part of the URL. The only character within an URL that 76 * must be escaped in html is the ampersand ("&") which separates query params. So we cannot mark 77 * the URL generation as always safe, but only when we are sure there won't be multiple query 78 * params. This is the case when there are none or only one constant parameter given. 79 * E.g. we know beforehand this will be safe: 80 * - path('route') 81 * - path('route', {'param': 'value'}) 82 * But the following may not: 83 * - path('route', var) 84 * - path('route', {'param': ['val1', 'val2'] }) // a sub-array 85 * - path('route', {'param1': 'value1', 'param2': 'value2'}) 86 * If param1 and param2 reference placeholder in the route, it would still be safe. But we don't know. 87 * 88 * @param Node $argsNode The arguments of the path/url function 89 * 90 * @return array An array with the contexts the URL is safe 91 * 92 * @final since version 3.4, type-hint to be changed to "\Twig\Node\Node" in 4.0 93 */ 94 public function isUrlGenerationSafe(\Twig_Node $argsNode) 95 { 96 // support named arguments 97 $paramsNode = $argsNode->hasNode('parameters') ? $argsNode->getNode('parameters') : ( 98 $argsNode->hasNode(1) ? $argsNode->getNode(1) : null 99 ); 100 101 if (null === $paramsNode || $paramsNode instanceof ArrayExpression && \count($paramsNode) <= 2 && 102 (!$paramsNode->hasNode(1) || $paramsNode->getNode(1) instanceof ConstantExpression) 103 ) { 104 return ['html']; 105 } 106 107 return []; 108 } 109 110 /** 111 * {@inheritdoc} 112 */ 113 public function getName() 114 { 115 return 'routing'; 116 } 117 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |