diff --git a/core/lib/Drupal/Component/Plugin/PluginBag.php b/core/lib/Drupal/Component/Plugin/PluginBag.php index 671a70a..9487d8c 100644 --- a/core/lib/Drupal/Component/Plugin/PluginBag.php +++ b/core/lib/Drupal/Component/Plugin/PluginBag.php @@ -9,6 +9,9 @@ /** * Defines an object which stores multiple plugin instances to lazy load them. + * + * The \ArrayAccess implementation is only for backwards compatibility, it is + * deprecated and should not be used by new code. */ abstract class PluginBag implements \ArrayAccess, \Iterator, \Countable { @@ -42,7 +45,56 @@ public function clear() { } /** + * 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]); + } + + /** + * Retrieves 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; + } + + /** + * Removes an initialized plugin. + * + * The plugin can still be used, it will be reinitialized. + */ + public function remove($instance_id) { + unset($this->pluginInstances[$instance_id]); + } + + /** * Implements \ArrayAccess::offsetExists(). + * + * This is deprecated, use \Drupal\Component\Plugin\PluginBag::has(). */ public function offsetExists($offset) { return isset($this->pluginInstances[$offset]) || isset($this->instanceIDs[$offset]); @@ -50,6 +102,8 @@ public function offsetExists($offset) { /** * Implements \ArrayAccess::offsetGet(). + * + * This is deprecated, use \Drupal\Component\Plugin\PluginBag::get(). */ public function offsetGet($offset) { if (!isset($this->pluginInstances[$offset])) { @@ -60,6 +114,8 @@ public function offsetGet($offset) { /** * Implements \ArrayAccess::offsetSet(). + * + * This is deprecated, use \Drupal\Component\Plugin\PluginBag::set(). */ public function offsetSet($offset, $value) { $this->pluginInstances[$offset] = $value; @@ -67,6 +123,8 @@ public function offsetSet($offset, $value) { /** * Implements \ArrayAccess::offsetUnset(). + * + * This is deprecated, use \Drupal\Component\Plugin\PluginBag::remove(). */ public function offsetUnset($offset) { unset($this->pluginInstances[$offset]);