RouteBuilder.php 4 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 Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface;
11 12 13 14 15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

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

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

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

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

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

48 49 50 51 52 53 54
  /**
   * The extension handler for retieving the list of enabled modules.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

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

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

84 85
    $parser = new Parser();

86 87
    // We need to manually call each module so that we can know which module
    // a given item came from.
88
    foreach ($this->moduleHandler->getModuleList() as $module => $filename) {
89
      $collection = new RouteCollection();
90
      $routing_file = DRUPAL_ROOT . '/' . dirname($filename) . '/' . $module . '.routing.yml';
91 92 93 94 95 96
      if (file_exists($routing_file)) {
        $routes = $parser->parse(file_get_contents($routing_file));
        if (!empty($routes)) {
          foreach ($routes as $name => $route_info) {
            $defaults = isset($route_info['defaults']) ? $route_info['defaults'] : array();
            $requirements = isset($route_info['requirements']) ? $route_info['requirements'] : array();
97 98
            $options = isset($route_info['options']) ? $route_info['options'] : array();
            $route = new Route($route_info['pattern'], $defaults, $requirements, $options);
99 100 101 102 103 104
            $collection->add($name, $route);
          }
        }
      }
      $this->dispatcher->dispatch(RoutingEvents::ALTER, new RouteBuildEvent($collection, $module));
      $this->dumper->addRoutes($collection);
105 106
      $this->dumper->dump(array('route_set' => $module));
    }
107 108 109 110 111 112 113 114

    // 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'));

115
    $this->lock->release('router_rebuild');
116 117 118
  }

}