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