DynamicRouter.php in Zircon Profile 8
File
vendor/symfony-cmf/routing/DynamicRouter.php
View source
<?php
namespace Symfony\Cmf\Component\Routing;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Routing\RequestContextAwareInterface;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Cmf\Component\Routing\Event\Events;
use Symfony\Cmf\Component\Routing\Event\RouterMatchEvent;
class DynamicRouter implements RouterInterface, RequestMatcherInterface, ChainedRouterInterface {
protected $matcher;
protected $generator;
protected $eventDispatcher;
protected $enhancers = array();
protected $sortedEnhancers = array();
protected $uriFilterRegexp;
protected $context;
private $routeCollection;
public function __construct(RequestContext $context, $matcher, UrlGeneratorInterface $generator, $uriFilterRegexp = '', EventDispatcherInterface $eventDispatcher = null, RouteProviderInterface $provider = null) {
$this->context = $context;
if (!$matcher instanceof RequestMatcherInterface && !$matcher instanceof UrlMatcherInterface) {
throw new \InvalidArgumentException('Matcher must implement either Symfony\\Component\\Routing\\Matcher\\RequestMatcherInterface or Symfony\\Component\\Routing\\Matcher\\UrlMatcherInterface');
}
$this->matcher = $matcher;
$this->generator = $generator;
$this->eventDispatcher = $eventDispatcher;
$this->uriFilterRegexp = $uriFilterRegexp;
$this->provider = $provider;
$this->generator
->setContext($context);
}
public function getRouteCollection() {
if (!$this->routeCollection instanceof RouteCollection) {
$this->routeCollection = $this->provider ? new LazyRouteCollection($this->provider) : new RouteCollection();
}
return $this->routeCollection;
}
public function getMatcher() {
if ($this->matcher instanceof RequestContextAwareInterface) {
$this->matcher
->setContext($this
->getContext());
}
return $this->matcher;
}
public function getGenerator() {
$this->generator
->setContext($this
->getContext());
return $this->generator;
}
public function generate($name, $parameters = array(), $absolute = false) {
return $this
->getGenerator()
->generate($name, $parameters, $absolute);
}
public function supports($name) {
if ($this->generator instanceof VersatileGeneratorInterface) {
return $this->generator
->supports($name);
}
return is_string($name);
}
public function match($pathinfo) {
$request = Request::create($pathinfo);
if ($this->eventDispatcher) {
$event = new RouterMatchEvent();
$this->eventDispatcher
->dispatch(Events::PRE_DYNAMIC_MATCH, $event);
}
if (!empty($this->uriFilterRegexp) && !preg_match($this->uriFilterRegexp, $pathinfo)) {
throw new ResourceNotFoundException("{$pathinfo} does not match the '{$this->uriFilterRegexp}' pattern");
}
$matcher = $this
->getMatcher();
if (!$matcher instanceof UrlMatcherInterface) {
throw new \InvalidArgumentException('Wrong matcher type, you need to call matchRequest');
}
$defaults = $matcher
->match($pathinfo);
return $this
->applyRouteEnhancers($defaults, $request);
}
public function matchRequest(Request $request) {
if ($this->eventDispatcher) {
$event = new RouterMatchEvent($request);
$this->eventDispatcher
->dispatch(Events::PRE_DYNAMIC_MATCH_REQUEST, $event);
}
if (!empty($this->uriFilterRegexp) && !preg_match($this->uriFilterRegexp, $request
->getPathInfo())) {
throw new ResourceNotFoundException("{$request->getPathInfo()} does not match the '{$this->uriFilterRegexp}' pattern");
}
$matcher = $this
->getMatcher();
if ($matcher instanceof UrlMatcherInterface) {
$defaults = $matcher
->match($request
->getPathInfo());
}
else {
$defaults = $matcher
->matchRequest($request);
}
return $this
->applyRouteEnhancers($defaults, $request);
}
protected function applyRouteEnhancers($defaults, Request $request) {
foreach ($this
->getRouteEnhancers() as $enhancer) {
$defaults = $enhancer
->enhance($defaults, $request);
}
return $defaults;
}
public function addRouteEnhancer(RouteEnhancerInterface $enhancer, $priority = 0) {
if (empty($this->enhancers[$priority])) {
$this->enhancers[$priority] = array();
}
$this->enhancers[$priority][] = $enhancer;
$this->sortedEnhancers = array();
return $this;
}
public function getRouteEnhancers() {
if (empty($this->sortedEnhancers)) {
$this->sortedEnhancers = $this
->sortRouteEnhancers();
}
return $this->sortedEnhancers;
}
protected function sortRouteEnhancers() {
$sortedEnhancers = array();
krsort($this->enhancers);
foreach ($this->enhancers as $enhancers) {
$sortedEnhancers = array_merge($sortedEnhancers, $enhancers);
}
return $sortedEnhancers;
}
public function setContext(RequestContext $context) {
$this->context = $context;
}
public function getContext() {
return $this->context;
}
public function getRouteDebugMessage($name, array $parameters = array()) {
if ($this->generator instanceof VersatileGeneratorInterface) {
return $this->generator
->getRouteDebugMessage($name, $parameters);
}
return "Route '{$name}' not found";
}
}
Classes
Name |
Description |
DynamicRouter |
A flexible router accepting matcher and generator through injection and
using the RouteEnhancer concept to generate additional data on the routes. |