diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index f1d0046..76164f4 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -71,7 +71,9 @@ public function build(ContainerBuilder $container) { ->addArgument(new Reference('database')); $container->register('router.builder', 'Drupal\Core\Routing\RouteBuilder') ->addArgument(new Reference('router.dumper')) - ->addArgument(new Reference('lock')); + ->addArgument(new Reference('lock')) + ->addArgument(new Reference('dispatcher')); + $container->register('matcher', 'Drupal\Core\Routing\ChainMatcher'); $container->register('legacy_url_matcher', 'Drupal\Core\LegacyUrlMatcher') diff --git a/core/lib/Drupal/Core/Routing/RouteBuildEvent.php b/core/lib/Drupal/Core/Routing/RouteBuildEvent.php new file mode 100644 index 0000000..9f16bcb --- /dev/null +++ b/core/lib/Drupal/Core/Routing/RouteBuildEvent.php @@ -0,0 +1,46 @@ +routeCollection = $route_collection; + $this->module = $module; + } + + /** + * Get the route collection. + */ + public function getRouteCollection() { + return $this->routeCollection; + } + + /** + * Get the module. + */ + public function getModule() { + return $this->module; + } + +} diff --git a/core/lib/Drupal/Core/Routing/RouteBuilder.php b/core/lib/Drupal/Core/Routing/RouteBuilder.php index 3a27767..1d91d56 100644 --- a/core/lib/Drupal/Core/Routing/RouteBuilder.php +++ b/core/lib/Drupal/Core/Routing/RouteBuilder.php @@ -7,8 +7,13 @@ namespace Drupal\Core\Routing; -use Drupal\Core\Lock\LockBackendInterface; use Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\Yaml\Parser; +use Symfony\Component\Routing\RouteCollection; +use Symfony\Component\Routing\Route; + +use Drupal\Core\Lock\LockBackendInterface; /** * Managing class for rebuilding the router table. @@ -33,16 +38,26 @@ class RouteBuilder { 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) { + public function __construct(MatcherDumperInterface $dumper, LockBackendInterface $lock, EventDispatcherInterface $dispatcher) { $this->dumper = $dumper; $this->lock = $lock; + $this->dispatcher = $dispatcher; } /** @@ -57,15 +72,38 @@ public function rebuild() { return; } + $parser = new Parser(); + // We need to manually call each module so that we can know which module // a given item came from. - - foreach (module_implements('route_info') as $module) { - $routes = call_user_func($module . '_route_info'); - drupal_alter('router_info', $routes, $module); - $this->dumper->addRoutes($routes); + // @todo Use an injected Extension service rather than module_list(): + // http://drupal.org/node/1331486. + foreach (module_list() as $module) { + $collection = new RouteCollection(); + $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 = new Route($route_info['pattern'], $defaults, $requirements); + $collection->add($name, $route); + } + } + } + $this->dispatcher->dispatch(RoutingEvents::ALTER, new RouteBuildEvent($collection, $module)); + $this->dumper->addRoutes($collection); $this->dumper->dump(array('route_set' => $module)); } + + // Now allow modules to register additional, dynamic routes. + $collection = new RouteCollection(); + $this->dispatcher->dispatch(RoutingEvents::DYNAMIC, new RouteBuildEvent($collection, 'dynamic_routes')); + $this->dispatcher->dispatch(RoutingEvents::ALTER, new RouteBuildEvent($collection, 'dynamic_routes')); + $this->dumper->addRoutes($collection); + $this->dumper->dump(array('route_set' => $module)); + $this->lock->release('router_rebuild'); } diff --git a/core/lib/Drupal/Core/Routing/RoutingEvents.php b/core/lib/Drupal/Core/Routing/RoutingEvents.php new file mode 100644 index 0000000..6a72ff0 --- /dev/null +++ b/core/lib/Drupal/Core/Routing/RoutingEvents.php @@ -0,0 +1,38 @@ + '\Drupal\router_test\TestControllers::test1' - )); - $collection->add('router_test_1', $route); - - $route = new Route('router_test/test2', array( - '_content' => '\Drupal\router_test\TestControllers::test2' - )); - $collection->add('router_test_2', $route); - - $route = new Route('router_test/test3/{value}', array( - '_content' => '\Drupal\router_test\TestControllers::test3' - )); - $collection->add('router_test_3', $route); - - $route = new Route('router_test/test4/{value}', array( - '_content' => '\Drupal\router_test\TestControllers::test4', - 'value' => 'narf', - )); - $collection->add('router_test_4', $route); - - return $collection; -} - -/** * Define menu items and page callbacks. * * This hook enables modules to register paths in order to define how URL diff --git a/core/modules/system/tests/modules/router_test/router_test.module b/core/modules/system/tests/modules/router_test/router_test.module index 4da939d..b3d9bbc 100644 --- a/core/modules/system/tests/modules/router_test/router_test.module +++ b/core/modules/system/tests/modules/router_test/router_test.module @@ -1,34 +1 @@ '\Drupal\router_test\TestControllers::test1' - )); - $collection->add('router_test_1', $route); - - $route = new Route('router_test/test2', array( - '_content' => '\Drupal\router_test\TestControllers::test2' - )); - $collection->add('router_test_2', $route); - - $route = new Route('router_test/test3/{value}', array( - '_content' => '\Drupal\router_test\TestControllers::test3' - )); - $collection->add('router_test_3', $route); - - $route = new Route('router_test/test4/{value}', array( - '_content' => '\Drupal\router_test\TestControllers::test4', - 'value' => 'narf', - )); - $collection->add('router_test_4', $route); - - return $collection; -} diff --git a/core/modules/system/tests/modules/router_test/router_test.routing.yml b/core/modules/system/tests/modules/router_test/router_test.routing.yml new file mode 100644 index 0000000..a49dd80 --- /dev/null +++ b/core/modules/system/tests/modules/router_test/router_test.routing.yml @@ -0,0 +1,20 @@ +router_test_1: + pattern: '/router_test/test1' + defaults: + _controller: '\Drupal\router_test\TestControllers::test1' + +router_test_2: + pattern: '/router_test/test2' + defaults: + _content: '\Drupal\router_test\TestControllers::test2' + +router_test_3: + pattern: '/router_test/test3/{value}' + defaults: + _content: '\Drupal\router_test\TestControllers::test3' + +router_test_4: + pattern: '/router_test/test4/{value}' + defaults: + _content: '\Drupal\router_test\TestControllers::test4' + value: 'narf'