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\ConfigCache
;
16 use Psr\Log\LoggerInterface
;
17 use Symfony\Component\Routing\Generator\ConfigurableRequirementsInterface
;
18 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
19 use Symfony\Component\Routing\Matcher\UrlMatcherInterface
;
22 * The Router class is an example of the integration of all pieces of the
23 * routing system for easier use.
25 * @author Fabien Potencier <fabien@symfony.com>
27 class Router
implements RouterInterface
30 * @var UrlMatcherInterface|null
35 * @var UrlGeneratorInterface|null
45 * @var LoaderInterface
50 * @var RouteCollection|null
52 protected $collection;
62 protected $options = array();
65 * @var LoggerInterface|null
72 * @param LoaderInterface $loader A LoaderInterface instance
73 * @param mixed $resource The main resource to load
74 * @param array $options An array of options
75 * @param RequestContext $context The context
76 * @param LoggerInterface $logger A logger instance
78 public function __construct(LoaderInterface
$loader, $resource, array $options = array(), RequestContext
$context = null, LoggerInterface
$logger = null)
80 $this->loader
= $loader;
81 $this->resource = $resource;
82 $this->logger
= $logger;
83 $this->context
= null === $context ? new RequestContext() : $context;
84 $this->setOptions($options);
92 * * cache_dir: The cache directory (or null to disable caching)
93 * * debug: Whether to enable debugging or not (false by default)
94 * * resource_type: Type hint for the main resource (optional)
96 * @param array $options An array of options
98 * @throws \InvalidArgumentException When unsupported option is provided
100 public function setOptions(array $options)
102 $this->options
= array(
105 'generator_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
106 'generator_base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
107 'generator_dumper_class' => 'Symfony\\Component\\Routing\\Generator\\Dumper\\PhpGeneratorDumper',
108 'generator_cache_class' => 'ProjectUrlGenerator',
109 'matcher_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
110 'matcher_base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
111 'matcher_dumper_class' => 'Symfony\\Component\\Routing\\Matcher\\Dumper\\PhpMatcherDumper',
112 'matcher_cache_class' => 'ProjectUrlMatcher',
113 'resource_type' => null,
114 'strict_requirements' => true,
117 // check option names and live merge, if errors are encountered Exception will be thrown
119 foreach ($options as $key => $value) {
120 if (array_key_exists($key, $this->options
)) {
121 $this->options
[$key] = $value;
128 throw new \
InvalidArgumentException(sprintf('The Router does not support the following options: "%s".', implode('", "', $invalid)));
135 * @param string $key The key
136 * @param mixed $value The value
138 * @throws \InvalidArgumentException
140 public function setOption($key, $value)
142 if (!array_key_exists($key, $this->options
)) {
143 throw new \
InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
146 $this->options
[$key] = $value;
150 * Gets an option value.
152 * @param string $key The key
154 * @return mixed The value
156 * @throws \InvalidArgumentException
158 public function getOption($key)
160 if (!array_key_exists($key, $this->options
)) {
161 throw new \
InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
164 return $this->options
[$key];
170 public function getRouteCollection()
172 if (null === $this->collection
) {
173 $this->collection
= $this->loader
->load($this->resource, $this->options
['resource_type']);
176 return $this->collection
;
182 public function setContext(RequestContext
$context)
184 $this->context
= $context;
186 if (null !== $this->matcher
) {
187 $this->getMatcher()->setContext($context);
189 if (null !== $this->generator
) {
190 $this->getGenerator()->setContext($context);
197 public function getContext()
199 return $this->context
;
205 public function generate($name, $parameters = array(), $referenceType = self
::ABSOLUTE_PATH
)
207 return $this->getGenerator()->generate($name, $parameters, $referenceType);
213 public function match($pathinfo)
215 return $this->getMatcher()->match($pathinfo);
219 * Gets the UrlMatcher instance associated with this Router.
221 * @return UrlMatcherInterface A UrlMatcherInterface instance
223 public function getMatcher()
225 if (null !== $this->matcher
) {
226 return $this->matcher
;
229 if (null === $this->options
['cache_dir'] || null === $this->options
['matcher_cache_class']) {
230 return $this->matcher
= new $this->options
['matcher_class']($this->getRouteCollection(), $this->context
);
233 $class = $this->options
['matcher_cache_class'];
234 $cache = new ConfigCache($this->options
['cache_dir'].'/'.$class.'.php', $this->options
['debug']);
235 if (!$cache->isFresh($class)) {
236 $dumper = new $this->options
['matcher_dumper_class']($this->getRouteCollection());
240 'base_class' => $this->options
['matcher_base_class'],
243 $cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
248 return $this->matcher
= new $class($this->context
);
252 * Gets the UrlGenerator instance associated with this Router.
254 * @return UrlGeneratorInterface A UrlGeneratorInterface instance
256 public function getGenerator()
258 if (null !== $this->generator
) {
259 return $this->generator
;
262 if (null === $this->options
['cache_dir'] || null === $this->options
['generator_cache_class']) {
263 $this->generator
= new $this->options
['generator_class']($this->getRouteCollection(), $this->context
, $this->logger
);
265 $class = $this->options
['generator_cache_class'];
266 $cache = new ConfigCache($this->options
['cache_dir'].'/'.$class.'.php', $this->options
['debug']);
267 if (!$cache->isFresh($class)) {
268 $dumper = new $this->options
['generator_dumper_class']($this->getRouteCollection());
272 'base_class' => $this->options
['generator_base_class'],
275 $cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
280 $this->generator
= new $class($this->context
, $this->logger
);
283 if ($this->generator
instanceof ConfigurableRequirementsInterface
) {
284 $this->generator
->setStrictRequirements($this->options
['strict_requirements']);
287 return $this->generator
;