PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/routing/Router.php

https://gitlab.com/Urtekin/ertexAdmin
PHP | 378 lines | 196 code | 52 blank | 130 comment | 20 complexity | 5c040d97062e08b2d3138f8e4f33f55d 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. $cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$this->options['matcher_cache_class'].'.php',
  247. function (ConfigCacheInterface $cache) {
  248. $dumper = $this->getMatcherDumperInstance();
  249. if (method_exists($dumper, 'addExpressionLanguageProvider')) {
  250. foreach ($this->expressionLanguageProviders as $provider) {
  251. $dumper->addExpressionLanguageProvider($provider);
  252. }
  253. }
  254. $options = array(
  255. 'class' => $this->options['matcher_cache_class'],
  256. 'base_class' => $this->options['matcher_base_class'],
  257. );
  258. $cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
  259. }
  260. );
  261. require_once $cache->getPath();
  262. return $this->matcher = new $this->options['matcher_cache_class']($this->context);
  263. }
  264. /**
  265. * Gets the UrlGenerator instance associated with this Router.
  266. *
  267. * @return UrlGeneratorInterface A UrlGeneratorInterface instance
  268. */
  269. public function getGenerator()
  270. {
  271. if (null !== $this->generator) {
  272. return $this->generator;
  273. }
  274. if (null === $this->options['cache_dir'] || null === $this->options['generator_cache_class']) {
  275. $this->generator = new $this->options['generator_class']($this->getRouteCollection(), $this->context, $this->logger);
  276. } else {
  277. $cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$this->options['generator_cache_class'].'.php',
  278. function (ConfigCacheInterface $cache) {
  279. $dumper = $this->getGeneratorDumperInstance();
  280. $options = array(
  281. 'class' => $this->options['generator_cache_class'],
  282. 'base_class' => $this->options['generator_base_class'],
  283. );
  284. $cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
  285. }
  286. );
  287. require_once $cache->getPath();
  288. $this->generator = new $this->options['generator_cache_class']($this->context, $this->logger);
  289. }
  290. if ($this->generator instanceof ConfigurableRequirementsInterface) {
  291. $this->generator->setStrictRequirements($this->options['strict_requirements']);
  292. }
  293. return $this->generator;
  294. }
  295. public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)
  296. {
  297. $this->expressionLanguageProviders[] = $provider;
  298. }
  299. /**
  300. * @return GeneratorDumperInterface
  301. */
  302. protected function getGeneratorDumperInstance()
  303. {
  304. return new $this->options['generator_dumper_class']($this->getRouteCollection());
  305. }
  306. /**
  307. * @return MatcherDumperInterface
  308. */
  309. protected function getMatcherDumperInstance()
  310. {
  311. return new $this->options['matcher_dumper_class']($this->getRouteCollection());
  312. }
  313. /**
  314. * Provides the ConfigCache factory implementation, falling back to a
  315. * default implementation if necessary.
  316. *
  317. * @return ConfigCacheFactoryInterface $configCacheFactory
  318. */
  319. private function getConfigCacheFactory()
  320. {
  321. if (null === $this->configCacheFactory) {
  322. $this->configCacheFactory = new ConfigCacheFactory($this->options['debug']);
  323. }
  324. return $this->configCacheFactory;
  325. }
  326. }