RouteBuilder.php 1.95 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\Core\Lock\LockBackendInterface;
11
use Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface;
12 13 14 15 16 17 18 19 20

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

21 22 23 24 25
  /**
   * The dumper to which we should send collected routes.
   *
   * @var \Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface
   */
26 27
  protected $dumper;

28 29 30 31 32 33 34
  /**
   * The used lock backend instance.
   *
   * @var \Drupal\Core\Lock\LockBackendInterface $lock
   */
  protected $lock;

35
  /**
36
   * Construcs the RouteBuilder using the passed MatcherDumperInterface.
37
   *
38
   * @param \Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface $dumper
39
   *   The matcher dumper used to store the route information.
40 41
   * @param \Drupal\Core\Lock\LockBackendInterface $lock
   *   The lock backend.
42
   */
43
  public function __construct(MatcherDumperInterface $dumper, LockBackendInterface $lock) {
44
    $this->dumper = $dumper;
45
    $this->lock = $lock;
46 47
  }

48 49 50
  /**
   * Rebuilds the route info and dumps to dumper.
   */
51
  public function rebuild() {
52 53 54 55 56 57 58 59
    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;
    }

60 61
    // We need to manually call each module so that we can know which module
    // a given item came from.
62 63 64 65 66

    foreach (module_implements('route_info') as $module) {
      $routes = call_user_func($module . '_route_info');
      drupal_alter('router_info', $routes, $module);
      $this->dumper->addRoutes($routes);
67 68
      $this->dumper->dump(array('route_set' => $module));
    }
69
    $this->lock->release('router_rebuild');
70 71 72
  }

}