Router.php 7.3 KB

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