vendor/symfony/framework-bundle/Routing/Router.php line 66

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\FrameworkBundle\Routing;
  11. use Psr\Container\ContainerInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Bundle\FrameworkBundle\DependencyInjection\CompatibilityServiceSubscriberInterface as ServiceSubscriberInterface;
  14. use Symfony\Component\Config\Loader\LoaderInterface;
  15. use Symfony\Component\DependencyInjection\Config\ContainerParametersResource;
  16. use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
  17. use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
  18. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  19. use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
  20. use Symfony\Component\Routing\RequestContext;
  21. use Symfony\Component\Routing\RouteCollection;
  22. use Symfony\Component\Routing\Router as BaseRouter;
  23. /**
  24.  * This Router creates the Loader only when the cache is empty.
  25.  *
  26.  * @author Fabien Potencier <fabien@symfony.com>
  27.  */
  28. class Router extends BaseRouter implements WarmableInterfaceServiceSubscriberInterface
  29. {
  30.     private $container;
  31.     private $collectedParameters = [];
  32.     private $paramFetcher;
  33.     /**
  34.      * @param mixed $resource The main resource to load
  35.      */
  36.     public function __construct(ContainerInterface $container$resource, array $options = [], RequestContext $context nullContainerInterface $parameters nullLoggerInterface $logger nullstring $defaultLocale null)
  37.     {
  38.         $this->container $container;
  39.         $this->resource $resource;
  40.         $this->context $context ?: new RequestContext();
  41.         $this->logger $logger;
  42.         $this->setOptions($options);
  43.         if ($parameters) {
  44.             $this->paramFetcher = [$parameters'get'];
  45.         } elseif ($container instanceof SymfonyContainerInterface) {
  46.             $this->paramFetcher = [$container'getParameter'];
  47.         } else {
  48.             throw new \LogicException(sprintf('You should either pass a "%s" instance or provide the $parameters argument of the "%s" method.'SymfonyContainerInterface::class, __METHOD__));
  49.         }
  50.         $this->defaultLocale $defaultLocale;
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      */
  55.     public function getRouteCollection()
  56.     {
  57.         if (null === $this->collection) {
  58.             $this->collection $this->container->get('routing.loader')->load($this->resource$this->options['resource_type']);
  59.             $this->resolveParameters($this->collection);
  60.             $this->collection->addResource(new ContainerParametersResource($this->collectedParameters));
  61.         }
  62.         return $this->collection;
  63.     }
  64.     /**
  65.      * {@inheritdoc}
  66.      */
  67.     public function warmUp($cacheDir)
  68.     {
  69.         $currentDir $this->getOption('cache_dir');
  70.         // force cache generation
  71.         $this->setOption('cache_dir'$cacheDir);
  72.         $this->getMatcher();
  73.         $this->getGenerator();
  74.         $this->setOption('cache_dir'$currentDir);
  75.     }
  76.     /**
  77.      * Replaces placeholders with service container parameter values in:
  78.      * - the route defaults,
  79.      * - the route requirements,
  80.      * - the route path,
  81.      * - the route host,
  82.      * - the route schemes,
  83.      * - the route methods.
  84.      */
  85.     private function resolveParameters(RouteCollection $collection)
  86.     {
  87.         foreach ($collection as $route) {
  88.             foreach ($route->getDefaults() as $name => $value) {
  89.                 $route->setDefault($name$this->resolve($value));
  90.             }
  91.             foreach ($route->getRequirements() as $name => $value) {
  92.                 $route->setRequirement($name$this->resolve($value));
  93.             }
  94.             $route->setPath($this->resolve($route->getPath()));
  95.             $route->setHost($this->resolve($route->getHost()));
  96.             $schemes = [];
  97.             foreach ($route->getSchemes() as $scheme) {
  98.                 $schemes array_merge($schemesexplode('|'$this->resolve($scheme)));
  99.             }
  100.             $route->setSchemes($schemes);
  101.             $methods = [];
  102.             foreach ($route->getMethods() as $method) {
  103.                 $methods array_merge($methodsexplode('|'$this->resolve($method)));
  104.             }
  105.             $route->setMethods($methods);
  106.             $route->setCondition($this->resolve($route->getCondition()));
  107.         }
  108.     }
  109.     /**
  110.      * Recursively replaces placeholders with the service container parameters.
  111.      *
  112.      * @param mixed $value The source which might contain "%placeholders%"
  113.      *
  114.      * @return mixed The source with the placeholders replaced by the container
  115.      *               parameters. Arrays are resolved recursively.
  116.      *
  117.      * @throws ParameterNotFoundException When a placeholder does not exist as a container parameter
  118.      * @throws RuntimeException           When a container value is not a string or a numeric value
  119.      */
  120.     private function resolve($value)
  121.     {
  122.         if (\is_array($value)) {
  123.             foreach ($value as $key => $val) {
  124.                 $value[$key] = $this->resolve($val);
  125.             }
  126.             return $value;
  127.         }
  128.         if (!\is_string($value)) {
  129.             return $value;
  130.         }
  131.         $escapedValue preg_replace_callback('/%%|%([^%\s]++)%/', function ($match) use ($value) {
  132.             // skip %%
  133.             if (!isset($match[1])) {
  134.                 return '%%';
  135.             }
  136.             if (preg_match('/^env\((?:\w++:)*+\w++\)$/'$match[1])) {
  137.                 throw new RuntimeException(sprintf('Using "%%%s%%" is not allowed in routing configuration.'$match[1]));
  138.             }
  139.             $resolved = ($this->paramFetcher)($match[1]);
  140.             if (\is_bool($resolved)) {
  141.                 $resolved = (string) (int) $resolved;
  142.             }
  143.             if (\is_string($resolved) || is_numeric($resolved)) {
  144.                 $this->collectedParameters[$match[1]] = $resolved;
  145.                 return (string) $this->resolve($resolved);
  146.             }
  147.             throw new RuntimeException(sprintf('The container parameter "%s", used in the route configuration value "%s", must be a string or numeric, but it is of type %s.'$match[1], $value, \gettype($resolved)));
  148.         }, $value);
  149.         return str_replace('%%''%'$escapedValue);
  150.     }
  151.     /**
  152.      * {@inheritdoc}
  153.      */
  154.     public static function getSubscribedServices()
  155.     {
  156.         return [
  157.             'routing.loader' => LoaderInterface::class,
  158.         ];
  159.     }
  160. }