RouteBuilder.php 7.25 KB
Newer Older
1 2
<?php

3 4
/**
 * @file
5
 * Contains \Drupal\Core\Routing\RouteBuilder.
6 7
 */

8 9
namespace Drupal\Core\Routing;

10
use Drupal\Component\Discovery\YamlDiscovery;
11
use Drupal\Core\Access\CheckProviderInterface;
12
use Drupal\Core\Controller\ControllerResolverInterface;
13 14
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Lock\LockBackendInterface;
15
use Symfony\Component\EventDispatcher\Event;
16 17 18 19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

20 21 22
/**
 * Managing class for rebuilding the router table.
 */
23
class RouteBuilder implements RouteBuilderInterface {
24

25 26 27
  /**
   * The dumper to which we should send collected routes.
   *
28
   * @var \Drupal\Core\Routing\MatcherDumperInterface
29
   */
30 31
  protected $dumper;

32 33 34 35 36 37 38
  /**
   * The used lock backend instance.
   *
   * @var \Drupal\Core\Lock\LockBackendInterface $lock
   */
  protected $lock;

39 40 41 42 43 44 45
  /**
   * The event dispatcher to notify of routes.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $dispatcher;

46
  /**
47 48 49 50 51 52 53 54
   * The yaml discovery used to find all the .routing.yml files.
   *
   * @var \Drupal\Component\Discovery\YamlDiscovery
   */
  protected $yamlDiscovery;

  /**
   * The module handler.
55 56 57 58 59
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

60 61 62 63 64 65 66
  /**
   * The route builder indicator.
   *
   * @var \Drupal\Core\Routing\RouteBuilderIndicatorInterface
   */
  protected $routeBuilderIndicator;

67 68 69 70 71 72 73
  /**
   * The controller resolver.
   *
   * @var \Drupal\Core\Controller\ControllerResolverInterface
   */
  protected $controllerResolver;

74 75 76 77 78 79 80
  /**
   * The route collection during the rebuild.
   *
   * @var \Symfony\Component\Routing\RouteCollection
   */
  protected $routeCollection;

81 82 83 84 85 86 87
  /**
   * Flag that indiciates if we are currently rebuilding the routes.
   *
   * @var bool
   */
  protected $building = FALSE;

88 89 90 91 92 93 94
  /**
   * The check provider.
   *
   * @var \Drupal\Core\Access\CheckProviderInterface
   */
  protected $checkProvider;

95
  /**
96
   * Constructs the RouteBuilder using the passed MatcherDumperInterface.
97
   *
98
   * @param \Drupal\Core\Routing\MatcherDumperInterface $dumper
99
   *   The matcher dumper used to store the route information.
100 101
   * @param \Drupal\Core\Lock\LockBackendInterface $lock
   *   The lock backend.
102
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
103
   *   The event dispatcher to notify of routes.
104 105
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
106 107
   * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
   *   The controller resolver.
108 109
   * @param \Drupal\Core\Access\CheckProviderInterface $check_provider
   *   The check provider.
110 111
   * @param \Drupal\Core\Routing\RouteBuilderIndicatorInterface $route_build_indicator
   *   The route build indicator.
112
   */
113
  public function __construct(MatcherDumperInterface $dumper, LockBackendInterface $lock, EventDispatcherInterface $dispatcher, ModuleHandlerInterface $module_handler, ControllerResolverInterface $controller_resolver, CheckProviderInterface $check_provider, RouteBuilderIndicatorInterface $route_build_indicator = NULL) {
114
    $this->dumper = $dumper;
115
    $this->lock = $lock;
116
    $this->dispatcher = $dispatcher;
117
    $this->moduleHandler = $module_handler;
118
    $this->controllerResolver = $controller_resolver;
119
    $this->routeBuilderIndicator = $route_build_indicator;
120
    $this->checkProvider = $check_provider;
121 122
  }

123
  /**
124
   * {@inheritdoc}
125
   */
126
  public function rebuild() {
127 128 129 130
    if ($this->building) {
      throw new \RuntimeException('Recursive router rebuild detected.');
    }

131 132 133 134 135
    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');
136
      return FALSE;
137 138
    }

139 140
    $this->building = TRUE;

141 142 143
    $collection = new RouteCollection();
    $this->routeCollection = $collection;
    foreach ($this->getRouteDefinitions() as $routes) {
144 145 146
      // 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
147 148 149 150
      // 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.
151 152 153 154
      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)) {
155 156 157 158 159 160 161 162 163
            // If a RouteCollection is returned, add the whole collection.
            if ($callback_routes instanceof RouteCollection) {
              $collection->addCollection($callback_routes);
            }
            // Otherwise, add each Route object individually.
            else {
              foreach ($callback_routes as $name => $callback_route) {
                $collection->add($name, $callback_route);
              }
164 165 166 167 168
            }
          }
        }
        unset($routes['route_callbacks']);
      }
169 170 171 172 173 174 175
      foreach ($routes as $name => $route_info) {
        $route_info += array(
          'defaults' => array(),
          'requirements' => array(),
          'options' => array(),
        );

176
        $route = new Route($route_info['path'], $route_info['defaults'], $route_info['requirements'], $route_info['options']);
177
        $collection->add($name, $route);
178
      }
179

180
    }
181

182 183 184 185 186 187 188 189 190
    // 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));

191 192
    $this->checkProvider->setChecks($collection);

193
    $this->dumper->addRoutes($collection);
194
    $this->dumper->dump();
195

196
    $this->routeBuilderIndicator->setRebuildDone();
197
    $this->lock->release('router_rebuild');
198
    $this->dispatcher->dispatch(RoutingEvents::FINISHED, new Event());
199
    $this->building = FALSE;
200 201 202

    $this->routeCollection = NULL;

203 204 205
    return TRUE;
  }

206 207 208 209
  /**
   * {@inheritdoc}
   */
  public function rebuildIfNeeded() {
210
    if ($this->routeBuilderIndicator->isRebuildNeeded()) {
211 212 213 214 215 216 217 218 219
      return $this->rebuild();
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function setRebuildNeeded() {
220
    $this->routeBuilderIndicator->setRebuildNeeded();
221 222
  }

223
  /**
224
   * Retrieves all defined routes from .routing.yml files.
225
   *
226 227
   * @return array
   *   The defined routes, keyed by provider.
228
   */
229
  protected function getRouteDefinitions() {
230 231 232
    if (!isset($this->yamlDiscovery)) {
      $this->yamlDiscovery = new YamlDiscovery('routing', $this->moduleHandler->getModuleDirectories());
    }
233
    return $this->yamlDiscovery->findAll();
234 235 236
  }

}