class LazyRouteFilter in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/Routing/LazyRouteFilter.php \Drupal\Core\Routing\LazyRouteFilter
A route filter which lazily loads route filters, depending on the route.
We lazy initialize route filters, because otherwise all dependencies of all route filters are initialized on every request, which is slow. However, with the use of lazy loading, dependencies are instantiated only when used.
Hierarchy
- class \Drupal\Core\Routing\LazyRouteFilter implements RouteFilterInterface, ContainerAwareInterface uses ContainerAwareTrait
Expanded class hierarchy of LazyRouteFilter
1 file declares its use of LazyRouteFilter
- RouteFilterSubscriber.php in core/
lib/ Drupal/ Core/ EventSubscriber/ RouteFilterSubscriber.php - Contains \Drupal\Core\EventSubscriber\RouteFilterSubscriber.
1 string reference to 'LazyRouteFilter'
- core.services.yml in core/
core.services.yml - core/core.services.yml
1 service uses LazyRouteFilter
File
- core/
lib/ Drupal/ Core/ Routing/ LazyRouteFilter.php, line 24 - Contains \Drupal\Core\Routing\LazyRouteFilter.
Namespace
Drupal\Core\RoutingView source
class LazyRouteFilter implements BaseRouteFilterInterface, ContainerAwareInterface {
use ContainerAwareTrait;
/**
* Array of route filter service IDs.
*
* @var array
*/
protected $serviceIds = [];
/**
* The initialized route filters.
*
* @var \Drupal\Core\Routing\RouteFilterInterface[]
*/
protected $filters = NULL;
/**
* Constructs the LazyRouteEnhancer object.
*
* @param $service_ids
* Array of route filter service IDs.
*/
public function __construct($service_ids) {
$this->serviceIds = $service_ids;
}
/**
* For each route, filter down the route collection.
*
* @param \Symfony\Component\Routing\RouteCollection $route_collection
* A collection of routes to apply filter checks to.
*/
public function setFilters(RouteCollection $route_collection) {
/** @var \Symfony\Component\Routing\Route $route **/
foreach ($route_collection as $route) {
$service_ids = [];
foreach ($this
->getFilters() as $service_id => $filter) {
if ($filter instanceof RouteFilterInterface && $filter
->applies($route)) {
$service_ids[] = $service_id;
}
}
if ($service_ids) {
$route
->setOption('_route_filters', array_unique($service_ids));
}
}
}
/**
* For each route, gets a list of applicable enhancers to the route.
*
* @return \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface[]|\Drupal\Core\Routing\Enhancer\RouteEnhancerInterface[]
*/
protected function getFilters() {
if (!isset($this->filters)) {
foreach ($this->serviceIds as $service_id) {
$this->filters[$service_id] = $this->container
->get($service_id);
}
}
return $this->filters;
}
/**
* {@inheritdoc}
*/
public function filter(RouteCollection $collection, Request $request) {
$filter_ids = [];
foreach ($collection
->all() as $route) {
$filter_ids = array_merge($filter_ids, $route
->getOption('_route_filters') ?: []);
}
$filter_ids = array_unique($filter_ids);
if (isset($filter_ids)) {
foreach ($filter_ids as $filter_id) {
if ($filter = $this->container
->get($filter_id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
$collection = $filter
->filter($collection, $request);
}
}
}
return $collection;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContainerAwareTrait:: |
protected | property | ||
ContainerAwareTrait:: |
public | function | Sets the Container associated with this Controller. | |
LazyRouteFilter:: |
protected | property | The initialized route filters. | |
LazyRouteFilter:: |
protected | property | Array of route filter service IDs. | |
LazyRouteFilter:: |
public | function |
Filters the route collection against a request and returns all matching
routes. Overrides RouteFilterInterface:: |
|
LazyRouteFilter:: |
protected | function | For each route, gets a list of applicable enhancers to the route. | |
LazyRouteFilter:: |
public | function | For each route, filter down the route collection. | |
LazyRouteFilter:: |
public | function | Constructs the LazyRouteEnhancer object. |