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

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