<?php
namespace Symfony\Component\Routing;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\ConfigCache;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Generator\ConfigurableRequirementsInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
class Router implements RouterInterface {
protected $matcher;
protected $generator;
protected $context;
protected $loader;
protected $collection;
protected $resource;
protected $options = array();
protected $logger;
public function __construct(LoaderInterface $loader, $resource, array $options = array(), RequestContext $context = null, LoggerInterface $logger = null) {
$this->loader = $loader;
$this->resource = $resource;
$this->logger = $logger;
$this->context = null === $context ? new RequestContext() : $context;
$this
->setOptions($options);
}
public function setOptions(array $options) {
$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,
'strict_requirements' => true,
);
$invalid = array();
foreach ($options as $key => $value) {
if (array_key_exists($key, $this->options)) {
$this->options[$key] = $value;
}
else {
$invalid[] = $key;
}
}
if ($invalid) {
throw new \InvalidArgumentException(sprintf('The Router does not support the following options: "%s".', implode('", "', $invalid)));
}
}
public function setOption($key, $value) {
if (!array_key_exists($key, $this->options)) {
throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
}
$this->options[$key] = $value;
}
public function getOption($key) {
if (!array_key_exists($key, $this->options)) {
throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
}
return $this->options[$key];
}
public function getRouteCollection() {
if (null === $this->collection) {
$this->collection = $this->loader
->load($this->resource, $this->options['resource_type']);
}
return $this->collection;
}
public function setContext(RequestContext $context) {
$this->context = $context;
if (null !== $this->matcher) {
$this
->getMatcher()
->setContext($context);
}
if (null !== $this->generator) {
$this
->getGenerator()
->setContext($context);
}
}
public function getContext() {
return $this->context;
}
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) {
return $this
->getGenerator()
->generate($name, $parameters, $referenceType);
}
public function match($pathinfo) {
return $this
->getMatcher()
->match($pathinfo);
}
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);
}
$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);
}
public function getGenerator() {
if (null !== $this->generator) {
return $this->generator;
}
if (null === $this->options['cache_dir'] || null === $this->options['generator_cache_class']) {
$this->generator = new $this->options['generator_class']($this
->getRouteCollection(), $this->context, $this->logger);
}
else {
$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;
$this->generator = new $class($this->context, $this->logger);
}
if ($this->generator instanceof ConfigurableRequirementsInterface) {
$this->generator
->setStrictRequirements($this->options['strict_requirements']);
}
return $this->generator;
}
}