You are here

class LazyContextRepository in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Plugin/Context/LazyContextRepository.php \Drupal\Core\Plugin\Context\LazyContextRepository

Provides a context repository which uses context provider services.

Hierarchy

Expanded class hierarchy of LazyContextRepository

2 files declare their use of LazyContextRepository
BlockLibraryController.php in core/modules/block/src/Controller/BlockLibraryController.php
Contains \Drupal\block\Controller\BlockLibraryController.
LazyContextRepositoryTest.php in core/tests/Drupal/Tests/Core/Plugin/Context/LazyContextRepositoryTest.php
Contains \Drupal\Tests\Core\Plugin\Context\LazyContextRepositoryTest.
1 string reference to 'LazyContextRepository'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses LazyContextRepository
context.repository in core/core.services.yml
Drupal\Core\Plugin\Context\LazyContextRepository

File

core/lib/Drupal/Core/Plugin/Context/LazyContextRepository.php, line 15
Contains \Drupal\Core\Plugin\Context\LazyContextRepository.

Namespace

Drupal\Core\Plugin\Context
View source
class LazyContextRepository implements ContextRepositoryInterface {

  /**
   * The set of available context providers service IDs.
   *
   * @var string[]
   *   Context provider service IDs.
   */
  protected $contextProviderServiceIDs = [];

  /**
   * The service container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface
   */
  protected $container;

  /**
   * The statically cached contexts.
   *
   * @var \Drupal\Core\Plugin\Context\ContextInterface[]
   */
  protected $contexts = [];

  /**
   * Constructs a LazyContextRepository object.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The current service container.
   * @param string[] $context_provider_service_ids
   *   The set of the available context provider service IDs.
   */
  public function __construct(ContainerInterface $container, array $context_provider_service_ids) {
    $this->container = $container;
    $this->contextProviderServiceIDs = $context_provider_service_ids;
  }

  /**
   * {@inheritdoc}
   */
  public function getRuntimeContexts(array $context_ids) {
    $contexts = [];

    // Create a map of context providers (service IDs) to unqualified context
    // IDs.
    $context_ids_by_service = [];
    foreach ($context_ids as $id) {
      if (isset($this->contexts[$id])) {
        $contexts[$id] = $this->contexts[$id];
        continue;
      }

      // The IDs have been passed in @{service_id}:{unqualified_context_id}
      // format.
      // @todo Convert to an assert once https://www.drupal.org/node/2408013 is
      //   in.
      if ($id[0] === '@' && strpos($id, ':') !== FALSE) {
        list($service_id, $unqualified_context_id) = explode(':', $id, 2);

        // Remove the leading '@'.
        $service_id = substr($service_id, 1);
      }
      else {
        throw new \InvalidArgumentException('You must provide the context IDs in the @{service_id}:{unqualified_context_id} format.');
      }
      $context_ids_by_service[$service_id][] = $unqualified_context_id;
    }

    // Iterate over all missing context providers (services), gather the
    // runtime contexts and assign them as requested.
    foreach ($context_ids_by_service as $service_id => $unqualified_context_ids) {
      $contexts_by_service = $this->container
        ->get($service_id)
        ->getRuntimeContexts($unqualified_context_ids);
      $wanted_contexts = array_intersect_key($contexts_by_service, array_flip($unqualified_context_ids));
      foreach ($wanted_contexts as $unqualified_context_id => $context) {
        $context_id = '@' . $service_id . ':' . $unqualified_context_id;
        $this->contexts[$context_id] = $contexts[$context_id] = $context;
      }
    }
    return $contexts;
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableContexts() {
    $contexts = [];
    foreach ($this->contextProviderServiceIDs as $service_id) {
      $contexts_by_service = $this->container
        ->get($service_id)
        ->getAvailableContexts();
      foreach ($contexts_by_service as $unqualified_context_id => $context) {
        $context_id = '@' . $service_id . ':' . $unqualified_context_id;
        $contexts[$context_id] = $context;
      }
    }
    return $contexts;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LazyContextRepository::$container protected property The service container.
LazyContextRepository::$contextProviderServiceIDs protected property The set of available context providers service IDs.
LazyContextRepository::$contexts protected property The statically cached contexts.
LazyContextRepository::getAvailableContexts public function Gets all available contexts for the purposes of configuration. Overrides ContextRepositoryInterface::getAvailableContexts
LazyContextRepository::getRuntimeContexts public function Gets runtime context values for the given context IDs. Overrides ContextRepositoryInterface::getRuntimeContexts
LazyContextRepository::__construct public function Constructs a LazyContextRepository object.