123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Routing;
- use Symfony\Component\Config\Loader\LoaderInterface;
- use Symfony\Component\Config\ConfigCache;
- /**
- * The Router class is an example of the integration of all pieces of the
- * routing system for easier use.
- *
- * @author Fabien Potencier <fabien@symfony.com>
- */
- class Router implements RouterInterface
- {
- private $matcher;
- private $generator;
- private $defaults;
- private $context;
- private $loader;
- private $collection;
- private $resource;
- protected $options;
- /**
- * Constructor.
- *
- * Available options:
- *
- * * cache_dir: The cache directory (or null to disable caching)
- * * debug: Whether to enable debugging or not (false by default)
- * * resource_type: Type hint for the main resource (optional)
- *
- * @param LoaderInterface $loader A LoaderInterface instance
- * @param mixed $resource The main resource to load
- * @param array $options An array of options
- * @param RequestContext $context The context
- * @param array $defaults The default values
- *
- * @throws \InvalidArgumentException When unsupported option is provided
- */
- public function __construct(LoaderInterface $loader, $resource, array $options = array(), RequestContext $context = null, array $defaults = array())
- {
- $this->loader = $loader;
- $this->resource = $resource;
- $this->context = null === $context ? new RequestContext() : $context;
- $this->defaults = $defaults;
- $this->options = array(
- 'cache_dir' => null,
- 'debug' => false,
- 'generator_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
- 'generator_base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
- 'generator_dumper_class' => 'Symfony\\Component\\Routing\\Generator\\Dumper\\PhpGeneratorDumper',
- 'generator_cache_class' => 'ProjectUrlGenerator',
- 'matcher_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
- 'matcher_base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
- 'matcher_dumper_class' => 'Symfony\\Component\\Routing\\Matcher\\Dumper\\PhpMatcherDumper',
- 'matcher_cache_class' => 'ProjectUrlMatcher',
- 'resource_type' => null,
- );
- // check option names and live merge, if errors are encountered Exception will be thrown
- $invalid = array();
- $isInvalid = false;
- foreach ($options as $key => $value) {
- if (array_key_exists($key, $this->options)) {
- $this->options[$key] = $value;
- } else {
- $isInvalid = true;
- $invalid[] = $key;
- }
- }
- if ($isInvalid) {
- throw new \InvalidArgumentException(sprintf('The Router does not support the following options: \'%s\'.', implode('\', \'', $invalid)));
- }
- }
- /**
- * Gets the RouteCollection instance associated with this Router.
- *
- * @return RouteCollection A RouteCollection instance
- */
- public function getRouteCollection()
- {
- if (null === $this->collection) {
- $this->collection = $this->loader->load($this->resource, $this->options['resource_type']);
- }
- return $this->collection;
- }
- /**
- * Sets the request context.
- *
- * @param RequestContext $context The context
- */
- public function setContext(RequestContext $context)
- {
- $this->context = $context;
- $this->getMatcher()->setContext($context);
- $this->getGenerator()->setContext($context);
- }
- /**
- * Gets the request context.
- *
- * @return RequestContext The context
- */
- public function getContext()
- {
- return $this->context;
- }
- /**
- * Generates a URL from the given parameters.
- *
- * @param string $name The name of the route
- * @param array $parameters An array of parameters
- * @param Boolean $absolute Whether to generate an absolute URL
- *
- * @return string The generated URL
- */
- public function generate($name, array $parameters = array(), $absolute = false)
- {
- return $this->getGenerator()->generate($name, $parameters, $absolute);
- }
- /**
- * Tries to match a URL with a set of routes.
- *
- * Returns false if no route matches the URL.
- *
- * @param string $url URL to be parsed
- *
- * @return array|false An array of parameters or false if no route matches
- */
- public function match($url)
- {
- return $this->getMatcher()->match($url);
- }
- /**
- * Gets the UrlMatcher instance associated with this Router.
- *
- * @return UrlMatcherInterface A UrlMatcherInterface instance
- */
- public function getMatcher()
- {
- if (null !== $this->matcher) {
- return $this->matcher;
- }
- if (null === $this->options['cache_dir'] || null === $this->options['matcher_cache_class']) {
- return $this->matcher = new $this->options['matcher_class']($this->getRouteCollection(), $this->context, $this->defaults);
- }
- $class = $this->options['matcher_cache_class'];
- $cache = new ConfigCache($this->options['cache_dir'].'/'.$class.'.php', $this->options['debug']);
- if (!$cache->isFresh($class)) {
- $dumper = new $this->options['matcher_dumper_class']($this->getRouteCollection());
- $options = array(
- 'class' => $class,
- 'base_class' => $this->options['matcher_base_class'],
- );
- $cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
- }
- require_once $cache;
- return $this->matcher = new $class($this->context, $this->defaults);
- }
- /**
- * Gets the UrlGenerator instance associated with this Router.
- *
- * @return UrlGeneratorInterface A UrlGeneratorInterface instance
- */
- public function getGenerator()
- {
- if (null !== $this->generator) {
- return $this->generator;
- }
- if (null === $this->options['cache_dir'] || null === $this->options['generator_cache_class']) {
- return $this->generator = new $this->options['generator_class']($this->getRouteCollection(), $this->context, $this->defaults);
- }
- $class = $this->options['generator_cache_class'];
- $cache = new ConfigCache($this->options['cache_dir'].'/'.$class.'.php', $this->options['debug']);
- if (!$cache->isFresh($class)) {
- $dumper = new $this->options['generator_dumper_class']($this->getRouteCollection());
- $options = array(
- 'class' => $class,
- 'base_class' => $this->options['generator_base_class'],
- );
- $cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
- }
- require_once $cache;
- return $this->generator = new $class($this->context, $this->defaults);
- }
- }
|