3 namespace Drupal\Core\Routing;
5 use Symfony\Cmf\Component\Routing\PagedRouteProviderInterface;
6 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7 use Symfony\Component\HttpFoundation\Request;
10 * A Route Provider front-end for all Drupal-stored routes.
12 class RouteProviderLazyBuilder implements PreloadableRouteProviderInterface, PagedRouteProviderInterface, EventSubscriberInterface {
15 * The route provider service.
17 * @var \Drupal\Core\Routing\RouteProviderInterface
19 protected $routeProvider;
22 * The route building service.
24 * @var \Drupal\Core\Routing\RouteBuilderInterface
26 protected $routeBuilder;
29 * Flag to determine if the router has been rebuilt.
33 protected $rebuilt = FALSE;
36 * Flag to determine if router is currently being rebuilt.
38 * Used to prevent recursive router rebuilds during module installation.
39 * Recursive rebuilds can occur when route information is required by alter
40 * hooks that are triggered during a rebuild, for example,
41 * hook_menu_links_discovered_alter().
45 protected $rebuilding = FALSE;
48 * RouteProviderLazyBuilder constructor.
50 * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
51 * The route provider service.
52 * @param \Drupal\Core\Routing\RouteBuilderInterface $route_builder
53 * The route building service.
55 public function __construct(RouteProviderInterface $route_provider, RouteBuilderInterface $route_builder) {
56 $this->routeProvider = $route_provider;
57 $this->routeBuilder = $route_builder;
61 * Gets the real route provider service and rebuilds the router id necessary.
63 * @return \Drupal\Core\Routing\RouteProviderInterface
64 * The route provider service.
66 protected function getRouteProvider() {
67 if (!$this->rebuilt && !$this->rebuilding) {
68 $this->routeBuilder->rebuild();
69 $this->rebuilt = TRUE;
71 return $this->routeProvider;
77 public function getRouteCollectionForRequest(Request $request) {
78 return $this->getRouteProvider()->getRouteCollectionForRequest($request);
84 public function getRouteByName($name) {
85 return $this->getRouteProvider()->getRouteByName($name);
91 public function preLoadRoutes($names) {
92 return $this->getRouteProvider()->preLoadRoutes($names);
98 public function getRoutesByNames($names) {
99 return $this->getRouteProvider()->getRoutesByNames($names);
105 public function getRoutesByPattern($pattern) {
106 return $this->getRouteProvider()->getRoutesByPattern($pattern);
112 public function getAllRoutes() {
113 return $this->getRouteProvider()->getAllRoutes();
119 public function reset() {
120 // Don't call getRouteProvider as this is results in recursive rebuilds.
121 return $this->routeProvider->reset();
127 public function getRoutesPaged($offset, $length = NULL) {
128 return $this->getRouteProvider()->getRoutesPaged($offset, $length);
134 public function getRoutesCount() {
135 return $this->getRouteProvider()->getRoutesCount();
139 * Determines if the router has been rebuilt.
142 * TRUE is the router has been rebuilt, FALSE if not.
144 public function hasRebuilt() {
145 return $this->rebuilt;
151 public static function getSubscribedEvents() {
152 $events[RoutingEvents::DYNAMIC][] = ['routerRebuilding', 3000];
153 $events[RoutingEvents::FINISHED][] = ['routerRebuildFinished', -3000];
158 * Sets the router rebuilding flag to TRUE.
160 public function routerRebuilding() {
161 $this->rebuilding = TRUE;
165 * Sets the router rebuilding flag to FALSE.
167 public function routerRebuildFinished() {
168 $this->rebuilding = FALSE;