trait IteratingServiceBaseTrait in Purge 8.3
Adds \Iterator logic to \Drupal\purge\ServiceInterface derivatives.
Hierarchy
- trait \Drupal\purge\IteratingServiceBaseTrait
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\purgeView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
IteratingServiceBaseTrait:: |
protected | property | Holds all instantiated plugins. | |
IteratingServiceBaseTrait:: |
protected | property | Current iterator position. | |
IteratingServiceBaseTrait:: |
public | function | Return the current element. | |
IteratingServiceBaseTrait:: |
protected | function | Instantiate all enabled plugins or check that they are present. | |
IteratingServiceBaseTrait:: |
public | function | Return the key of the current element. | |
IteratingServiceBaseTrait:: |
public | function | Move forward to next element. | 1 |
IteratingServiceBaseTrait:: |
protected | function | Rewind the iterator and destruct loaded plugin instances. | |
IteratingServiceBaseTrait:: |
public | function | Rewind the Iterator to the first element. | |
IteratingServiceBaseTrait:: |
public | function | Checks if current position is valid. |