3 namespace Drupal\Component\Plugin;
6 * Defines an object which stores multiple plugin instances to lazy load them.
10 abstract class LazyPluginCollection implements \IteratorAggregate, \Countable {
13 * Stores all instantiated plugins.
17 protected $pluginInstances = [];
20 * Stores the IDs of all potential plugin instances.
24 protected $instanceIDs = [];
27 * Initializes and stores a plugin.
29 * @param string $instance_id
30 * The ID of the plugin instance to initialize.
32 abstract protected function initializePlugin($instance_id);
35 * Gets the current configuration of all plugins in this collection.
38 * An array of up-to-date plugin configuration.
40 abstract public function getConfiguration();
43 * Sets the configuration for all plugins in this collection.
45 * @param array $configuration
46 * An array of up-to-date plugin configuration.
50 abstract public function setConfiguration($configuration);
53 * Clears all instantiated plugins.
55 public function clear() {
56 $this->pluginInstances = [];
60 * Determines if a plugin instance exists.
62 * @param string $instance_id
63 * The ID of the plugin instance to check.
66 * TRUE if the plugin instance exists, FALSE otherwise.
68 public function has($instance_id) {
69 return isset($this->pluginInstances[$instance_id]) || isset($this->instanceIDs[$instance_id]);
73 * Gets a plugin instance, initializing it if necessary.
75 * @param string $instance_id
76 * The ID of the plugin instance being retrieved.
78 public function &get($instance_id) {
79 if (!isset($this->pluginInstances[$instance_id])) {
80 $this->initializePlugin($instance_id);
82 return $this->pluginInstances[$instance_id];
86 * Stores an initialized plugin.
88 * @param string $instance_id
89 * The ID of the plugin instance being stored.
91 * An instantiated plugin.
93 public function set($instance_id, $value) {
94 $this->pluginInstances[$instance_id] = $value;
95 $this->addInstanceId($instance_id);
99 * Removes an initialized plugin.
101 * The plugin can still be used; it will be reinitialized.
103 * @param string $instance_id
104 * The ID of the plugin instance to remove.
106 public function remove($instance_id) {
107 unset($this->pluginInstances[$instance_id]);
111 * Adds an instance ID to the available instance IDs.
114 * The ID of the plugin instance to add.
115 * @param array|null $configuration
116 * (optional) The configuration used by this instance. Defaults to NULL.
118 public function addInstanceId($id, $configuration = NULL) {
119 if (!isset($this->instanceIDs[$id])) {
120 $this->instanceIDs[$id] = $id;
125 * Gets all instance IDs.
128 * An array of all available instance IDs.
130 public function getInstanceIds() {
131 return $this->instanceIDs;
135 * Removes an instance ID.
137 * @param string $instance_id
138 * The ID of the plugin instance to remove.
140 public function removeInstanceId($instance_id) {
141 unset($this->instanceIDs[$instance_id]);
142 $this->remove($instance_id);
145 public function getIterator() {
147 foreach ($this->getInstanceIds() as $instance_id) {
148 $instances[$instance_id] = $this->get($instance_id);
150 return new \ArrayIterator($instances);
156 public function count() {
157 return count($this->instanceIDs);