PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/cakephp/core/PluginCollection.php

http://github.com/josegonzalez/git-php
PHP | 270 lines | 111 code | 27 blank | 132 comment | 8 complexity | de77f4abda1edfc124c79eb15a697d36 MD5 | raw file
  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 Cake\Core\Exception\MissingPluginException;
  16. use Countable;
  17. use InvalidArgumentException;
  18. use Iterator;
  19. /**
  20. * Plugin Collection
  21. *
  22. * Holds onto plugin objects loaded into an application, and
  23. * provides methods for iterating, and finding plugins based
  24. * on criteria.
  25. *
  26. * This class implements the Iterator interface to allow plugins
  27. * to be iterated, handling the situation where a plugin's hook
  28. * method (usually bootstrap) loads another plugin during iteration.
  29. */
  30. class PluginCollection implements Iterator, Countable
  31. {
  32. /**
  33. * Plugin list
  34. *
  35. * @var array
  36. */
  37. protected $plugins = [];
  38. /**
  39. * Names of plugins
  40. *
  41. * @var array
  42. */
  43. protected $names = [];
  44. /**
  45. * Iterator position.
  46. *
  47. * @var int
  48. */
  49. protected $position = 0;
  50. /**
  51. * Constructor
  52. *
  53. * @param array $plugins The map of plugins to add to the collection.
  54. */
  55. public function __construct(array $plugins = [])
  56. {
  57. foreach ($plugins as $plugin) {
  58. $this->add($plugin);
  59. }
  60. $this->loadConfig();
  61. }
  62. /**
  63. * Load the path information stored in vendor/cakephp-plugins.php
  64. *
  65. * This file is generated by the cakephp/plugin-installer package and used
  66. * to locate plugins on the filesystem as applications can use `extra.plugin-paths`
  67. * in their composer.json file to move plugin outside of vendor/
  68. *
  69. * @internal
  70. * @return void
  71. */
  72. protected function loadConfig()
  73. {
  74. if (Configure::check('plugins')) {
  75. return;
  76. }
  77. $vendorFile = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'cakephp-plugins.php';
  78. if (!file_exists($vendorFile)) {
  79. $vendorFile = dirname(dirname(dirname(dirname(__DIR__)))) . DIRECTORY_SEPARATOR . 'cakephp-plugins.php';
  80. if (!file_exists($vendorFile)) {
  81. Configure::write(['plugins' => []]);
  82. return;
  83. }
  84. }
  85. $config = require $vendorFile;
  86. Configure::write($config);
  87. }
  88. /**
  89. * Locate a plugin path by looking at configuration data.
  90. *
  91. * This will use the `plugins` Configure key, and fallback to enumerating `App::path('Plugin')`
  92. *
  93. * This method is not part of the official public API as plugins with
  94. * no plugin class are being phased out.
  95. *
  96. * @param string $name The plugin name to locate a path for. Will return '' when a plugin cannot be found.
  97. * @return string
  98. * @throws \Cake\Core\Exception\MissingPluginException when a plugin path cannot be resolved.
  99. * @internal
  100. */
  101. public function findPath($name)
  102. {
  103. $this->loadConfig();
  104. $path = Configure::read('plugins.' . $name);
  105. if ($path) {
  106. return $path;
  107. }
  108. $pluginPath = str_replace('/', DIRECTORY_SEPARATOR, $name);
  109. $paths = App::path('Plugin');
  110. foreach ($paths as $path) {
  111. if (is_dir($path . $pluginPath)) {
  112. return $path . $pluginPath . DIRECTORY_SEPARATOR;
  113. }
  114. }
  115. throw new MissingPluginException(['plugin' => $name]);
  116. }
  117. /**
  118. * Add a plugin to the collection
  119. *
  120. * Plugins will be keyed by their names.
  121. *
  122. * @param \Cake\Core\PluginInterface $plugin The plugin to load.
  123. * @return $this
  124. */
  125. public function add(PluginInterface $plugin)
  126. {
  127. $name = $plugin->getName();
  128. $this->plugins[$name] = $plugin;
  129. $this->names = array_keys($this->plugins);
  130. return $this;
  131. }
  132. /**
  133. * Remove a plugin from the collection if it exists.
  134. *
  135. * @param string $name The named plugin.
  136. * @return $this
  137. */
  138. public function remove($name)
  139. {
  140. unset($this->plugins[$name]);
  141. $this->names = array_keys($this->plugins);
  142. return $this;
  143. }
  144. /**
  145. * Check whether the named plugin exists in the collection.
  146. *
  147. * @param string $name The named plugin.
  148. * @return bool
  149. */
  150. public function has($name)
  151. {
  152. return isset($this->plugins[$name]);
  153. }
  154. /**
  155. * Get the a plugin by name
  156. *
  157. * @param string $name The plugin to get.
  158. * @return \Cake\Core\PluginInterface The plugin.
  159. * @throws \Cake\Core\Exception\MissingPluginException when unknown plugins are fetched.
  160. */
  161. public function get($name)
  162. {
  163. if (!$this->has($name)) {
  164. throw new MissingPluginException(['plugin' => $name]);
  165. }
  166. return $this->plugins[$name];
  167. }
  168. /**
  169. * Part of Iterator Interface
  170. *
  171. * @return void
  172. */
  173. public function next()
  174. {
  175. $this->position++;
  176. }
  177. /**
  178. * Part of Iterator Interface
  179. *
  180. * @return string
  181. */
  182. public function key()
  183. {
  184. return $this->names[$this->position];
  185. }
  186. /**
  187. * Part of Iterator Interface
  188. *
  189. * @return \Cake\Core\PluginInterface
  190. */
  191. public function current()
  192. {
  193. $name = $this->names[$this->position];
  194. return $this->plugins[$name];
  195. }
  196. /**
  197. * Part of Iterator Interface
  198. *
  199. * @return void
  200. */
  201. public function rewind()
  202. {
  203. $this->position = 0;
  204. }
  205. /**
  206. * Part of Iterator Interface
  207. *
  208. * @return bool
  209. */
  210. public function valid()
  211. {
  212. return $this->position < count($this->plugins);
  213. }
  214. /**
  215. * Implementation of Countable.
  216. *
  217. * Get the number of plugins in the collection.
  218. *
  219. * @return int
  220. */
  221. public function count()
  222. {
  223. return count($this->plugins);
  224. }
  225. /**
  226. * Filter the plugins to those with the named hook enabled.
  227. *
  228. * @param string $hook The hook to filter plugins by
  229. * @return \Generator A generator containing matching plugins.
  230. * @throws \InvalidArgumentException on invalid hooks
  231. */
  232. public function with($hook)
  233. {
  234. if (!in_array($hook, PluginInterface::VALID_HOOKS)) {
  235. throw new InvalidArgumentException("The `{$hook}` hook is not a known plugin hook.");
  236. }
  237. foreach ($this as $plugin) {
  238. if ($plugin->isEnabled($hook)) {
  239. yield $plugin;
  240. }
  241. }
  242. }
  243. }