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

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

8 9
namespace Drupal\Core\Routing;

10
use Drupal\Component\Discovery\YamlDiscovery;
11
use Drupal\Core\Controller\ControllerResolverInterface;
12 13 14 15 16
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

17
use Drupal\Core\Extension\ModuleHandlerInterface;
18
use Drupal\Core\Lock\LockBackendInterface;
19 20 21 22 23 24 25 26 27

/**
 * Managing class for rebuilding the router table.
 *
 * Because this class makes use of the modules system, it cannot currently
 * be unit tested.
 */
class RouteBuilder {

28 29 30 31 32
  /**
   * The dumper to which we should send collected routes.
   *
   * @var \Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface
   */
33 34
  protected $dumper;

35 36 37 38 39 40 41
  /**
   * The used lock backend instance.
   *
   * @var \Drupal\Core\Lock\LockBackendInterface $lock
   */
  protected $lock;

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

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

  /**
   * The module handler.
58 59 60 61 62
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

63 64 65 66 67 68 69
  /**
   * The controller resolver.
   *
   * @var \Drupal\Core\Controller\ControllerResolverInterface
   */
  protected $controllerResolver;

70
  /**
71
   * Construcs the RouteBuilder using the passed MatcherDumperInterface.
72
   *
73
   * @param \Drupal\Core\Routing\MatcherDumperInterface $dumper
74
   *   The matcher dumper used to store the route information.
75 76
   * @param \Drupal\Core\Lock\LockBackendInterface $lock
   *   The lock backend.
77
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
78
   *   The event dispatcher to notify of routes.
79 80
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
81 82
   * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
   *   The controller resolver.
83
   */
84
  public function __construct(MatcherDumperInterface $dumper, LockBackendInterface $lock, EventDispatcherInterface $dispatcher, ModuleHandlerInterface $module_handler, ControllerResolverInterface $controller_resolver) {
85
    $this->dumper = $dumper;
86
    $this->lock = $lock;
87
    $this->dispatcher = $dispatcher;
88
    $this->moduleHandler = $module_handler;
89
    $this->controllerResolver = $controller_resolver;
90 91
  }

92 93
  /**
   * Rebuilds the route info and dumps to dumper.
94 95 96
   *
   * @return bool
   *   Returns TRUE if the rebuild succeeds, FALSE otherwise.
97
   */
98
  public function rebuild() {
99 100 101 102 103
    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');
104
      return FALSE;
105 106
    }

107
    $yaml_discovery = $this->getYamlDiscovery();
108

109
    foreach ($yaml_discovery->findAll() as $provider => $routes) {
110
      $collection = new RouteCollection();
111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
      // 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, or as a new
      // \Symfony\Component\Routing\RouteCollection, which will be iterated over
      // and added to the collection for this provider.
      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)) {
            foreach ($callback_routes as $name => $callback_route) {
              $collection->add($name, $callback_route);
            }
          }
        }
        unset($routes['route_callbacks']);
      }
129 130 131 132 133 134 135
      foreach ($routes as $name => $route_info) {
        $route_info += array(
          'defaults' => array(),
          'requirements' => array(),
          'options' => array(),
        );

136
        $route = new Route($route_info['path'], $route_info['defaults'], $route_info['requirements'], $route_info['options']);
137
        $collection->add($name, $route);
138
      }
139

140
      $this->dispatcher->dispatch(RoutingEvents::ALTER, new RouteBuildEvent($collection, $provider));
141
      $this->dumper->addRoutes($collection);
142
      $this->dumper->dump(array('provider' => $provider));
143
    }
144 145

    // Now allow modules to register additional, dynamic routes.
146
    // @todo Either remove this alter or the per-provider alter.
147 148 149
    $collection = new RouteCollection();
    $this->dispatcher->dispatch(RoutingEvents::ALTER, new RouteBuildEvent($collection, 'dynamic_routes'));
    $this->dumper->addRoutes($collection);
150
    $this->dumper->dump(array('provider' => 'dynamic_routes'));
151

152
    $this->lock->release('router_rebuild');
153 154 155 156 157 158 159 160 161 162 163 164 165 166
    return TRUE;
  }

  /**
   * Returns the YAML discovery for getting all the .routing.yml files.
   *
   * @return \Drupal\Component\Discovery\YamlDiscovery
   *   The yaml discovery.
   */
  protected function getYamlDiscovery() {
    if (!isset($this->yamlDiscovery)) {
      $this->yamlDiscovery = new YamlDiscovery('routing', $this->moduleHandler->getModuleDirectories());
    }
    return $this->yamlDiscovery;
167 168 169
  }

}