DefaultLazyPluginCollection.php in Plug 7
File
lib/Drupal/Core/Plugin/DefaultLazyPluginCollection.php
View source
<?php
namespace Drupal\Core\Plugin;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Plugin\LazyPluginCollection;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
class DefaultLazyPluginCollection extends LazyPluginCollection {
protected $manager;
protected $configurations = array();
protected $pluginKey = 'id';
protected $originalOrder = array();
public function __construct(PluginManagerInterface $manager, array $configurations = array()) {
$this->manager = $manager;
$this->configurations = $configurations;
if (!empty($configurations)) {
$instance_ids = array_keys($configurations);
$this->instanceIDs = array_combine($instance_ids, $instance_ids);
$this->originalOrder = $this->instanceIDs;
}
}
protected function initializePlugin($instance_id) {
$configuration = isset($this->configurations[$instance_id]) ? $this->configurations[$instance_id] : array();
if (!isset($configuration[$this->pluginKey])) {
throw new PluginNotFoundException($instance_id);
}
$this
->set($instance_id, $this->manager
->createInstance($configuration[$this->pluginKey], $configuration));
}
public function sort() {
uasort($this->instanceIDs, array(
$this,
'sortHelper',
));
return $this;
}
public function sortHelper($aID, $bID) {
$a = $this
->get($aID);
$b = $this
->get($bID);
return strnatcasecmp($a
->getPluginId(), $b
->getPluginId());
}
public function getConfiguration() {
$instances = array();
$current_order = $this->instanceIDs;
$this->instanceIDs = $this->originalOrder + $current_order;
foreach ($this as $instance_id => $instance) {
if ($instance instanceof ConfigurablePluginInterface) {
$instances[$instance_id] = $instance
->getConfiguration();
}
else {
$instances[$instance_id] = $this->configurations[$instance_id];
}
}
$this->instanceIDs = $current_order;
return $instances;
}
public function setConfiguration($configuration) {
$unprocessed_instance_ids = $this
->getInstanceIds();
foreach ($configuration as $instance_id => $instance_configuration) {
$this
->setInstanceConfiguration($instance_id, $instance_configuration);
unset($unprocessed_instance_ids[$instance_id]);
}
foreach ($unprocessed_instance_ids as $unprocessed_instance_id) {
$this
->removeInstanceId($unprocessed_instance_id);
}
return $this;
}
public function setInstanceConfiguration($instance_id, array $configuration) {
$this->configurations[$instance_id] = $configuration;
$instance = $this
->get($instance_id);
if ($instance instanceof ConfigurablePluginInterface) {
$instance
->setConfiguration($configuration);
}
}
public function addInstanceId($id, $configuration = NULL) {
parent::addInstanceId($id);
if ($configuration !== NULL) {
$this
->setInstanceConfiguration($id, $configuration);
}
if (!isset($this->originalOrder[$id])) {
$this->originalOrder[$id] = $id;
}
}
public function removeInstanceId($instance_id) {
parent::removeInstanceId($instance_id);
unset($this->originalOrder[$instance_id]);
unset($this->configurations[$instance_id]);
}
}