namespace Drupal\Core\Routing;
use Symfony\Cmf\Component\Routing\PagedRouteProviderInterface;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* A Route Provider front-end for all Drupal-stored routes.
*/
-class RouteProviderLazyBuilder implements PreloadableRouteProviderInterface, PagedRouteProviderInterface {
+class RouteProviderLazyBuilder implements PreloadableRouteProviderInterface, PagedRouteProviderInterface, EventSubscriberInterface {
/**
* The route provider service.
*/
protected $rebuilt = FALSE;
+ /**
+ * Flag to determine if router is currently being rebuilt.
+ *
+ * Used to prevent recursive router rebuilds during module installation.
+ * Recursive rebuilds can occur when route information is required by alter
+ * hooks that are triggered during a rebuild, for example,
+ * hook_menu_links_discovered_alter().
+ *
+ * @var bool
+ */
+ protected $rebuilding = FALSE;
+
/**
* RouteProviderLazyBuilder constructor.
*
* The route provider service.
*/
protected function getRouteProvider() {
- if (!$this->rebuilt) {
+ if (!$this->rebuilt && !$this->rebuilding) {
$this->routeBuilder->rebuild();
$this->rebuilt = TRUE;
}
return $this->rebuilt;
}
+ /**
+ * {@inheritdoc}
+ */
+ public static function getSubscribedEvents() {
+ $events[RoutingEvents::DYNAMIC][] = ['routerRebuilding', 3000];
+ $events[RoutingEvents::FINISHED][] = ['routerRebuildFinished', -3000];
+ return $events;
+ }
+
+ /**
+ * Sets the router rebuilding flag to TRUE.
+ */
+ public function routerRebuilding() {
+ $this->rebuilding = TRUE;
+ }
+
+ /**
+ * Sets the router rebuilding flag to FALSE.
+ */
+ public function routerRebuildFinished() {
+ $this->rebuilding = FALSE;
+ }
+
}