comparison core/lib/Drupal/Component/Plugin/LazyPluginCollection.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Component\Plugin;
4
5 /**
6 * Defines an object which stores multiple plugin instances to lazy load them.
7 *
8 * @ingroup plugin_api
9 */
10 abstract class LazyPluginCollection implements \IteratorAggregate, \Countable {
11
12 /**
13 * Stores all instantiated plugins.
14 *
15 * @var array
16 */
17 protected $pluginInstances = [];
18
19 /**
20 * Stores the IDs of all potential plugin instances.
21 *
22 * @var array
23 */
24 protected $instanceIDs = [];
25
26 /**
27 * Initializes and stores a plugin.
28 *
29 * @param string $instance_id
30 * The ID of the plugin instance to initialize.
31 */
32 abstract protected function initializePlugin($instance_id);
33
34 /**
35 * Gets the current configuration of all plugins in this collection.
36 *
37 * @return array
38 * An array of up-to-date plugin configuration.
39 */
40 abstract public function getConfiguration();
41
42 /**
43 * Sets the configuration for all plugins in this collection.
44 *
45 * @param array $configuration
46 * An array of up-to-date plugin configuration.
47 *
48 * @return $this
49 */
50 abstract public function setConfiguration($configuration);
51
52 /**
53 * Clears all instantiated plugins.
54 */
55 public function clear() {
56 $this->pluginInstances = [];
57 }
58
59 /**
60 * Determines if a plugin instance exists.
61 *
62 * @param string $instance_id
63 * The ID of the plugin instance to check.
64 *
65 * @return bool
66 * TRUE if the plugin instance exists, FALSE otherwise.
67 */
68 public function has($instance_id) {
69 return isset($this->pluginInstances[$instance_id]) || isset($this->instanceIDs[$instance_id]);
70 }
71
72 /**
73 * Gets a plugin instance, initializing it if necessary.
74 *
75 * @param string $instance_id
76 * The ID of the plugin instance being retrieved.
77 */
78 public function &get($instance_id) {
79 if (!isset($this->pluginInstances[$instance_id])) {
80 $this->initializePlugin($instance_id);
81 }
82 return $this->pluginInstances[$instance_id];
83 }
84
85 /**
86 * Stores an initialized plugin.
87 *
88 * @param string $instance_id
89 * The ID of the plugin instance being stored.
90 * @param mixed $value
91 * An instantiated plugin.
92 */
93 public function set($instance_id, $value) {
94 $this->pluginInstances[$instance_id] = $value;
95 $this->addInstanceId($instance_id);
96 }
97
98 /**
99 * Removes an initialized plugin.
100 *
101 * The plugin can still be used; it will be reinitialized.
102 *
103 * @param string $instance_id
104 * The ID of the plugin instance to remove.
105 */
106 public function remove($instance_id) {
107 unset($this->pluginInstances[$instance_id]);
108 }
109
110 /**
111 * Adds an instance ID to the available instance IDs.
112 *
113 * @param string $id
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.
117 */
118 public function addInstanceId($id, $configuration = NULL) {
119 if (!isset($this->instanceIDs[$id])) {
120 $this->instanceIDs[$id] = $id;
121 }
122 }
123
124 /**
125 * Gets all instance IDs.
126 *
127 * @return array
128 * An array of all available instance IDs.
129 */
130 public function getInstanceIds() {
131 return $this->instanceIDs;
132 }
133
134 /**
135 * Removes an instance ID.
136 *
137 * @param string $instance_id
138 * The ID of the plugin instance to remove.
139 */
140 public function removeInstanceId($instance_id) {
141 unset($this->instanceIDs[$instance_id]);
142 $this->remove($instance_id);
143 }
144
145 public function getIterator() {
146 $instances = [];
147 foreach ($this->getInstanceIds() as $instance_id) {
148 $instances[$instance_id] = $this->get($instance_id);
149 }
150 return new \ArrayIterator($instances);
151 }
152
153 /**
154 * {@inheritdoc}
155 */
156 public function count() {
157 return count($this->instanceIDs);
158 }
159
160 }