PathRootsSubscriber.php in Drupal 8
File
core/lib/Drupal/Core/EventSubscriber/PathRootsSubscriber.php
View source
<?php
namespace Drupal\Core\EventSubscriber;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PathRootsSubscriber implements EventSubscriberInterface {
protected $pathRoots = [];
protected $state;
public function __construct(StateInterface $state) {
$this->state = $state;
}
public function onRouteAlter(RouteBuildEvent $event) {
$collection = $event
->getRouteCollection();
foreach ($collection
->all() as $route) {
$bits = explode('/', ltrim($route
->getPath(), '/'));
$this->pathRoots[$bits[0]] = $bits[0];
}
}
public function onRouteFinished() {
$this->state
->set('router.path_roots', array_keys($this->pathRoots));
$this->pathRoots = [];
}
public static function getSubscribedEvents() {
$events = [];
$events[RoutingEvents::ALTER][] = [
'onRouteAlter',
-1024,
];
$events[RoutingEvents::FINISHED][] = [
'onRouteFinished',
];
return $events;
}
}