[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/Extension/ -> RoutingExtension.php (source)

   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       * Returns a list of functions to add to the existing list.
  37       *
  38       * @return array An array of functions
  39       */
  40      public function getFunctions()
  41      {
  42          return array(
  43              new TwigFunction('url', array($this, 'getUrl'), array('is_safe_callback' => array($this, 'isUrlGenerationSafe'))),
  44              new TwigFunction('path', array($this, 'getPath'), array('is_safe_callback' => array($this, 'isUrlGenerationSafe'))),
  45          );
  46      }
  47  
  48      /**
  49       * @param string $name
  50       * @param array  $parameters
  51       * @param bool   $relative
  52       *
  53       * @return string
  54       */
  55      public function getPath($name, $parameters = array(), $relative = false)
  56      {
  57          return $this->generator->generate($name, $parameters, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH);
  58      }
  59  
  60      /**
  61       * @param string $name
  62       * @param array  $parameters
  63       * @param bool   $schemeRelative
  64       *
  65       * @return string
  66       */
  67      public function getUrl($name, $parameters = array(), $schemeRelative = false)
  68      {
  69          return $this->generator->generate($name, $parameters, $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
  70      }
  71  
  72      /**
  73       * Determines at compile time whether the generated URL will be safe and thus
  74       * saving the unneeded automatic escaping for performance reasons.
  75       *
  76       * The URL generation process percent encodes non-alphanumeric characters. So there is no risk
  77       * that malicious/invalid characters are part of the URL. The only character within an URL that
  78       * must be escaped in html is the ampersand ("&") which separates query params. So we cannot mark
  79       * the URL generation as always safe, but only when we are sure there won't be multiple query
  80       * params. This is the case when there are none or only one constant parameter given.
  81       * E.g. we know beforehand this will be safe:
  82       * - path('route')
  83       * - path('route', {'param': 'value'})
  84       * But the following may not:
  85       * - path('route', var)
  86       * - path('route', {'param': ['val1', 'val2'] }) // a sub-array
  87       * - path('route', {'param1': 'value1', 'param2': 'value2'})
  88       * If param1 and param2 reference placeholder in the route, it would still be safe. But we don't know.
  89       *
  90       * @param Node $argsNode The arguments of the path/url function
  91       *
  92       * @return array An array with the contexts the URL is safe
  93       *
  94       * To be made @final in 3.4, and the type-hint be changed to "\Twig\Node\Node" in 4.0.
  95       */
  96      public function isUrlGenerationSafe(\Twig_Node $argsNode)
  97      {
  98          // support named arguments
  99          $paramsNode = $argsNode->hasNode('parameters') ? $argsNode->getNode('parameters') : (
 100              $argsNode->hasNode(1) ? $argsNode->getNode(1) : null
 101          );
 102  
 103          if (null === $paramsNode || $paramsNode instanceof ArrayExpression && \count($paramsNode) <= 2 &&
 104              (!$paramsNode->hasNode(1) || $paramsNode->getNode(1) instanceof ConstantExpression)
 105          ) {
 106              return array('html');
 107          }
 108  
 109          return array();
 110      }
 111  
 112      /**
 113       * {@inheritdoc}
 114       */
 115      public function getName()
 116      {
 117          return 'routing';
 118      }
 119  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1