You are here

public function RouteBuilder::rebuild in Drupal 9

Same name in this branch
  1. 9 core/lib/Drupal/Core/Routing/RouteBuilder.php \Drupal\Core\Routing\RouteBuilder::rebuild()
  2. 9 core/lib/Drupal/Core/ProxyClass/Routing/RouteBuilder.php \Drupal\Core\ProxyClass\Routing\RouteBuilder::rebuild()
Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Routing/RouteBuilder.php \Drupal\Core\Routing\RouteBuilder::rebuild()
  2. 10 core/lib/Drupal/Core/Routing/RouteBuilder.php \Drupal\Core\Routing\RouteBuilder::rebuild()

Rebuilds the route information and dumps it.

Return value

bool Returns TRUE if the rebuild succeeds, FALSE otherwise.

Overrides RouteBuilderInterface::rebuild

1 call to RouteBuilder::rebuild()
RouteBuilder::rebuildIfNeeded in core/lib/Drupal/Core/Routing/RouteBuilder.php
Rebuilds the route information if necessary, and dumps it.

File

core/lib/Drupal/Core/Routing/RouteBuilder.php, line 119

Class

RouteBuilder
Managing class for rebuilding the router table.

Namespace

Drupal\Core\Routing

Code

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 += [
        'defaults' => [],
        'requirements' => [],
        'options' => [],
        'host' => NULL,
        'schemes' => [],
        'methods' => [],
        'condition' => '',
      ];

      // Ensure routes default to using Drupal's route compiler instead of
      // Symfony's.
      $route_info['options'] += [
        'compiler_class' => RouteCompiler::class,
      ];
      $route = new Route($route_info['path'], $route_info['defaults'], $route_info['requirements'], $route_info['options'], $route_info['host'], $route_info['schemes'], $route_info['methods'], $route_info['condition']);
      $collection
        ->add($name, $route);
    }
  }

  // DYNAMIC is supposed to be used to add new routes based upon all the
  // static defined ones.
  $this->dispatcher
    ->dispatch(new RouteBuildEvent($collection), RoutingEvents::DYNAMIC);

  // 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(new RouteBuildEvent($collection), RoutingEvents::ALTER);
  $this->checkProvider
    ->setChecks($collection);
  $this->dumper
    ->addRoutes($collection);
  $this->dumper
    ->dump();
  $this->lock
    ->release('router_rebuild');
  $this->dispatcher
    ->dispatch(new Event(), RoutingEvents::FINISHED);
  $this->building = FALSE;
  $this->rebuildNeeded = FALSE;
  return TRUE;
}