abstract class PluginBag

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

The \ArrayAccess implementation is only for backwards compatibility, it is deprecated and should not be used by new code.

Hierarchy

  • class \Drupal\Component\Plugin\PluginBag implements \Drupal\Component\Plugin\Iterator, \Drupal\Component\Plugin\Countable

Expanded class hierarchy of PluginBag

6 files declare their use of PluginBag
ActionBag.php in drupal/core/lib/Drupal/Core/Action/ActionBag.php
Contains \Drupal\Core\Action\ActionBag.
BlockPluginBag.php in drupal/core/modules/block/lib/Drupal/block/BlockPluginBag.php
Contains \Drupal\block\BlockPluginBag.
DisplayBag.php in drupal/core/modules/views/lib/Drupal/views/DisplayBag.php
Contains \Drupal\views\DisplayBag.
FilterBag.php in drupal/core/modules/filter/lib/Drupal/filter/FilterBag.php
Contains \Drupal\filter\FilterBag.
TestPluginBag.php in drupal/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/TestPluginBag.php
Contains \Drupal\plugin_test\Plugin\TestPluginBag.

... See full list

File

drupal/core/lib/Drupal/Component/Plugin/PluginBag.php, line 16
Contains \Drupal\Component\Plugin\PluginBag.

Namespace

Drupal\Component\Plugin
View source
abstract class PluginBag implements \Iterator, \Countable {

  /**
   * Stores all instantiated plugins.
   *
   * @var array
   */
  protected $pluginInstances = array();

  /**
   * Stores the IDs of all potential plugin instances.
   *
   * @var array
   */
  protected $instanceIDs = array();

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

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

  /**
   * 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]);
  }

  /**
   * Retrieves 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;
  }

  /**
   * 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 array of available instance IDs.
   *
   * @param string $id
   *   The ID of the plugin instance to add.
   */
  public function addInstanceID($id) {
    $this->instanceIDs[$id] = $id;
  }

  /**
   * Returns all instance IDs.
   *
   * @return array
   *   An array of all available instance IDs.
   */
  public function getInstanceIDs() {
    return $this->instanceIDs;
  }

  /**
   * Sets the instance IDs property.
   *
   * @param array $instance_ids
   *   An associative array of instance IDs.
   */
  public function setInstanceIDs(array $instance_ids) {
    $this->instanceIDs = $instance_ids;
  }

  /**
   * Implements \Iterator::current().
   */
  public function current() {
    return $this
      ->get($this
      ->key());
  }

  /**
   * Implements \Iterator::next().
   */
  public function next() {
    next($this->instanceIDs);
  }

  /**
   * Implements \Iterator::key().
   */
  public function key() {
    return key($this->instanceIDs);
  }

  /**
   * Implements \Iterator::valid().
   */
  public function valid() {
    $key = key($this->instanceIDs);
    return $key !== NULL && $key !== FALSE;
  }

  /**
   * Implements \Iterator::rewind().
   */
  public function rewind() {
    reset($this->instanceIDs);
  }

  /**
   * Implements \Countable::count().
   */
  public function count() {
    return count($this->instanceIDs);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PluginBag::$instanceIDs protected property Stores the IDs of all potential plugin instances.
PluginBag::$pluginInstances protected property Stores all instantiated plugins.
PluginBag::addInstanceID public function Adds an instance ID to the array of available instance IDs.
PluginBag::clear public function Clears all instantiated plugins. 1
PluginBag::count public function Implements \Countable::count().
PluginBag::current public function Implements \Iterator::current().
PluginBag::get public function Retrieves a plugin instance, initializing it if necessary.
PluginBag::getInstanceIDs public function Returns all instance IDs.
PluginBag::has public function Determines if a plugin instance exists.
PluginBag::initializePlugin abstract protected function Initializes a plugin and stores the result in $this->pluginInstances. 6
PluginBag::key public function Implements \Iterator::key().
PluginBag::next public function Implements \Iterator::next().
PluginBag::remove public function Removes an initialized plugin. 1
PluginBag::rewind public function Implements \Iterator::rewind().
PluginBag::set public function Stores an initialized plugin.
PluginBag::setInstanceIDs public function Sets the instance IDs property.
PluginBag::valid public function Implements \Iterator::valid().