<?php/**
* @file
* Definition of Drupal\Core\Routing\RouteBuilder.
*/namespaceDrupal\Core\Routing;
useSymfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface;
useSymfony\Component\EventDispatcher\EventDispatcherInterface;
useSymfony\Component\Yaml\Parser;
useSymfony\Component\Routing\RouteCollection;
useSymfony\Component\Routing\Route;
useDrupal\Core\Lock\LockBackendInterface;
/**
* Managing class for rebuilding the router table.
*
* Because this class makes use of the modules system, it cannot currently
* be unit tested.
*/class RouteBuilder {
/**
* The dumper to which we should send collected routes.
*
* @var \Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface
*/
protected $dumper;
/**
* The used lock backend instance.
*
* @var \Drupal\Core\Lock\LockBackendInterface $lock
*/
protected $lock;
/**
* The event dispatcher to notify of routes.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $dispatcher;
/**
* Construcs the RouteBuilder using the passed MatcherDumperInterface.
*
* @param \Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface $dumper
* The matcher dumper used to store the route information.
* @param \Drupal\Core\Lock\LockBackendInterface $lock
* The lock backend.
* @param \Symfony\Component\EventDispatcherEventDispatcherInterface
* The event dispatcher to notify of routes.
*/
public function__construct(MatcherDumperInterface $dumper, LockBackendInterface $lock, EventDispatcherInterface $dispatcher) {
$this->dumper = $dumper;
$this->lock = $lock;
$this->dispatcher = $dispatcher;
}
/**
* Rebuilds the route info and dumps to dumper.
*/
public functionrebuild() {
if (!$this->lock
->acquire('router_rebuild')) {
// Wait for another request that is already doing this work.
// We choose to block here since otherwise the routes might not be
// available, resulting in a 404.$this->lock
->wait('router_rebuild');
return;
}
$parser = newParser();
// We need to manually call each module so that we can know which module
// a given item came from.
// @todo Use an injected Extension service rather than module_list():
// http://drupal.org/node/1331486.foreach (module_list() as $module) {
$collection = newRouteCollection();
$routing_file = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . '/' . $module . '.routing.yml';
if (file_exists($routing_file)) {
$routes = $parser
->parse(file_get_contents($routing_file));
if (!empty($routes)) {
foreach ($routes as $name => $route_info) {
$defaults = isset($route_info['defaults']) ? $route_info['defaults'] : array();
$requirements = isset($route_info['requirements']) ? $route_info['requirements'] : array();
$route = newRoute($route_info['pattern'], $defaults, $requirements);
$collection
->add($name, $route);
}
}
}
$this->dispatcher
->dispatch(RoutingEvents::ALTER, newRouteBuildEvent($collection, $module));
$this->dumper
->addRoutes($collection);
$this->dumper
->dump(array(
'route_set' => $module,
));
}
// Now allow modules to register additional, dynamic routes.$collection = newRouteCollection();
$this->dispatcher
->dispatch(RoutingEvents::DYNAMIC, newRouteBuildEvent($collection, 'dynamic_routes'));
$this->dispatcher
->dispatch(RoutingEvents::ALTER, newRouteBuildEvent($collection, 'dynamic_routes'));
$this->dumper
->addRoutes($collection);
$this->dumper
->dump(array(
'route_set' => 'dynamic_routes',
));
$this->lock
->release('router_rebuild');
}
}