ChainRouter.php in Zircon Profile 8
File
vendor/symfony-cmf/routing/ChainRouter.php
View source
<?php
namespace Symfony\Cmf\Component\Routing;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RequestContextAwareInterface;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Psr\Log\LoggerInterface;
class ChainRouter implements ChainRouterInterface, WarmableInterface {
private $context;
private $routers = array();
private $sortedRouters;
private $routeCollection;
protected $logger;
public function __construct(LoggerInterface $logger = null) {
$this->logger = $logger;
}
public function getContext() {
return $this->context;
}
public function add($router, $priority = 0) {
if (!$router instanceof RouterInterface && !($router instanceof RequestMatcherInterface && $router instanceof UrlGeneratorInterface)) {
throw new \InvalidArgumentException(sprintf('%s is not a valid router.', get_class($router)));
}
if (empty($this->routers[$priority])) {
$this->routers[$priority] = array();
}
$this->routers[$priority][] = $router;
$this->sortedRouters = array();
}
public function all() {
if (empty($this->sortedRouters)) {
$this->sortedRouters = $this
->sortRouters();
$context = $this
->getContext();
if (null !== $context) {
foreach ($this->sortedRouters as $router) {
if ($router instanceof RequestContextAwareInterface) {
$router
->setContext($context);
}
}
}
}
return $this->sortedRouters;
}
protected function sortRouters() {
$sortedRouters = array();
krsort($this->routers);
foreach ($this->routers as $routers) {
$sortedRouters = array_merge($sortedRouters, $routers);
}
return $sortedRouters;
}
public function match($url) {
return $this
->doMatch($url);
}
public function matchRequest(Request $request) {
return $this
->doMatch($request
->getPathInfo(), $request);
}
private function doMatch($url, Request $request = null) {
$methodNotAllowed = null;
$requestForMatching = $request;
foreach ($this
->all() as $router) {
try {
if ($router instanceof RequestMatcherInterface) {
if (empty($requestForMatching)) {
$requestForMatching = Request::create($url);
}
return $router
->matchRequest($requestForMatching);
}
return $router
->match($url);
} catch (ResourceNotFoundException $e) {
if ($this->logger) {
$this->logger
->debug('Router ' . get_class($router) . ' was not able to match, message "' . $e
->getMessage() . '"');
}
} catch (MethodNotAllowedException $e) {
if ($this->logger) {
$this->logger
->debug('Router ' . get_class($router) . ' throws MethodNotAllowedException with message "' . $e
->getMessage() . '"');
}
$methodNotAllowed = $e;
}
}
$info = $request ? "this request\n{$request}" : "url '{$url}'";
throw $methodNotAllowed ?: new ResourceNotFoundException("None of the routers in the chain matched {$info}");
}
public function generate($name, $parameters = array(), $absolute = false) {
$debug = array();
foreach ($this
->all() as $router) {
if ($name && !is_string($name) && !$router instanceof VersatileGeneratorInterface) {
continue;
}
if ($router instanceof VersatileGeneratorInterface && !$router
->supports($name)) {
continue;
}
try {
return $router
->generate($name, $parameters, $absolute);
} catch (RouteNotFoundException $e) {
$hint = $this
->getErrorMessage($name, $router, $parameters);
$debug[] = $hint;
if ($this->logger) {
$this->logger
->debug('Router ' . get_class($router) . " was unable to generate route. Reason: '{$hint}': " . $e
->getMessage());
}
}
}
if ($debug) {
$debug = array_unique($debug);
$info = implode(', ', $debug);
}
else {
$info = $this
->getErrorMessage($name);
}
throw new RouteNotFoundException(sprintf('None of the chained routers were able to generate route: %s', $info));
}
private function getErrorMessage($name, $router = null, $parameters = null) {
if ($router instanceof VersatileGeneratorInterface) {
$displayName = $router
->getRouteDebugMessage($name, $parameters);
}
elseif (is_object($name)) {
$displayName = method_exists($name, '__toString') ? (string) $name : get_class($name);
}
else {
$displayName = (string) $name;
}
return "Route '{$displayName}' not found";
}
public function setContext(RequestContext $context) {
foreach ($this
->all() as $router) {
if ($router instanceof RequestContextAwareInterface) {
$router
->setContext($context);
}
}
$this->context = $context;
}
public function warmUp($cacheDir) {
foreach ($this
->all() as $router) {
if ($router instanceof WarmableInterface) {
$router
->warmUp($cacheDir);
}
}
}
public function getRouteCollection() {
if (!$this->routeCollection instanceof RouteCollection) {
$this->routeCollection = new ChainRouteCollection();
foreach ($this
->all() as $router) {
$this->routeCollection
->addCollection($router
->getRouteCollection());
}
}
return $this->routeCollection;
}
}
Classes
Name |
Description |
ChainRouter |
The ChainRouter allows to combine several routers to try in a defined order. |