You are here

trait IteratingServiceBaseTrait in Purge 8.3

Adds \Iterator logic to \Drupal\purge\ServiceInterface derivatives.

Hierarchy

4 files declare their use of IteratingServiceBaseTrait
DiagnosticsService.php in src/Plugin/Purge/DiagnosticCheck/DiagnosticsService.php
ProcessorsService.php in src/Plugin/Purge/Processor/ProcessorsService.php
QueuersService.php in src/Plugin/Purge/Queuer/QueuersService.php
TagsHeadersService.php in src/Plugin/Purge/TagsHeader/TagsHeadersService.php

File

src/IteratingServiceBaseTrait.php, line 8

Namespace

Drupal\purge
View source
trait IteratingServiceBaseTrait {

  /**
   * Current iterator position.
   *
   * @var int
   *
   * @ingroup iterator
   */
  protected $position = 0;

  /**
   * Holds all instantiated plugins.
   *
   * @var null|\Drupal\Component\Plugin\PluginInspectionInterface[]
   */
  protected $instances;

  /**
   * Instantiate all enabled plugins or check that they are present.
   */
  protected function initializePluginInstances() {
    if (!is_null($this->instances)) {
      return;
    }
    $this->instances = [];
    foreach ($this
      ->getPluginsEnabled() as $plugin_id) {
      $this->instances[] = $this->pluginManager
        ->createInstance($plugin_id);
    }
  }

  /**
   * Return the current element.
   *
   * @ingroup iterator
   */
  public function current() {
    $this
      ->initializePluginInstances();
    if ($this
      ->valid()) {
      return $this->instances[$this->position];
    }
    return FALSE;
  }

  /**
   * Return the key of the current element.
   *
   * @ingroup iterator
   */
  public function key() {
    $this
      ->initializePluginInstances();
    return $this->position;
  }

  /**
   * Move forward to next element.
   *
   * @ingroup iterator
   */
  public function next() {
    $this
      ->initializePluginInstances();
    ++$this->position;
  }

  /**
   * Rewind the iterator and destruct loaded plugin instances.
   *
   * @warning
   *   Reloading a service implies that all cached data will be reset and that
   *   plugins get reinstantiated during the current request, which should
   *   normally not be used. This method is specifically used in tests.
   *
   * @see \Drupal\purge\ServiceInterface::reload()
   */
  protected function reloadIterator() {
    $this->instances = NULL;
    $this
      ->rewind();
  }

  /**
   * Rewind the Iterator to the first element.
   *
   * @ingroup iterator
   */
  public function rewind() {
    $this->position = 0;
  }

  /**
   * Checks if current position is valid.
   *
   * @ingroup iterator
   */
  public function valid() {
    $this
      ->initializePluginInstances();
    return isset($this->instances[$this->position]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
IteratingServiceBaseTrait::$instances protected property Holds all instantiated plugins.
IteratingServiceBaseTrait::$position protected property Current iterator position.
IteratingServiceBaseTrait::current public function Return the current element.
IteratingServiceBaseTrait::initializePluginInstances protected function Instantiate all enabled plugins or check that they are present.
IteratingServiceBaseTrait::key public function Return the key of the current element.
IteratingServiceBaseTrait::next public function Move forward to next element. 1
IteratingServiceBaseTrait::reloadIterator protected function Rewind the iterator and destruct loaded plugin instances.
IteratingServiceBaseTrait::rewind public function Rewind the Iterator to the first element.
IteratingServiceBaseTrait::valid public function Checks if current position is valid.