4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing;
14 use Psr\Log\LoggerInterface;
15 use Symfony\Component\Config\ConfigCacheFactory;
16 use Symfony\Component\Config\ConfigCacheFactoryInterface;
17 use Symfony\Component\Config\ConfigCacheInterface;
18 use Symfony\Component\Config\Loader\LoaderInterface;
19 use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
20 use Symfony\Component\HttpFoundation\Request;
21 use Symfony\Component\Routing\Generator\ConfigurableRequirementsInterface;
22 use Symfony\Component\Routing\Generator\Dumper\GeneratorDumperInterface;
23 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
24 use Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface;
25 use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
26 use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
29 * The Router class is an example of the integration of all pieces of the
30 * routing system for easier use.
32 * @author Fabien Potencier <fabien@symfony.com>
34 class Router implements RouterInterface, RequestMatcherInterface
37 * @var UrlMatcherInterface|null
42 * @var UrlGeneratorInterface|null
52 * @var LoaderInterface
57 * @var RouteCollection|null
59 protected $collection;
69 protected $options = array();
72 * @var LoggerInterface|null
77 * @var ConfigCacheFactoryInterface|null
79 private $configCacheFactory;
82 * @var ExpressionFunctionProviderInterface[]
84 private $expressionLanguageProviders = array();
87 * @param LoaderInterface $loader A LoaderInterface instance
88 * @param mixed $resource The main resource to load
89 * @param array $options An array of options
90 * @param RequestContext $context The context
91 * @param LoggerInterface $logger A logger instance
93 public function __construct(LoaderInterface $loader, $resource, array $options = array(), RequestContext $context = null, LoggerInterface $logger = null)
95 $this->loader = $loader;
96 $this->resource = $resource;
97 $this->logger = $logger;
98 $this->context = $context ?: new RequestContext();
99 $this->setOptions($options);
107 * * cache_dir: The cache directory (or null to disable caching)
108 * * debug: Whether to enable debugging or not (false by default)
109 * * generator_class: The name of a UrlGeneratorInterface implementation
110 * * generator_base_class: The base class for the dumped generator class
111 * * generator_cache_class: The class name for the dumped generator class
112 * * generator_dumper_class: The name of a GeneratorDumperInterface implementation
113 * * matcher_class: The name of a UrlMatcherInterface implementation
114 * * matcher_base_class: The base class for the dumped matcher class
115 * * matcher_dumper_class: The class name for the dumped matcher class
116 * * matcher_cache_class: The name of a MatcherDumperInterface implementation
117 * * resource_type: Type hint for the main resource (optional)
118 * * strict_requirements: Configure strict requirement checking for generators
119 * implementing ConfigurableRequirementsInterface (default is true)
121 * @param array $options An array of options
123 * @throws \InvalidArgumentException When unsupported option is provided
125 public function setOptions(array $options)
127 $this->options = array(
130 'generator_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
131 'generator_base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
132 'generator_dumper_class' => 'Symfony\\Component\\Routing\\Generator\\Dumper\\PhpGeneratorDumper',
133 'generator_cache_class' => 'ProjectUrlGenerator',
134 'matcher_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
135 'matcher_base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
136 'matcher_dumper_class' => 'Symfony\\Component\\Routing\\Matcher\\Dumper\\PhpMatcherDumper',
137 'matcher_cache_class' => 'ProjectUrlMatcher',
138 'resource_type' => null,
139 'strict_requirements' => true,
142 // check option names and live merge, if errors are encountered Exception will be thrown
144 foreach ($options as $key => $value) {
145 if (array_key_exists($key, $this->options)) {
146 $this->options[$key] = $value;
153 throw new \InvalidArgumentException(sprintf('The Router does not support the following options: "%s".', implode('", "', $invalid)));
160 * @param string $key The key
161 * @param mixed $value The value
163 * @throws \InvalidArgumentException
165 public function setOption($key, $value)
167 if (!array_key_exists($key, $this->options)) {
168 throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
171 $this->options[$key] = $value;
175 * Gets an option value.
177 * @param string $key The key
179 * @return mixed The value
181 * @throws \InvalidArgumentException
183 public function getOption($key)
185 if (!array_key_exists($key, $this->options)) {
186 throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
189 return $this->options[$key];
195 public function getRouteCollection()
197 if (null === $this->collection) {
198 $this->collection = $this->loader->load($this->resource, $this->options['resource_type']);
201 return $this->collection;
207 public function setContext(RequestContext $context)
209 $this->context = $context;
211 if (null !== $this->matcher) {
212 $this->getMatcher()->setContext($context);
214 if (null !== $this->generator) {
215 $this->getGenerator()->setContext($context);
222 public function getContext()
224 return $this->context;
228 * Sets the ConfigCache factory to use.
230 public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory)
232 $this->configCacheFactory = $configCacheFactory;
238 public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
240 return $this->getGenerator()->generate($name, $parameters, $referenceType);
246 public function match($pathinfo)
248 return $this->getMatcher()->match($pathinfo);
254 public function matchRequest(Request $request)
256 $matcher = $this->getMatcher();
257 if (!$matcher instanceof RequestMatcherInterface) {
258 // fallback to the default UrlMatcherInterface
259 return $matcher->match($request->getPathInfo());
262 return $matcher->matchRequest($request);
266 * Gets the UrlMatcher instance associated with this Router.
268 * @return UrlMatcherInterface A UrlMatcherInterface instance
270 public function getMatcher()
272 if (null !== $this->matcher) {
273 return $this->matcher;
276 if (null === $this->options['cache_dir'] || null === $this->options['matcher_cache_class']) {
277 $this->matcher = new $this->options['matcher_class']($this->getRouteCollection(), $this->context);
278 if (method_exists($this->matcher, 'addExpressionLanguageProvider')) {
279 foreach ($this->expressionLanguageProviders as $provider) {
280 $this->matcher->addExpressionLanguageProvider($provider);
284 return $this->matcher;
287 $cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$this->options['matcher_cache_class'].'.php',
288 function (ConfigCacheInterface $cache) {
289 $dumper = $this->getMatcherDumperInstance();
290 if (method_exists($dumper, 'addExpressionLanguageProvider')) {
291 foreach ($this->expressionLanguageProviders as $provider) {
292 $dumper->addExpressionLanguageProvider($provider);
297 'class' => $this->options['matcher_cache_class'],
298 'base_class' => $this->options['matcher_base_class'],
301 $cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
305 if (!class_exists($this->options['matcher_cache_class'], false)) {
306 require_once $cache->getPath();
309 return $this->matcher = new $this->options['matcher_cache_class']($this->context);
313 * Gets the UrlGenerator instance associated with this Router.
315 * @return UrlGeneratorInterface A UrlGeneratorInterface instance
317 public function getGenerator()
319 if (null !== $this->generator) {
320 return $this->generator;
323 if (null === $this->options['cache_dir'] || null === $this->options['generator_cache_class']) {
324 $this->generator = new $this->options['generator_class']($this->getRouteCollection(), $this->context, $this->logger);
326 $cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$this->options['generator_cache_class'].'.php',
327 function (ConfigCacheInterface $cache) {
328 $dumper = $this->getGeneratorDumperInstance();
331 'class' => $this->options['generator_cache_class'],
332 'base_class' => $this->options['generator_base_class'],
335 $cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
339 if (!class_exists($this->options['generator_cache_class'], false)) {
340 require_once $cache->getPath();
343 $this->generator = new $this->options['generator_cache_class']($this->context, $this->logger);
346 if ($this->generator instanceof ConfigurableRequirementsInterface) {
347 $this->generator->setStrictRequirements($this->options['strict_requirements']);
350 return $this->generator;
353 public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)
355 $this->expressionLanguageProviders[] = $provider;
359 * @return GeneratorDumperInterface
361 protected function getGeneratorDumperInstance()
363 return new $this->options['generator_dumper_class']($this->getRouteCollection());
367 * @return MatcherDumperInterface
369 protected function getMatcherDumperInstance()
371 return new $this->options['matcher_dumper_class']($this->getRouteCollection());
375 * Provides the ConfigCache factory implementation, falling back to a
376 * default implementation if necessary.
378 * @return ConfigCacheFactoryInterface $configCacheFactory
380 private function getConfigCacheFactory()
382 if (null === $this->configCacheFactory) {
383 $this->configCacheFactory = new ConfigCacheFactory($this->options['debug']);
386 return $this->configCacheFactory;