RouteBuilder.php 3.63 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 Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface;
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
  /**
   * The extension handler for retieving the list of enabled modules.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

56
  /**
57
   * Construcs the RouteBuilder using the passed MatcherDumperInterface.
58
   *
59
   * @param \Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface $dumper
60
   *   The matcher dumper used to store the route information.
61 62
   * @param \Drupal\Core\Lock\LockBackendInterface $lock
   *   The lock backend.
63 64
   * @param \Symfony\Component\EventDispatcherEventDispatcherInterface
   *   The event dispatcher to notify of routes.
65
   */
66
  public function __construct(MatcherDumperInterface $dumper, LockBackendInterface $lock, EventDispatcherInterface $dispatcher, ModuleHandlerInterface $module_handler) {
67
    $this->dumper = $dumper;
68
    $this->lock = $lock;
69
    $this->dispatcher = $dispatcher;
70
    $this->moduleHandler = $module_handler;
71 72
  }

73 74 75
  /**
   * Rebuilds the route info and dumps to dumper.
   */
76
  public function rebuild() {
77 78 79 80 81 82 83 84
    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;
    }

85
    $yaml_discovery = new YamlDiscovery('routing', $this->moduleHandler->getModuleDirectories());
86

87
    foreach ($yaml_discovery->findAll() as $module => $routes) {
88
      $collection = new RouteCollection();
89 90 91 92 93 94 95 96

      foreach ($routes as $name => $route_info) {
        $route_info += array(
          'defaults' => array(),
          'requirements' => array(),
          'options' => array(),
        );

97
        $route = new Route($route_info['path'], $route_info['defaults'], $route_info['requirements'], $route_info['options']);
98
        $collection->add($name, $route);
99
      }
100

101 102
      $this->dispatcher->dispatch(RoutingEvents::ALTER, new RouteBuildEvent($collection, $module));
      $this->dumper->addRoutes($collection);
103 104
      $this->dumper->dump(array('route_set' => $module));
    }
105 106 107 108 109 110 111 112

    // Now allow modules to register additional, dynamic routes.
    $collection = new RouteCollection();
    $this->dispatcher->dispatch(RoutingEvents::DYNAMIC, new RouteBuildEvent($collection, 'dynamic_routes'));
    $this->dispatcher->dispatch(RoutingEvents::ALTER, new RouteBuildEvent($collection, 'dynamic_routes'));
    $this->dumper->addRoutes($collection);
    $this->dumper->dump(array('route_set' => 'dynamic_routes'));

113
    $this->lock->release('router_rebuild');
114 115 116
  }

}