Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

RoutingExtension.php

Zuletzt modifiziert: 02.04.2025, 15:03 - Dateigröße: 3.82 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Bridge\Twig\Extension;
013   
014  use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
015  use Twig\Extension\AbstractExtension;
016  use Twig\Node\Expression\ArrayExpression;
017  use Twig\Node\Expression\ConstantExpression;
018  use Twig\Node\Node;
019  use Twig\TwigFunction;
020   
021  /**
022   * Provides integration of the Routing component with Twig.
023   *
024   * @author Fabien Potencier <fabien@symfony.com>
025   */
026  class RoutingExtension extends AbstractExtension
027  {
028      private $generator;
029   
030      public function __construct(UrlGeneratorInterface $generator)
031      {
032          $this->generator = $generator;
033      }
034   
035      /**
036       * {@inheritdoc}
037       */
038      public function getFunctions()
039      {
040          return [
041              new TwigFunction('url', [$this, 'getUrl'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
042              new TwigFunction('path', [$this, 'getPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
043          ];
044      }
045   
046      /**
047       * @param string $name
048       * @param array  $parameters
049       * @param bool   $relative
050       *
051       * @return string
052       */
053      public function getPath($name, $parameters = [], $relative = false)
054      {
055          return $this->generator->generate($name, $parameters, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH);
056      }
057   
058      /**
059       * @param string $name
060       * @param array  $parameters
061       * @param bool   $schemeRelative
062       *
063       * @return string
064       */
065      public function getUrl($name, $parameters = [], $schemeRelative = false)
066      {
067          return $this->generator->generate($name, $parameters, $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
068      }
069   
070      /**
071       * Determines at compile time whether the generated URL will be safe and thus
072       * saving the unneeded automatic escaping for performance reasons.
073       *
074       * The URL generation process percent encodes non-alphanumeric characters. So there is no risk
075       * that malicious/invalid characters are part of the URL. The only character within an URL that
076       * must be escaped in html is the ampersand ("&") which separates query params. So we cannot mark
077       * the URL generation as always safe, but only when we are sure there won't be multiple query
078       * params. This is the case when there are none or only one constant parameter given.
079       * E.g. we know beforehand this will be safe:
080       * - path('route')
081       * - path('route', {'param': 'value'})
082       * But the following may not:
083       * - path('route', var)
084       * - path('route', {'param': ['val1', 'val2'] }) // a sub-array
085       * - path('route', {'param1': 'value1', 'param2': 'value2'})
086       * If param1 and param2 reference placeholder in the route, it would still be safe. But we don't know.
087       *
088       * @param Node $argsNode The arguments of the path/url function
089       *
090       * @return array An array with the contexts the URL is safe
091       *
092       * @final since version 3.4, type-hint to be changed to "\Twig\Node\Node" in 4.0
093       */
094      public function isUrlGenerationSafe(\Twig_Node $argsNode)
095      {
096          // support named arguments
097          $paramsNode = $argsNode->hasNode('parameters') ? $argsNode->getNode('parameters') : (
098              $argsNode->hasNode(1) ? $argsNode->getNode(1) : null
099          );
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  }
118