Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Component/Plugin/LazyPluginCollection.php \Drupal\Component\Plugin\LazyPluginCollection
  2. 9 core/lib/Drupal/Component/Plugin/LazyPluginCollection.php \Drupal\Component\Plugin\LazyPluginCollection

Defines an object which stores multiple plugin instances to lazy load them.

Hierarchy

  • class \Drupal\Component\Plugin\LazyPluginCollection implements \Drupal\Component\Plugin\IteratorAggregate, \Drupal\Component\Plugin\Countable

Expanded class hierarchy of LazyPluginCollection

Related topics

3 files declare their use of LazyPluginCollection
DefaultLazyPluginCollection.php in core/lib/Drupal/Core/Plugin/DefaultLazyPluginCollection.php
DefaultSingleLazyPluginCollection.php in core/lib/Drupal/Core/Plugin/DefaultSingleLazyPluginCollection.php
TestLazyPluginCollection.php in core/modules/system/tests/modules/plugin_test/src/Plugin/TestLazyPluginCollection.php

File

core/lib/Drupal/Component/Plugin/LazyPluginCollection.php, line 10

Namespace

Drupal\Component\Plugin
View source
abstract class LazyPluginCollection implements \IteratorAggregate, \Countable {

  /**
   * Stores all instantiated plugins.
   *
   * @var array
   */
  protected $pluginInstances = [];

  /**
   * Stores the IDs of all potential plugin instances.
   *
   * @var array
   */
  protected $instanceIds = [];

  /**
   * Initializes and stores a plugin.
   *
   * @param string $instance_id
   *   The ID of the plugin instance to initialize.
   */
  protected abstract function initializePlugin($instance_id);

  /**
   * Gets the current configuration of all plugins in this collection.
   *
   * @return array
   *   An array of up-to-date plugin configuration.
   */
  public abstract function getConfiguration();

  /**
   * Sets the configuration for all plugins in this collection.
   *
   * @param array $configuration
   *   An array of up-to-date plugin configuration.
   *
   * @return $this
   */
  public abstract function setConfiguration($configuration);

  /**
   * Clears all instantiated plugins.
   */
  public function clear() {
    $this->pluginInstances = [];
  }

  /**
   * Determines if a plugin instance exists.
   *
   * @param string $instance_id
   *   The ID of the plugin instance to check.
   *
   * @return bool
   *   TRUE if the plugin instance exists, FALSE otherwise.
   */
  public function has($instance_id) {
    return isset($this->pluginInstances[$instance_id]) || isset($this->instanceIds[$instance_id]);
  }

  /**
   * Gets a plugin instance, initializing it if necessary.
   *
   * @param string $instance_id
   *   The ID of the plugin instance being retrieved.
   */
  public function &get($instance_id) {
    if (!isset($this->pluginInstances[$instance_id])) {
      $this
        ->initializePlugin($instance_id);
    }
    return $this->pluginInstances[$instance_id];
  }

  /**
   * Stores an initialized plugin.
   *
   * @param string $instance_id
   *   The ID of the plugin instance being stored.
   * @param mixed $value
   *   An instantiated plugin.
   */
  public function set($instance_id, $value) {
    $this->pluginInstances[$instance_id] = $value;
    $this
      ->addInstanceId($instance_id);
  }

  /**
   * Removes an initialized plugin.
   *
   * The plugin can still be used; it will be reinitialized.
   *
   * @param string $instance_id
   *   The ID of the plugin instance to remove.
   */
  public function remove($instance_id) {
    unset($this->pluginInstances[$instance_id]);
  }

  /**
   * Adds an instance ID to the available instance IDs.
   *
   * @param string $id
   *   The ID of the plugin instance to add.
   * @param array|null $configuration
   *   (optional) The configuration used by this instance. Defaults to NULL.
   */
  public function addInstanceId($id, $configuration = NULL) {
    if (!isset($this->instanceIds[$id])) {
      $this->instanceIds[$id] = $id;
    }
  }

  /**
   * Gets all instance IDs.
   *
   * @return array
   *   An array of all available instance IDs.
   */
  public function getInstanceIds() {
    return $this->instanceIds;
  }

  /**
   * Removes an instance ID.
   *
   * @param string $instance_id
   *   The ID of the plugin instance to remove.
   */
  public function removeInstanceId($instance_id) {
    unset($this->instanceIds[$instance_id]);
    $this
      ->remove($instance_id);
  }

  #[\ReturnTypeWillChange]
  public function getIterator() {
    $instances = [];
    foreach ($this
      ->getInstanceIds() as $instance_id) {
      $instances[$instance_id] = $this
        ->get($instance_id);
    }
    return new \ArrayIterator($instances);
  }

  /**
   * {@inheritdoc}
   */

  #[\ReturnTypeWillChange]
  public function count() {
    return count($this->instanceIds);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LazyPluginCollection::$instanceIds protected property Stores the IDs of all potential plugin instances.
LazyPluginCollection::$pluginInstances protected property Stores all instantiated plugins.
LazyPluginCollection::addInstanceId public function Adds an instance ID to the available instance IDs. 2
LazyPluginCollection::clear public function Clears all instantiated plugins. 1
LazyPluginCollection::count public function
LazyPluginCollection::get public function Gets a plugin instance, initializing it if necessary. 8
LazyPluginCollection::getConfiguration abstract public function Gets the current configuration of all plugins in this collection. 3
LazyPluginCollection::getInstanceIds public function Gets all instance IDs.
LazyPluginCollection::getIterator public function
LazyPluginCollection::has public function Determines if a plugin instance exists.
LazyPluginCollection::initializePlugin abstract protected function Initializes and stores a plugin. 3
LazyPluginCollection::remove public function Removes an initialized plugin. 1
LazyPluginCollection::removeInstanceId public function Removes an instance ID. 1
LazyPluginCollection::set public function Stores an initialized plugin.
LazyPluginCollection::setConfiguration abstract public function Sets the configuration for all plugins in this collection. 3