class LazyRouteEnhancer in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/lib/Drupal/Core/Routing/LazyRouteEnhancer.php \Drupal\Core\Routing\LazyRouteEnhancer
A route enhancer which lazily loads route enhancers, depending on the route.
We lazy initialize route enhancers, because otherwise all dependencies of all route enhancers 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\LazyRouteEnhancer implements RouteEnhancerInterface, ContainerAwareInterface uses ContainerAwareTrait
Expanded class hierarchy of LazyRouteEnhancer
1 file declares its use of LazyRouteEnhancer
- RouteEnhancerSubscriber.php in core/
lib/ Drupal/ Core/ EventSubscriber/ RouteEnhancerSubscriber.php - Contains \Drupal\Core\EventSubscriber\RouteEnhancerSubscriber.
1 string reference to 'LazyRouteEnhancer'
- core.services.yml in core/
core.services.yml - core/core.services.yml
1 service uses LazyRouteEnhancer
File
- core/
lib/ Drupal/ Core/ Routing/ LazyRouteEnhancer.php, line 25 - Contains \Drupal\Core\Routing\LazyRouteEnhancer.
Namespace
Drupal\Core\RoutingView source
class LazyRouteEnhancer implements BaseRouteEnhancerInterface, ContainerAwareInterface {
use ContainerAwareTrait;
/**
* Array of enhancers service IDs.
*
* @var array
*/
protected $serviceIds = [];
/**
* The initialized route enhancers.
*
* @var \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface[]|\Drupal\Core\Routing\Enhancer\RouteEnhancerInterface[]
*/
protected $enhancers = NULL;
/**
* Constructs the LazyRouteEnhancer object.
*
* @param $service_ids
* Array of enhancers service IDs.
*/
public function __construct($service_ids) {
$this->serviceIds = $service_ids;
}
/**
* For each route, saves a list of applicable enhancers to the route.
*
* @param \Symfony\Component\Routing\RouteCollection $route_collection
* A collection of routes to apply enhancer checks to.
*/
public function setEnhancers(RouteCollection $route_collection) {
/** @var \Symfony\Component\Routing\Route $route **/
foreach ($route_collection as $route_name => $route) {
$service_ids = [];
foreach ($this
->getEnhancers() as $service_id => $enhancer) {
if (!$enhancer instanceof RouteEnhancerInterface || $enhancer
->applies($route)) {
$service_ids[] = $service_id;
}
}
if ($service_ids) {
$route
->setOption('_route_enhancers', array_unique($service_ids));
}
}
}
/**
* For each route, gets a list of applicable enhancer to the route.
*
* @return \Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface[]|\Drupal\Core\Routing\Enhancer\RouteEnhancerInterface[]
*/
protected function getEnhancers() {
if (!isset($this->enhancers)) {
foreach ($this->serviceIds as $service_id) {
$this->enhancers[$service_id] = $this->container
->get($service_id);
}
}
return $this->enhancers;
}
/**
* {@inheritdoc}
*/
public function enhance(array $defaults, Request $request) {
/** @var \Symfony\Component\Routing\Route $route */
$route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
$enhancer_ids = $route
->getOption('_route_enhancers');
if (isset($enhancer_ids)) {
foreach ($enhancer_ids as $enhancer_id) {
$defaults = $this->container
->get($enhancer_id)
->enhance($defaults, $request);
}
}
return $defaults;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContainerAwareTrait:: |
protected | property | ||
ContainerAwareTrait:: |
public | function | Sets the Container associated with this Controller. | |
LazyRouteEnhancer:: |
protected | property | The initialized route enhancers. | |
LazyRouteEnhancer:: |
protected | property | Array of enhancers service IDs. | |
LazyRouteEnhancer:: |
public | function |
Update the defaults based on its own data and the request. Overrides RouteEnhancerInterface:: |
|
LazyRouteEnhancer:: |
protected | function | For each route, gets a list of applicable enhancer to the route. | |
LazyRouteEnhancer:: |
public | function | For each route, saves a list of applicable enhancers to the route. | |
LazyRouteEnhancer:: |
public | function | Constructs the LazyRouteEnhancer object. |