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 Symfony\Component\Config\Loader\LoaderInterface;
15 use Symfony\Component\Config\ConfigCacheInterface;
16 use Symfony\Component\Config\ConfigCacheFactoryInterface;
17 use Symfony\Component\Config\ConfigCacheFactory;
18 use Psr\Log\LoggerInterface;
19 use Symfony\Component\Routing\Generator\ConfigurableRequirementsInterface;
20 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
21 use Symfony\Component\Routing\Generator\Dumper\GeneratorDumperInterface;
22 use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
23 use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
24 use Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface;
25 use Symfony\Component\HttpFoundation\Request;
26 use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
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();
89 * @param LoaderInterface $loader A LoaderInterface instance
90 * @param mixed $resource The main resource to load
91 * @param array $options An array of options
92 * @param RequestContext $context The context
93 * @param LoggerInterface $logger A logger instance
95 public function __construct(LoaderInterface $loader, $resource, array $options = array(), RequestContext $context = null, LoggerInterface $logger = null)
97 $this->loader = $loader;
98 $this->resource = $resource;
99 $this->logger = $logger;
100 $this->context = $context ?: new RequestContext();
101 $this->setOptions($options);
109 * * cache_dir: The cache directory (or null to disable caching)
110 * * debug: Whether to enable debugging or not (false by default)
111 * * generator_class: The name of a UrlGeneratorInterface implementation
112 * * generator_base_class: The base class for the dumped generator class
113 * * generator_cache_class: The class name for the dumped generator class
114 * * generator_dumper_class: The name of a GeneratorDumperInterface implementation
115 * * matcher_class: The name of a UrlMatcherInterface implementation
116 * * matcher_base_class: The base class for the dumped matcher class
117 * * matcher_dumper_class: The class name for the dumped matcher class
118 * * matcher_cache_class: The name of a MatcherDumperInterface implementation
119 * * resource_type: Type hint for the main resource (optional)
120 * * strict_requirements: Configure strict requirement checking for generators
121 * implementing ConfigurableRequirementsInterface (default is true)
123 * @param array $options An array of options
125 * @throws \InvalidArgumentException When unsupported option is provided
127 public function setOptions(array $options)
129 $this->options = array(
132 'generator_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
133 'generator_base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
134 'generator_dumper_class' => 'Symfony\\Component\\Routing\\Generator\\Dumper\\PhpGeneratorDumper',
135 'generator_cache_class' => 'ProjectUrlGenerator',
136 'matcher_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
137 'matcher_base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
138 'matcher_dumper_class' => 'Symfony\\Component\\Routing\\Matcher\\Dumper\\PhpMatcherDumper',
139 'matcher_cache_class' => 'ProjectUrlMatcher',
140 'resource_type' => null,
141 'strict_requirements' => true,
144 // check option names and live merge, if errors are encountered Exception will be thrown
146 foreach ($options as $key => $value) {
147 if (array_key_exists($key, $this->options)) {
148 $this->options[$key] = $value;
155 throw new \InvalidArgumentException(sprintf('The Router does not support the following options: "%s".', implode('", "', $invalid)));
162 * @param string $key The key
163 * @param mixed $value The value
165 * @throws \InvalidArgumentException
167 public function setOption($key, $value)
169 if (!array_key_exists($key, $this->options)) {
170 throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
173 $this->options[$key] = $value;
177 * Gets an option value.
179 * @param string $key The key
181 * @return mixed The value
183 * @throws \InvalidArgumentException
185 public function getOption($key)
187 if (!array_key_exists($key, $this->options)) {
188 throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
191 return $this->options[$key];
197 public function getRouteCollection()
199 if (null === $this->collection) {
200 $this->collection = $this->loader->load($this->resource, $this->options['resource_type']);
203 return $this->collection;
209 public function setContext(RequestContext $context)
211 $this->context = $context;
213 if (null !== $this->matcher) {
214 $this->getMatcher()->setContext($context);
216 if (null !== $this->generator) {
217 $this->getGenerator()->setContext($context);
224 public function getContext()
226 return $this->context;
230 * Sets the ConfigCache factory to use.
232 * @param ConfigCacheFactoryInterface $configCacheFactory The factory to use
234 public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory)
236 $this->configCacheFactory = $configCacheFactory;
242 public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
244 return $this->getGenerator()->generate($name, $parameters, $referenceType);
250 public function match($pathinfo)
252 return $this->getMatcher()->match($pathinfo);
258 public function matchRequest(Request $request)
260 $matcher = $this->getMatcher();
261 if (!$matcher instanceof RequestMatcherInterface) {
262 // fallback to the default UrlMatcherInterface
263 return $matcher->match($request->getPathInfo());
266 return $matcher->matchRequest($request);
270 * Gets the UrlMatcher instance associated with this Router.
272 * @return UrlMatcherInterface A UrlMatcherInterface instance
274 public function getMatcher()
276 if (null !== $this->matcher) {
277 return $this->matcher;
280 if (null === $this->options['cache_dir'] || null === $this->options['matcher_cache_class']) {
281 $this->matcher = new $this->options['matcher_class']($this->getRouteCollection(), $this->context);
282 if (method_exists($this->matcher, 'addExpressionLanguageProvider')) {
283 foreach ($this->expressionLanguageProviders as $provider) {
284 $this->matcher->addExpressionLanguageProvider($provider);
288 return $this->matcher;
291 $cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$this->options['matcher_cache_class'].'.php',
292 function (ConfigCacheInterface $cache) {
293 $dumper = $this->getMatcherDumperInstance();
294 if (method_exists($dumper, 'addExpressionLanguageProvider')) {
295 foreach ($this->expressionLanguageProviders as $provider) {
296 $dumper->addExpressionLanguageProvider($provider);
301 'class' => $this->options['matcher_cache_class'],
302 'base_class' => $this->options['matcher_base_class'],
305 $cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
309 require_once $cache->getPath();
311 return $this->matcher = new $this->options['matcher_cache_class']($this->context);
315 * Gets the UrlGenerator instance associated with this Router.
317 * @return UrlGeneratorInterface A UrlGeneratorInterface instance
319 public function getGenerator()
321 if (null !== $this->generator) {
322 return $this->generator;
325 if (null === $this->options['cache_dir'] || null === $this->options['generator_cache_class']) {
326 $this->generator = new $this->options['generator_class']($this->getRouteCollection(), $this->context, $this->logger);
328 $cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$this->options['generator_cache_class'].'.php',
329 function (ConfigCacheInterface $cache) {
330 $dumper = $this->getGeneratorDumperInstance();
333 'class' => $this->options['generator_cache_class'],
334 'base_class' => $this->options['generator_base_class'],
337 $cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
341 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;