PluginCollection.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright 2005-2011, Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.6.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Core;
  15. use ArrayIterator;
  16. use Cake\Core\Exception\MissingPluginException;
  17. use Countable;
  18. use InvalidArgumentException;
  19. use Iterator;
  20. use RuntimeException;
  21. /**
  22. * Plugin Collection
  23. *
  24. * Holds onto plugin objects loaded into an application, and
  25. * provides methods for iterating, and finding plugins based
  26. * on criteria.
  27. *
  28. * This class implements the Iterator interface to allow plugins
  29. * to be iterated, handling the situation where a plugin's hook
  30. * method (usually bootstrap) loads another plugin during iteration.
  31. */
  32. class PluginCollection implements Iterator, Countable
  33. {
  34. /**
  35. * Plugin list
  36. *
  37. * @var array
  38. */
  39. protected $plugins = [];
  40. /**
  41. * Names of plugins
  42. *
  43. * @var array
  44. */
  45. protected $names = [];
  46. /**
  47. * Iterator position.
  48. *
  49. * @var int
  50. */
  51. protected $position = 0;
  52. /**
  53. * Constructor
  54. *
  55. * @param array $plugins The map of plugins to add to the collection.
  56. */
  57. public function __construct(array $plugins = [])
  58. {
  59. foreach ($plugins as $plugin) {
  60. $this->add($plugin);
  61. }
  62. }
  63. /**
  64. * Add a plugin to the collection
  65. *
  66. * Plugins will be keyed by their names.
  67. *
  68. * @param \Cake\Core\PluginInterface $plugin The plugin to load.
  69. * @return $this
  70. */
  71. public function add(PluginInterface $plugin)
  72. {
  73. $name = $plugin->getName();
  74. $this->plugins[$name] = $plugin;
  75. $this->names = array_keys($this->plugins);
  76. return $this;
  77. }
  78. /**
  79. * Remove a plugin from the collection if it exists.
  80. *
  81. * @param string $name The named plugin.
  82. * @return $this
  83. */
  84. public function remove($name)
  85. {
  86. unset($this->plugins[$name]);
  87. $this->names = array_keys($this->plugins);
  88. return $this;
  89. }
  90. /**
  91. * Check whether the named plugin exists in the collection.
  92. *
  93. * @param string $name The named plugin.
  94. * @return bool
  95. */
  96. public function has($name)
  97. {
  98. return isset($this->plugins[$name]);
  99. }
  100. /**
  101. * Get the a plugin by name
  102. *
  103. * @param string $name The plugin to get.
  104. * @return \Cake\Core\PluginInterface The plugin.
  105. * @throws \Cake\Core\Exception\MissingPluginException when unknown plugins are fetched.
  106. */
  107. public function get($name)
  108. {
  109. if (!$this->has($name)) {
  110. throw new MissingPluginException(['plugin' => $name]);
  111. }
  112. return $this->plugins[$name];
  113. }
  114. /**
  115. * Part of Iterator Interface
  116. *
  117. * @return void
  118. */
  119. public function next()
  120. {
  121. $this->position++;
  122. }
  123. /**
  124. * Part of Iterator Interface
  125. *
  126. * @return string
  127. */
  128. public function key()
  129. {
  130. return $this->names[$this->position];
  131. }
  132. /**
  133. * Part of Iterator Interface
  134. *
  135. * @return \Cake\Core\PluginInterface
  136. */
  137. public function current()
  138. {
  139. $name = $this->names[$this->position];
  140. return $this->plugins[$name];
  141. }
  142. /**
  143. * Part of Iterator Interface
  144. *
  145. * @return void
  146. */
  147. public function rewind()
  148. {
  149. $this->position = 0;
  150. }
  151. /**
  152. * Part of Iterator Interface
  153. *
  154. * @return bool
  155. */
  156. public function valid()
  157. {
  158. return $this->position < count($this->plugins);
  159. }
  160. /**
  161. * Implementation of Countable.
  162. *
  163. * Get the number of plugins in the collection.
  164. *
  165. * @return int
  166. */
  167. public function count()
  168. {
  169. return count($this->plugins);
  170. }
  171. /**
  172. * Filter the plugins to those with the named hook enabled.
  173. *
  174. * @param string $hook The hook to filter plugins by
  175. * @return \Generator A generator containing matching plugins.
  176. * @throws \InvalidArgumentException on invalid hooks
  177. */
  178. public function with($hook)
  179. {
  180. if (!in_array($hook, PluginInterface::VALID_HOOKS)) {
  181. throw new InvalidArgumentException("The `{$hook}` hook is not a known plugin hook.");
  182. }
  183. foreach ($this as $plugin) {
  184. if ($plugin->isEnabled($hook)) {
  185. yield $plugin;
  186. }
  187. }
  188. }
  189. }