You are here

class LazyRouteFilter in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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

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
route_filter.lazy_collector in core/core.services.yml
Drupal\Core\Routing\LazyRouteFilter

File

core/lib/Drupal/Core/Routing/LazyRouteFilter.php, line 24
Contains \Drupal\Core\Routing\LazyRouteFilter.

Namespace

Drupal\Core\Routing
View 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

Namesort descending Modifiers Type Description Overrides
ContainerAwareTrait::$container protected property
ContainerAwareTrait::setContainer public function Sets the Container associated with this Controller.
LazyRouteFilter::$filters protected property The initialized route filters.
LazyRouteFilter::$serviceIds protected property Array of route filter service IDs.
LazyRouteFilter::filter public function Filters the route collection against a request and returns all matching routes. Overrides RouteFilterInterface::filter
LazyRouteFilter::getFilters protected function For each route, gets a list of applicable enhancers to the route.
LazyRouteFilter::setFilters public function For each route, filter down the route collection.
LazyRouteFilter::__construct public function Constructs the LazyRouteEnhancer object.