RouteProviderLazyBuilder.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace Drupal\Core\Routing;
  3. use Symfony\Cmf\Component\Routing\PagedRouteProviderInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. /**
  7. * A Route Provider front-end for all Drupal-stored routes.
  8. */
  9. class RouteProviderLazyBuilder implements PreloadableRouteProviderInterface, PagedRouteProviderInterface, EventSubscriberInterface {
  10. /**
  11. * The route provider service.
  12. *
  13. * @var \Drupal\Core\Routing\RouteProviderInterface
  14. */
  15. protected $routeProvider;
  16. /**
  17. * The route building service.
  18. *
  19. * @var \Drupal\Core\Routing\RouteBuilderInterface
  20. */
  21. protected $routeBuilder;
  22. /**
  23. * Flag to determine if the router has been rebuilt.
  24. *
  25. * @var bool
  26. */
  27. protected $rebuilt = FALSE;
  28. /**
  29. * Flag to determine if router is currently being rebuilt.
  30. *
  31. * Used to prevent recursive router rebuilds during module installation.
  32. * Recursive rebuilds can occur when route information is required by alter
  33. * hooks that are triggered during a rebuild, for example,
  34. * hook_menu_links_discovered_alter().
  35. *
  36. * @var bool
  37. */
  38. protected $rebuilding = FALSE;
  39. /**
  40. * RouteProviderLazyBuilder constructor.
  41. *
  42. * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
  43. * The route provider service.
  44. * @param \Drupal\Core\Routing\RouteBuilderInterface $route_builder
  45. * The route building service.
  46. */
  47. public function __construct(RouteProviderInterface $route_provider, RouteBuilderInterface $route_builder) {
  48. $this->routeProvider = $route_provider;
  49. $this->routeBuilder = $route_builder;
  50. }
  51. /**
  52. * Gets the real route provider service and rebuilds the router id necessary.
  53. *
  54. * @return \Drupal\Core\Routing\RouteProviderInterface
  55. * The route provider service.
  56. */
  57. protected function getRouteProvider() {
  58. if (!$this->rebuilt && !$this->rebuilding) {
  59. $this->routeBuilder->rebuild();
  60. $this->rebuilt = TRUE;
  61. }
  62. return $this->routeProvider;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function getRouteCollectionForRequest(Request $request) {
  68. return $this->getRouteProvider()->getRouteCollectionForRequest($request);
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getRouteByName($name) {
  74. return $this->getRouteProvider()->getRouteByName($name);
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function preLoadRoutes($names) {
  80. return $this->getRouteProvider()->preLoadRoutes($names);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function getRoutesByNames($names) {
  86. return $this->getRouteProvider()->getRoutesByNames($names);
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function getRoutesByPattern($pattern) {
  92. return $this->getRouteProvider()->getRoutesByPattern($pattern);
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function getAllRoutes() {
  98. return $this->getRouteProvider()->getAllRoutes();
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function reset() {
  104. // Don't call getRouteProvider as this is results in recursive rebuilds.
  105. return $this->routeProvider->reset();
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function getRoutesPaged($offset, $length = NULL) {
  111. return $this->getRouteProvider()->getRoutesPaged($offset, $length);
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function getRoutesCount() {
  117. return $this->getRouteProvider()->getRoutesCount();
  118. }
  119. /**
  120. * Determines if the router has been rebuilt.
  121. *
  122. * @return bool
  123. * TRUE is the router has been rebuilt, FALSE if not.
  124. */
  125. public function hasRebuilt() {
  126. return $this->rebuilt;
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public static function getSubscribedEvents() {
  132. $events[RoutingEvents::DYNAMIC][] = ['routerRebuilding', 3000];
  133. $events[RoutingEvents::FINISHED][] = ['routerRebuildFinished', -3000];
  134. return $events;
  135. }
  136. /**
  137. * Sets the router rebuilding flag to TRUE.
  138. */
  139. public function routerRebuilding() {
  140. $this->rebuilding = TRUE;
  141. }
  142. /**
  143. * Sets the router rebuilding flag to FALSE.
  144. */
  145. public function routerRebuildFinished() {
  146. $this->rebuilding = FALSE;
  147. }
  148. }