Example #1
0
 /**
  * {@inheritdoc}
  */
 public function rebuild()
 {
     if ($this->building) {
         throw new \RuntimeException('Recursive router rebuild detected.');
     }
     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 FALSE;
     }
     $this->building = TRUE;
     $collection = new RouteCollection();
     foreach ($this->getRouteDefinitions() as $routes) {
         // The top-level 'routes_callback' is a list of methods in controller
         // syntax, see \Drupal\Core\Controller\ControllerResolver. These methods
         // should return a set of \Symfony\Component\Routing\Route objects, either
         // in an associative array keyed by the route name, which will be iterated
         // over and added to the collection for this provider, or as a new
         // \Symfony\Component\Routing\RouteCollection object, which will be added
         // to the collection.
         if (isset($routes['route_callbacks'])) {
             foreach ($routes['route_callbacks'] as $route_callback) {
                 $callback = $this->controllerResolver->getControllerFromDefinition($route_callback);
                 if ($callback_routes = call_user_func($callback)) {
                     // If a RouteCollection is returned, add the whole collection.
                     if ($callback_routes instanceof RouteCollection) {
                         $collection->addCollection($callback_routes);
                     } else {
                         foreach ($callback_routes as $name => $callback_route) {
                             $collection->add($name, $callback_route);
                         }
                     }
                 }
             }
             unset($routes['route_callbacks']);
         }
         foreach ($routes as $name => $route_info) {
             $route_info += array('defaults' => array(), 'requirements' => array(), 'options' => array());
             $route = new Route($route_info['path'], $route_info['defaults'], $route_info['requirements'], $route_info['options']);
             $collection->add($name, $route);
         }
     }
     // DYNAMIC is supposed to be used to add new routes based upon all the
     // static defined ones.
     $this->dispatcher->dispatch(RoutingEvents::DYNAMIC, new RouteBuildEvent($collection));
     // ALTER is the final step to alter all the existing routes. We cannot stop
     // people from adding new routes here, but we define two separate steps to
     // make it clear.
     $this->dispatcher->dispatch(RoutingEvents::ALTER, new RouteBuildEvent($collection));
     $this->checkProvider->setChecks($collection);
     $this->dumper->addRoutes($collection);
     $this->dumper->dump();
     $this->lock->release('router_rebuild');
     $this->dispatcher->dispatch(RoutingEvents::FINISHED, new Event());
     $this->building = FALSE;
     $this->rebuildNeeded = FALSE;
     return TRUE;
 }
 /**
  * Tests the rebuild with routes provided by a callback.
  *
  * @see \Drupal\Core\Routing\RouteBuilder::rebuild()
  */
 public function testRebuildWithProviderBasedRoutes()
 {
     $this->lock->expects($this->once())->method('acquire')->with('router_rebuild')->will($this->returnValue(TRUE));
     $this->yamlDiscovery->expects($this->once())->method('findAll')->will($this->returnValue(array('test_module' => array('route_callbacks' => array('\\Drupal\\Tests\\Core\\Routing\\TestRouteSubscriber::routesFromArray', 'test_module.route_service:routesFromCollection')))));
     $container = new ContainerBuilder();
     $container->set('test_module.route_service', new TestRouteSubscriber());
     $this->controllerResolver->expects($this->any())->method('getControllerFromDefinition')->will($this->returnCallback(function ($controller) use($container) {
         $count = substr_count($controller, ':');
         if ($count == 1) {
             list($service, $method) = explode(':', $controller, 2);
             $object = $container->get($service);
         } else {
             list($class, $method) = explode('::', $controller, 2);
             $object = new $class();
         }
         return array($object, $method);
     }));
     $route_collection_filled = new RouteCollection();
     $route_collection_filled->add('test_route.1', new Route('/test-route/1'));
     $route_collection_filled->add('test_route.2', new Route('/test-route/2'));
     $route_build_event = new RouteBuildEvent($route_collection_filled);
     // Ensure that the alter routes events are fired.
     $this->dispatcher->expects($this->at(0))->method('dispatch')->with(RoutingEvents::DYNAMIC, $route_build_event);
     $this->dispatcher->expects($this->at(1))->method('dispatch')->with(RoutingEvents::ALTER, $route_build_event);
     // Ensure that the routes are set to the dumper and dumped.
     $this->dumper->expects($this->at(0))->method('addRoutes')->with($route_collection_filled);
     $this->dumper->expects($this->at(1))->method('dump');
     $this->assertTrue($this->routeBuilder->rebuild());
 }