2010-02-17 13:53:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-02-17 13:53:31 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-02-17 13:53:31 +00:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-02-17 13:53:31 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Routing;
|
|
|
|
|
2011-02-10 15:15:51 +00:00
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface;
|
|
|
|
use Symfony\Component\Config\ConfigCache;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2010-02-17 13:53:31 +00:00
|
|
|
/**
|
|
|
|
* The Router class is an example of the integration of all pieces of the
|
|
|
|
* routing system for easier use.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-02-17 13:53:31 +00:00
|
|
|
*/
|
|
|
|
class Router implements RouterInterface
|
|
|
|
{
|
2011-03-23 18:24:18 +00:00
|
|
|
private $matcher;
|
|
|
|
private $generator;
|
|
|
|
private $options;
|
|
|
|
private $defaults;
|
|
|
|
private $context;
|
|
|
|
private $loader;
|
|
|
|
private $collection;
|
|
|
|
private $resource;
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* Available options:
|
|
|
|
*
|
2011-02-10 15:26:13 +00:00
|
|
|
* * 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)
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @param LoaderInterface $loader A LoaderInterface instance
|
|
|
|
* @param mixed $resource The main resource to load
|
|
|
|
* @param array $options An array of options
|
|
|
|
* @param array $context The context
|
|
|
|
* @param array $defaults The default values
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException When unsupported option is provided
|
|
|
|
*/
|
2011-04-20 12:54:39 +01:00
|
|
|
public function __construct(LoaderInterface $loader, $resource, array $options = array(), RequestContext $context = null, array $defaults = array())
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->loader = $loader;
|
2010-07-17 16:18:30 +01:00
|
|
|
$this->resource = $resource;
|
2011-04-20 12:54:39 +01:00
|
|
|
$this->context = null === $context ? new RequestContext() : $context;
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->defaults = $defaults;
|
|
|
|
$this->options = array(
|
|
|
|
'cache_dir' => null,
|
|
|
|
'debug' => false,
|
2010-08-20 22:09:55 +01:00
|
|
|
'generator_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
|
|
|
|
'generator_base_class' => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
|
|
|
|
'generator_dumper_class' => 'Symfony\\Component\\Routing\\Generator\\Dumper\\PhpGeneratorDumper',
|
2010-05-06 12:25:53 +01:00
|
|
|
'generator_cache_class' => 'ProjectUrlGenerator',
|
2010-08-20 22:09:55 +01:00
|
|
|
'matcher_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
|
|
|
|
'matcher_base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
|
|
|
|
'matcher_dumper_class' => 'Symfony\\Component\\Routing\\Matcher\\Dumper\\PhpMatcherDumper',
|
2010-05-06 12:25:53 +01:00
|
|
|
'matcher_cache_class' => 'ProjectUrlMatcher',
|
2011-02-10 15:26:13 +00:00
|
|
|
'resource_type' => null,
|
2010-05-06 12:25:53 +01:00
|
|
|
);
|
|
|
|
|
2011-03-10 17:24:33 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2011-03-14 12:54:48 +00:00
|
|
|
|
2011-03-10 17:24:33 +00:00
|
|
|
if ($isInvalid) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('The Router does not support the following options: \'%s\'.', implode('\', \'', $invalid)));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets the RouteCollection instance associated with this Router.
|
|
|
|
*
|
|
|
|
* @return RouteCollection A RouteCollection instance
|
|
|
|
*/
|
|
|
|
public function getRouteCollection()
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null === $this->collection) {
|
2011-02-10 15:26:13 +00:00
|
|
|
$this->collection = $this->loader->load($this->resource, $this->options['resource_type']);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->collection;
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Sets the request context.
|
|
|
|
*
|
2011-04-20 12:54:39 +01:00
|
|
|
* @param RequestContext $context The context
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-04-20 12:54:39 +01:00
|
|
|
public function setContext(RequestContext $context)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2011-01-24 15:59:32 +00:00
|
|
|
$this->getMatcher()->setContext($context);
|
|
|
|
$this->getGenerator()->setContext($context);
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2010-08-15 11:13:58 +01:00
|
|
|
public function generate($name, array $parameters = array(), $absolute = false)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->getGenerator()->generate($name, $parameters, $absolute);
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* 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)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->getMatcher()->match($url);
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets the UrlMatcher instance associated with this Router.
|
|
|
|
*
|
|
|
|
* @return UrlMatcherInterface A UrlMatcherInterface instance
|
|
|
|
*/
|
|
|
|
public function getMatcher()
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null !== $this->matcher) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->matcher;
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null === $this->options['cache_dir'] || null === $this->options['matcher_cache_class']) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->matcher = new $this->options['matcher_class']($this->getRouteCollection(), $this->context, $this->defaults);
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$class = $this->options['matcher_cache_class'];
|
2011-03-14 17:37:25 +00:00
|
|
|
$cache = new ConfigCache($this->options['cache_dir'].'/'.$class.'.php', $this->options['debug']);
|
2011-02-10 15:15:51 +00:00
|
|
|
if (!$cache->isFresh($class)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$dumper = new $this->options['matcher_dumper_class']($this->getRouteCollection());
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$options = array(
|
|
|
|
'class' => $class,
|
|
|
|
'base_class' => $this->options['matcher_base_class'],
|
|
|
|
);
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2011-02-10 15:15:51 +00:00
|
|
|
$cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2011-02-10 15:15:51 +00:00
|
|
|
require_once $cache;
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->matcher = new $class($this->context, $this->defaults);
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Gets the UrlGenerator instance associated with this Router.
|
|
|
|
*
|
|
|
|
* @return UrlGeneratorInterface A UrlGeneratorInterface instance
|
|
|
|
*/
|
|
|
|
public function getGenerator()
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null !== $this->generator) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->generator;
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null === $this->options['cache_dir'] || null === $this->options['generator_cache_class']) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->generator = new $this->options['generator_class']($this->getRouteCollection(), $this->context, $this->defaults);
|
|
|
|
}
|
|
|
|
|
|
|
|
$class = $this->options['generator_cache_class'];
|
2011-03-14 17:37:25 +00:00
|
|
|
$cache = new ConfigCache($this->options['cache_dir'].'/'.$class.'.php', $this->options['debug']);
|
2011-02-10 15:15:51 +00:00
|
|
|
if (!$cache->isFresh($class)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$dumper = new $this->options['generator_dumper_class']($this->getRouteCollection());
|
|
|
|
|
|
|
|
$options = array(
|
|
|
|
'class' => $class,
|
|
|
|
'base_class' => $this->options['generator_base_class'],
|
|
|
|
);
|
|
|
|
|
2011-02-10 15:15:51 +00:00
|
|
|
$cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2011-02-10 15:15:51 +00:00
|
|
|
require_once $cache;
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
return $this->generator = new $class($this->context, $this->defaults);
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
}
|