2010-02-17 13:53:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Components\Routing;
|
|
|
|
|
2010-07-17 16:18:30 +01:00
|
|
|
use Symfony\Components\Routing\Loader\LoaderInterface;
|
|
|
|
|
2010-02-17 13:53:31 +00:00
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony framework.
|
2010-02-17 13:53:31 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Router class is an example of the integration of all pieces of the
|
|
|
|
* routing system for easier use.
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class Router implements RouterInterface
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $matcher;
|
|
|
|
protected $generator;
|
|
|
|
protected $options;
|
|
|
|
protected $defaults;
|
|
|
|
protected $context;
|
|
|
|
protected $loader;
|
|
|
|
protected $collection;
|
2010-07-17 16:18:30 +01:00
|
|
|
protected $resource;
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* Available options:
|
|
|
|
*
|
|
|
|
* * cache_dir: The cache directory (or null to disable caching)
|
|
|
|
* * debug: Whether to enable debugging or not (false by default)
|
|
|
|
*
|
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
|
|
|
|
*/
|
2010-07-17 16:18:30 +01:00
|
|
|
public function __construct(LoaderInterface $loader, $resource, array $options = array(), array $context = array(), 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;
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->context = $context;
|
|
|
|
$this->defaults = $defaults;
|
|
|
|
$this->options = array(
|
|
|
|
'cache_dir' => null,
|
|
|
|
'debug' => false,
|
|
|
|
'generator_class' => 'Symfony\\Components\\Routing\\Generator\\UrlGenerator',
|
|
|
|
'generator_base_class' => 'Symfony\\Components\\Routing\\Generator\\UrlGenerator',
|
|
|
|
'generator_dumper_class' => 'Symfony\\Components\\Routing\\Generator\\Dumper\\PhpGeneratorDumper',
|
|
|
|
'generator_cache_class' => 'ProjectUrlGenerator',
|
|
|
|
'matcher_class' => 'Symfony\\Components\\Routing\\Matcher\\UrlMatcher',
|
|
|
|
'matcher_base_class' => 'Symfony\\Components\\Routing\\Matcher\\UrlMatcher',
|
|
|
|
'matcher_dumper_class' => 'Symfony\\Components\\Routing\\Matcher\\Dumper\\PhpMatcherDumper',
|
|
|
|
'matcher_cache_class' => 'ProjectUrlMatcher',
|
|
|
|
);
|
|
|
|
|
|
|
|
// check option names
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('The Router does not support the following options: \'%s\'.', implode('\', \'', $diff)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->options = array_merge($this->options, $options);
|
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) {
|
2010-07-17 16:18:30 +01:00
|
|
|
$this->collection = $this->loader->load($this->resource);
|
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.
|
|
|
|
*
|
|
|
|
* @param array $context The context
|
|
|
|
*/
|
|
|
|
public function setContext(array $context = array())
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->context = $context;
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Sets the defaults.
|
|
|
|
*
|
|
|
|
* @param array $defaults The defaults
|
|
|
|
*/
|
|
|
|
public function setDefaults(array $defaults = array())
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->defaults = $defaults;
|
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
|
|
|
|
*/
|
|
|
|
public function generate($name, array $parameters, $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'];
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->needsReload($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
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->updateCache($class, $dumper->dump($options));
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
require_once $this->getCacheFile($class);
|
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'];
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->needsReload($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'],
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->updateCache($class, $dumper->dump($options));
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once $this->getCacheFile($class);
|
|
|
|
|
|
|
|
return $this->generator = new $class($this->context, $this->defaults);
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function updateCache($class, $dump)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->writeCacheFile($this->getCacheFile($class), $dump);
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->options['debug']) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->writeCacheFile($this->getCacheFile($class, 'meta'), serialize($this->getRouteCollection()->getResources()));
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function needsReload($class)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$file = $this->getCacheFile($class);
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!file_exists($file)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$this->options['debug']) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$metadata = $this->getCacheFile($class, 'meta');
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!file_exists($metadata)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$time = filemtime($file);
|
|
|
|
$meta = unserialize(file_get_contents($metadata));
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($meta as $resource) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (!$resource->isUptodate($time)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function getCacheFile($class, $extension = 'php')
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->options['cache_dir'].'/'.$class.'.'.$extension;
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* @throws \RuntimeException When cache file can't be wrote
|
|
|
|
*/
|
|
|
|
protected function writeCacheFile($file, $content)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$tmpFile = tempnam(dirname($file), basename($file));
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$fp = @fopen($tmpFile, 'wb')) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $tmpFile));
|
|
|
|
}
|
|
|
|
|
|
|
|
@fwrite($fp, $content);
|
|
|
|
@fclose($fp);
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($content != file_get_contents($tmpFile)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \RuntimeException(sprintf('Failed to write cache file "%s" (cache corrupted).', $tmpFile));
|
|
|
|
}
|
|
|
|
|
2010-06-22 20:01:18 +01:00
|
|
|
if (!@rename($tmpFile, $file)) {
|
|
|
|
throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
chmod($file, 0644);
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
}
|