PageRenderTime 33ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/routing/Router.php

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 394 lines | 203 code | 53 blank | 138 comment | 20 complexity | b8c637e6bfde7c2c84d99970f65f8bd7 MD5 | raw file
  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\Component\Routing;
  11. use Symfony\Component\Config\Loader\LoaderInterface;
  12. use Symfony\Component\Config\ConfigCacheInterface;
  13. use Symfony\Component\Config\ConfigCacheFactoryInterface;
  14. use Symfony\Component\Config\ConfigCacheFactory;
  15. use Psr\Log\LoggerInterface;
  16. use Symfony\Component\Routing\Generator\ConfigurableRequirementsInterface;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. use Symfony\Component\Routing\Generator\Dumper\GeneratorDumperInterface;
  19. use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
  20. use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
  21. use Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
  24. /**
  25. * The Router class is an example of the integration of all pieces of the
  26. * routing system for easier use.
  27. *
  28. * @author Fabien Potencier <fabien@symfony.com>
  29. */
  30. class Router implements RouterInterface, RequestMatcherInterface
  31. {
  32. /**
  33. * @var UrlMatcherInterface|null
  34. */
  35. protected $matcher;
  36. /**
  37. * @var UrlGeneratorInterface|null
  38. */
  39. protected $generator;
  40. /**
  41. * @var RequestContext
  42. */
  43. protected $context;
  44. /**
  45. * @var LoaderInterface
  46. */
  47. protected $loader;
  48. /**
  49. * @var RouteCollection|null
  50. */
  51. protected $collection;
  52. /**
  53. * @var mixed
  54. */
  55. protected $resource;
  56. /**
  57. * @var array
  58. */
  59. protected $options = array();
  60. /**
  61. * @var LoggerInterface|null
  62. */
  63. protected $logger;
  64. /**
  65. * @var ConfigCacheFactoryInterface|null
  66. */
  67. private $configCacheFactory;
  68. /**
  69. * @var ExpressionFunctionProviderInterface[]
  70. */
  71. private $expressionLanguageProviders = array();
  72. /**
  73. * Constructor.
  74. *
  75. * @param LoaderInterface $loader A LoaderInterface instance
  76. * @param mixed $resource The main resource to load
  77. * @param array $options An array of options
  78. * @param RequestContext $context The context
  79. * @param LoggerInterface $logger A logger instance
  80. */
  81. public function __construct(LoaderInterface $loader, $resource, array $options = array(), RequestContext $context = null, LoggerInterface $logger = null)
  82. {
  83. $this->loader = $loader;
  84. $this->resource = $resource;
  85. $this->logger = $logger;
  86. $this->context = $context ?: new RequestContext();
  87. $this->setOptions($options);
  88. }
  89. /**
  90. * Sets options.
  91. *
  92. * Available options:
  93. *
  94. * * cache_dir: The cache directory (or null to disable caching)
  95. * * debug: Whether to enable debugging or not (false by default)
  96. * * resource_type: Type hint for the main resource (optional)
  97. *
  98. * @param array $options An array of options
  99. *
  100. * @throws \InvalidArgumentException When unsupported option is provided
  101. */
  102. public function setOptions(array $options)
  103. {
  104. $this->options = array(
  105. 'cache_dir' => null,
  106. 'debug' => false,
  107. 'generator_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
  108. 'generator_base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
  109. 'generator_dumper_class' => 'Symfony\\Component\\Routing\\Generator\\Dumper\\PhpGeneratorDumper',
  110. 'generator_cache_class' => 'ProjectUrlGenerator',
  111. 'matcher_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
  112. 'matcher_base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
  113. 'matcher_dumper_class' => 'Symfony\\Component\\Routing\\Matcher\\Dumper\\PhpMatcherDumper',
  114. 'matcher_cache_class' => 'ProjectUrlMatcher',
  115. 'resource_type' => null,
  116. 'strict_requirements' => true,
  117. );
  118. // check option names and live merge, if errors are encountered Exception will be thrown
  119. $invalid = array();
  120. foreach ($options as $key => $value) {
  121. if (array_key_exists($key, $this->options)) {
  122. $this->options[$key] = $value;
  123. } else {
  124. $invalid[] = $key;
  125. }
  126. }
  127. if ($invalid) {
  128. throw new \InvalidArgumentException(sprintf('The Router does not support the following options: "%s".', implode('", "', $invalid)));
  129. }
  130. }
  131. /**
  132. * Sets an option.
  133. *
  134. * @param string $key The key
  135. * @param mixed $value The value
  136. *
  137. * @throws \InvalidArgumentException
  138. */
  139. public function setOption($key, $value)
  140. {
  141. if (!array_key_exists($key, $this->options)) {
  142. throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
  143. }
  144. $this->options[$key] = $value;
  145. }
  146. /**
  147. * Gets an option value.
  148. *
  149. * @param string $key The key
  150. *
  151. * @return mixed The value
  152. *
  153. * @throws \InvalidArgumentException
  154. */
  155. public function getOption($key)
  156. {
  157. if (!array_key_exists($key, $this->options)) {
  158. throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
  159. }
  160. return $this->options[$key];
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function getRouteCollection()
  166. {
  167. if (null === $this->collection) {
  168. $this->collection = $this->loader->load($this->resource, $this->options['resource_type']);
  169. }
  170. return $this->collection;
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function setContext(RequestContext $context)
  176. {
  177. $this->context = $context;
  178. if (null !== $this->matcher) {
  179. $this->getMatcher()->setContext($context);
  180. }
  181. if (null !== $this->generator) {
  182. $this->getGenerator()->setContext($context);
  183. }
  184. }
  185. /**
  186. * {@inheritdoc}
  187. */
  188. public function getContext()
  189. {
  190. return $this->context;
  191. }
  192. /**
  193. * Sets the ConfigCache factory to use.
  194. *
  195. * @param ConfigCacheFactoryInterface $configCacheFactory The factory to use.
  196. */
  197. public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory)
  198. {
  199. $this->configCacheFactory = $configCacheFactory;
  200. }
  201. /**
  202. * {@inheritdoc}
  203. */
  204. public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
  205. {
  206. return $this->getGenerator()->generate($name, $parameters, $referenceType);
  207. }
  208. /**
  209. * {@inheritdoc}
  210. */
  211. public function match($pathinfo)
  212. {
  213. return $this->getMatcher()->match($pathinfo);
  214. }
  215. /**
  216. * {@inheritdoc}
  217. */
  218. public function matchRequest(Request $request)
  219. {
  220. $matcher = $this->getMatcher();
  221. if (!$matcher instanceof RequestMatcherInterface) {
  222. // fallback to the default UrlMatcherInterface
  223. return $matcher->match($request->getPathInfo());
  224. }
  225. return $matcher->matchRequest($request);
  226. }
  227. /**
  228. * Gets the UrlMatcher instance associated with this Router.
  229. *
  230. * @return UrlMatcherInterface A UrlMatcherInterface instance
  231. */
  232. public function getMatcher()
  233. {
  234. if (null !== $this->matcher) {
  235. return $this->matcher;
  236. }
  237. if (null === $this->options['cache_dir'] || null === $this->options['matcher_cache_class']) {
  238. $this->matcher = new $this->options['matcher_class']($this->getRouteCollection(), $this->context);
  239. if (method_exists($this->matcher, 'addExpressionLanguageProvider')) {
  240. foreach ($this->expressionLanguageProviders as $provider) {
  241. $this->matcher->addExpressionLanguageProvider($provider);
  242. }
  243. }
  244. return $this->matcher;
  245. }
  246. $class = $this->options['matcher_cache_class'];
  247. $baseClass = $this->options['matcher_base_class'];
  248. $expressionLanguageProviders = $this->expressionLanguageProviders;
  249. $that = $this; // required for PHP 5.3 where "$this" cannot be use()d in anonymous functions. Change in Symfony 3.0.
  250. $cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$class.'.php',
  251. function (ConfigCacheInterface $cache) use ($that, $class, $baseClass, $expressionLanguageProviders) {
  252. $dumper = $that->getMatcherDumperInstance();
  253. if (method_exists($dumper, 'addExpressionLanguageProvider')) {
  254. foreach ($expressionLanguageProviders as $provider) {
  255. $dumper->addExpressionLanguageProvider($provider);
  256. }
  257. }
  258. $options = array(
  259. 'class' => $class,
  260. 'base_class' => $baseClass,
  261. );
  262. $cache->write($dumper->dump($options), $that->getRouteCollection()->getResources());
  263. }
  264. );
  265. require_once $cache->getPath();
  266. return $this->matcher = new $class($this->context);
  267. }
  268. /**
  269. * Gets the UrlGenerator instance associated with this Router.
  270. *
  271. * @return UrlGeneratorInterface A UrlGeneratorInterface instance
  272. */
  273. public function getGenerator()
  274. {
  275. if (null !== $this->generator) {
  276. return $this->generator;
  277. }
  278. if (null === $this->options['cache_dir'] || null === $this->options['generator_cache_class']) {
  279. $this->generator = new $this->options['generator_class']($this->getRouteCollection(), $this->context, $this->logger);
  280. } else {
  281. $class = $this->options['generator_cache_class'];
  282. $baseClass = $this->options['generator_base_class'];
  283. $that = $this; // required for PHP 5.3 where "$this" cannot be use()d in anonymous functions. Change in Symfony 3.0.
  284. $cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$class.'.php',
  285. function (ConfigCacheInterface $cache) use ($that, $class, $baseClass) {
  286. $dumper = $that->getGeneratorDumperInstance();
  287. $options = array(
  288. 'class' => $class,
  289. 'base_class' => $baseClass,
  290. );
  291. $cache->write($dumper->dump($options), $that->getRouteCollection()->getResources());
  292. }
  293. );
  294. require_once $cache->getPath();
  295. $this->generator = new $class($this->context, $this->logger);
  296. }
  297. if ($this->generator instanceof ConfigurableRequirementsInterface) {
  298. $this->generator->setStrictRequirements($this->options['strict_requirements']);
  299. }
  300. return $this->generator;
  301. }
  302. public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)
  303. {
  304. $this->expressionLanguageProviders[] = $provider;
  305. }
  306. /**
  307. * This method is public because it needs to be callable from a closure in PHP 5.3. It should be converted back to protected in 3.0.
  308. *
  309. * @internal
  310. *
  311. * @return GeneratorDumperInterface
  312. */
  313. public function getGeneratorDumperInstance()
  314. {
  315. return new $this->options['generator_dumper_class']($this->getRouteCollection());
  316. }
  317. /**
  318. * This method is public because it needs to be callable from a closure in PHP 5.3. It should be converted back to protected in 3.0.
  319. *
  320. * @internal
  321. *
  322. * @return MatcherDumperInterface
  323. */
  324. public function getMatcherDumperInstance()
  325. {
  326. return new $this->options['matcher_dumper_class']($this->getRouteCollection());
  327. }
  328. /**
  329. * Provides the ConfigCache factory implementation, falling back to a
  330. * default implementation if necessary.
  331. *
  332. * @return ConfigCacheFactoryInterface $configCacheFactory
  333. */
  334. private function getConfigCacheFactory()
  335. {
  336. if (null === $this->configCacheFactory) {
  337. $this->configCacheFactory = new ConfigCacheFactory($this->options['debug']);
  338. }
  339. return $this->configCacheFactory;
  340. }
  341. }