diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index f1e43d6..c5ed4a2 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -63,8 +63,11 @@ public function build(ContainerBuilder $container) { $container->register('router.dumper', '\Drupal\Core\Routing\MatcherDumper') ->addArgument(new Reference('database')); + $container->register('router.collection_factory', '\Drupal\Core\Routing\RouteCollectionFactory') + ->addArgument(new Reference('database')); $container->register('router.builder', 'Drupal\Core\Routing\RouteBuilder') - ->addArgument(new Reference('router.dumper')); + ->addArgument(new Reference('router.dumper')) + ->addArgument(new Reference('router.collection_factory')); // @todo Replace below lines with the commented out block below it when it's // performant to do so: http://drupal.org/node/1706064. diff --git a/core/lib/Drupal/Core/Routing/RouteBuilder.php b/core/lib/Drupal/Core/Routing/RouteBuilder.php index 6335ffe..932c476 100644 --- a/core/lib/Drupal/Core/Routing/RouteBuilder.php +++ b/core/lib/Drupal/Core/Routing/RouteBuilder.php @@ -27,13 +27,21 @@ class RouteBuilder { protected $dumper; /** + * The route collection factory to pass to hook_route_info() implementations. + * + * @var \Drupal\Core\Routing\RouteCollectionFactory + */ + protected $factory; + + /** * Construcs the RouteBuilder using the passed MatcherDumperInterface. * * @param Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface $dumper * The matcher dumper used to store the route information. */ - public function __construct(MatcherDumperInterface $dumper) { + public function __construct(MatcherDumperInterface $dumper, RouteCollectionFactory $factory) { $this->dumper = $dumper; + $this->factory = $factory; } /** @@ -44,7 +52,7 @@ public function rebuild() { // a given item came from. foreach (module_implements('route_info') as $module) { - $routes = call_user_func($module . '_route_info'); + $routes = call_user_func($module . '_route_info', $this->factory); drupal_alter('router_info', $routes, $module); $this->dumper->addRoutes($routes); $this->dumper->dump(array('route_set' => $module)); diff --git a/core/lib/Drupal/Core/Routing/RouteCollectionFactory.php b/core/lib/Drupal/Core/Routing/RouteCollectionFactory.php new file mode 100644 index 0000000..949f029 --- /dev/null +++ b/core/lib/Drupal/Core/Routing/RouteCollectionFactory.php @@ -0,0 +1,31 @@ + $item) { + $requirements = isset($item['requirements']) ? $item['requirements'] : array(); + $route = new Route($item['pattern'], $item['defaults'], $requirements); + $collection->add($name, $route); + } + return $collection; + } + +} diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index a9a2e17..546aa18 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -583,7 +583,8 @@ function hook_menu_get_item_alter(&$router_item, $path, $original_map) { * by a more robust mechanism in the near future. It is documented here * only for completeness. */ -function hook_route_info() { +function hook_route_info($route_collection_factory) { + // Option 1: Build a RouteCollection explicitly and return it. $collection = new RouteCollection(); $route = new Route('router_test/test1', array( @@ -608,6 +609,37 @@ function hook_route_info() { $collection->add('router_test_4', $route); return $collection; + + // Option 2: Use the factory to build a RouteCollection from a PHP array. + return $route_collection_factory->createFromArray(array( + 'router_test_1' => array( + 'pattern' => 'router_test/test1', + 'defaults' => array( + '_controller' => '\Drupal\router_test\TestControllers::test1', + ), + ), + 'router_test_2' => array( + 'pattern' => 'router_test/test2', + 'defaults' => array( + '_content' => '\Drupal\router_test\TestControllers::test2', + ), + ), + 'router_test_3' => array( + 'pattern' => 'router_test/test3/{value}', + 'defaults' => array( + '_content' => '\Drupal\router_test\TestControllers::test3', + ), + ), + 'router_test_4' => array( + 'pattern' => 'router_test/test4/{value}', + 'defaults' => array( + '_content' => '\Drupal\router_test\TestControllers::test4', + 'value' => 'narf', + ), + ), + )); + + // @todo More options TBD: http://drupal.org/node/1801570. } /**