LazyPluginCollection.php 3.7 KB

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