Chris@0: routeProvider = $route_provider; Chris@0: $this->state = $state; Chris@0: $this->cache = $cache; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Loads all non-admin routes right before the actual page is rendered. Chris@0: * Chris@0: * @param \Symfony\Component\HttpKernel\Event\KernelEvent $event Chris@0: * The event to process. Chris@0: */ Chris@0: public function onRequest(KernelEvent $event) { Chris@0: // Only preload on normal HTML pages, as they will display menu links. Chris@0: if ($this->routeProvider instanceof PreloadableRouteProviderInterface && $event->getRequest()->getRequestFormat() == 'html') { Chris@0: Chris@0: // Ensure that the state query is cached to skip the database query, if Chris@0: // possible. Chris@0: $key = 'routing.non_admin_routes'; Chris@0: if ($cache = $this->cache->get($key)) { Chris@0: $routes = $cache->data; Chris@0: } Chris@0: else { Chris@0: $routes = $this->state->get($key, []); Chris@0: $this->cache->set($key, $routes, Cache::PERMANENT, ['routes']); Chris@0: } Chris@0: Chris@0: if ($routes) { Chris@0: // Preload all the non-admin routes at once. Chris@0: $this->routeProvider->preLoadRoutes($routes); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Alters existing routes for a specific collection. Chris@0: * Chris@0: * @param \Drupal\Core\Routing\RouteBuildEvent $event Chris@0: * The route build event. Chris@0: */ Chris@0: public function onAlterRoutes(RouteBuildEvent $event) { Chris@0: $collection = $event->getRouteCollection(); Chris@0: foreach ($collection->all() as $name => $route) { Chris@0: if (strpos($route->getPath(), '/admin/') !== 0 && $route->getPath() != '/admin') { Chris@0: $this->nonAdminRoutesOnRebuild[] = $name; Chris@0: } Chris@0: } Chris@0: $this->nonAdminRoutesOnRebuild = array_unique($this->nonAdminRoutesOnRebuild); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Store the non admin routes in state when the route building is finished. Chris@0: * Chris@0: * @param \Symfony\Component\EventDispatcher\Event $event Chris@0: * The route finish event. Chris@0: */ Chris@0: public function onFinishedRoutes(Event $event) { Chris@0: $this->state->set('routing.non_admin_routes', $this->nonAdminRoutesOnRebuild); Chris@0: $this->nonAdminRoutesOnRebuild = []; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function getSubscribedEvents() { Chris@0: // Set a really low priority to catch as many as possible routes. Chris@0: $events[RoutingEvents::ALTER] = ['onAlterRoutes', -1024]; Chris@0: $events[RoutingEvents::FINISHED] = ['onFinishedRoutes']; Chris@0: // Load the routes before the controller is executed (which happens after Chris@0: // the kernel request event). Chris@0: $events[KernelEvents::REQUEST][] = ['onRequest']; Chris@0: return $events; Chris@0: } Chris@0: Chris@0: }