Plugin.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Core;
  16. use Cake\Core\Exception\MissingPluginException;
  17. use DirectoryIterator;
  18. /**
  19. * Plugin is used to load and locate plugins.
  20. *
  21. * It also can retrieve plugin paths and load their bootstrap and routes files.
  22. *
  23. * @link https://book.cakephp.org/3.0/en/plugins.html
  24. */
  25. class Plugin
  26. {
  27. /**
  28. * Holds a list of all loaded plugins and their configuration
  29. *
  30. * @var \Cake\Core\PluginCollection|null
  31. */
  32. protected static $plugins;
  33. /**
  34. * Class loader instance
  35. *
  36. * @var \Cake\Core\ClassLoader
  37. */
  38. protected static $_loader;
  39. /**
  40. * Loads a plugin and optionally loads bootstrapping,
  41. * routing files or runs an initialization function.
  42. *
  43. * Plugins only need to be loaded if you want bootstrapping/routes/cli commands to
  44. * be exposed. If your plugin does not expose any of these features you do not need
  45. * to load them.
  46. *
  47. * This method does not configure any autoloaders. That must be done separately either
  48. * through composer, or your own code during config/bootstrap.php.
  49. *
  50. * ### Examples:
  51. *
  52. * `Plugin::load('DebugKit')`
  53. *
  54. * Will load the DebugKit plugin and will not load any bootstrap nor route files.
  55. * However, the plugin will be part of the framework default routes, and have its
  56. * CLI tools (if any) available for use.
  57. *
  58. * `Plugin::load('DebugKit', ['bootstrap' => true, 'routes' => true])`
  59. *
  60. * Will load the bootstrap.php and routes.php files.
  61. *
  62. * `Plugin::load('DebugKit', ['bootstrap' => false, 'routes' => true])`
  63. *
  64. * Will load routes.php file but not bootstrap.php
  65. *
  66. * `Plugin::load('FOC/Authenticate')`
  67. *
  68. * Will load plugin from `plugins/FOC/Authenticate`.
  69. *
  70. * It is also possible to load multiple plugins at once. Examples:
  71. *
  72. * `Plugin::load(['DebugKit', 'ApiGenerator'])`
  73. *
  74. * Will load the DebugKit and ApiGenerator plugins.
  75. *
  76. * `Plugin::load(['DebugKit', 'ApiGenerator'], ['bootstrap' => true])`
  77. *
  78. * Will load bootstrap file for both plugins
  79. *
  80. * ```
  81. * Plugin::load([
  82. * 'DebugKit' => ['routes' => true],
  83. * 'ApiGenerator'
  84. * ],
  85. * ['bootstrap' => true])
  86. * ```
  87. *
  88. * Will only load the bootstrap for ApiGenerator and only the routes for DebugKit
  89. *
  90. * ### Configuration options
  91. *
  92. * - `bootstrap` - array - Whether or not you want the $plugin/config/bootstrap.php file loaded.
  93. * - `routes` - boolean - Whether or not you want to load the $plugin/config/routes.php file.
  94. * - `ignoreMissing` - boolean - Set to true to ignore missing bootstrap/routes files.
  95. * - `path` - string - The path the plugin can be found on. If empty the default plugin path (App.pluginPaths) will be used.
  96. * - `classBase` - The path relative to `path` which contains the folders with class files.
  97. * Defaults to "src".
  98. * - `autoload` - boolean - Whether or not you want an autoloader registered. This defaults to false. The framework
  99. * assumes you have configured autoloaders using composer. However, if your application source tree is made up of
  100. * plugins, this can be a useful option.
  101. *
  102. * @param string|array $plugin name of the plugin to be loaded in CamelCase format or array or plugins to load
  103. * @param array $config configuration options for the plugin
  104. * @throws \Cake\Core\Exception\MissingPluginException if the folder for the plugin to be loaded is not found
  105. * @return void
  106. * @deprecated 3.7.0 This method will be removed in 4.0.0. Use Application::addPlugin() instead.
  107. */
  108. public static function load($plugin, array $config = [])
  109. {
  110. deprecationWarning(
  111. 'Plugin::load() is deprecated. ' .
  112. 'Use Application::addPlugin() instead. ' .
  113. 'This method will be removed in 4.0.0.'
  114. );
  115. if (is_array($plugin)) {
  116. foreach ($plugin as $name => $conf) {
  117. list($name, $conf) = is_numeric($name) ? [$conf, $config] : [$name, $conf];
  118. static::load($name, $conf);
  119. }
  120. return;
  121. }
  122. $config += [
  123. 'autoload' => false,
  124. 'bootstrap' => false,
  125. 'routes' => false,
  126. 'console' => true,
  127. 'classBase' => 'src',
  128. 'ignoreMissing' => false,
  129. 'name' => $plugin
  130. ];
  131. if (!isset($config['path'])) {
  132. $config['path'] = static::getCollection()->findPath($plugin);
  133. }
  134. $config['classPath'] = $config['path'] . $config['classBase'] . DIRECTORY_SEPARATOR;
  135. if (!isset($config['configPath'])) {
  136. $config['configPath'] = $config['path'] . 'config' . DIRECTORY_SEPARATOR;
  137. }
  138. $pluginClass = str_replace('/', '\\', $plugin) . '\\Plugin';
  139. if (class_exists($pluginClass)) {
  140. $instance = new $pluginClass($config);
  141. } else {
  142. // Use stub plugin as this method will be removed long term.
  143. $instance = new BasePlugin($config);
  144. }
  145. static::getCollection()->add($instance);
  146. if ($config['autoload'] === true) {
  147. if (empty(static::$_loader)) {
  148. static::$_loader = new ClassLoader();
  149. static::$_loader->register();
  150. }
  151. static::$_loader->addNamespace(
  152. str_replace('/', '\\', $plugin),
  153. $config['path'] . $config['classBase'] . DIRECTORY_SEPARATOR
  154. );
  155. static::$_loader->addNamespace(
  156. str_replace('/', '\\', $plugin) . '\Test',
  157. $config['path'] . 'tests' . DIRECTORY_SEPARATOR
  158. );
  159. }
  160. if ($config['bootstrap'] === true) {
  161. static::bootstrap($plugin);
  162. }
  163. }
  164. /**
  165. * Will load all the plugins located in the default plugin folder.
  166. *
  167. * If passed an options array, it will be used as a common default for all plugins to be loaded
  168. * It is possible to set specific defaults for each plugins in the options array. Examples:
  169. *
  170. * ```
  171. * Plugin::loadAll([
  172. * ['bootstrap' => true],
  173. * 'DebugKit' => ['routes' => true],
  174. * ]);
  175. * ```
  176. *
  177. * The above example will load the bootstrap file for all plugins, but for DebugKit it will only load the routes file
  178. * and will not look for any bootstrap script.
  179. *
  180. * If a plugin has been loaded already, it will not be reloaded by loadAll().
  181. *
  182. * @param array $options Options.
  183. * @return void
  184. * @throws \Cake\Core\Exception\MissingPluginException
  185. * @deprecated 3.7.0 This method will be removed in 4.0.0.
  186. */
  187. public static function loadAll(array $options = [])
  188. {
  189. $plugins = [];
  190. foreach (App::path('Plugin') as $path) {
  191. if (!is_dir($path)) {
  192. continue;
  193. }
  194. $dir = new DirectoryIterator($path);
  195. foreach ($dir as $dirPath) {
  196. if ($dirPath->isDir() && !$dirPath->isDot()) {
  197. $plugins[] = $dirPath->getBasename();
  198. }
  199. }
  200. }
  201. if (Configure::check('plugins')) {
  202. $plugins = array_merge($plugins, array_keys(Configure::read('plugins')));
  203. $plugins = array_unique($plugins);
  204. }
  205. $collection = static::getCollection();
  206. foreach ($plugins as $p) {
  207. $opts = isset($options[$p]) ? $options[$p] : null;
  208. if ($opts === null && isset($options[0])) {
  209. $opts = $options[0];
  210. }
  211. if ($collection->has($p)) {
  212. continue;
  213. }
  214. static::load($p, (array)$opts);
  215. }
  216. }
  217. /**
  218. * Returns the filesystem path for a plugin
  219. *
  220. * @param string $name name of the plugin in CamelCase format
  221. * @return string path to the plugin folder
  222. * @throws \Cake\Core\Exception\MissingPluginException if the folder for plugin was not found or plugin has not been loaded
  223. */
  224. public static function path($name)
  225. {
  226. $plugin = static::getCollection()->get($name);
  227. return $plugin->getPath();
  228. }
  229. /**
  230. * Returns the filesystem path for plugin's folder containing class folders.
  231. *
  232. * @param string $name name of the plugin in CamelCase format.
  233. * @return string Path to the plugin folder container class folders.
  234. * @throws \Cake\Core\Exception\MissingPluginException If plugin has not been loaded.
  235. */
  236. public static function classPath($name)
  237. {
  238. $plugin = static::getCollection()->get($name);
  239. return $plugin->getClassPath();
  240. }
  241. /**
  242. * Returns the filesystem path for plugin's folder containing config files.
  243. *
  244. * @param string $name name of the plugin in CamelCase format.
  245. * @return string Path to the plugin folder container config files.
  246. * @throws \Cake\Core\Exception\MissingPluginException If plugin has not been loaded.
  247. */
  248. public static function configPath($name)
  249. {
  250. $plugin = static::getCollection()->get($name);
  251. return $plugin->getConfigPath();
  252. }
  253. /**
  254. * Loads the bootstrapping files for a plugin, or calls the initialization setup in the configuration
  255. *
  256. * @param string $name name of the plugin
  257. * @return mixed
  258. * @see \Cake\Core\Plugin::load() for examples of bootstrap configuration
  259. * @deprecated 3.7.0 This method will be removed in 4.0.0.
  260. */
  261. public static function bootstrap($name)
  262. {
  263. deprecationWarning(
  264. 'Plugin::bootstrap() is deprecated. ' .
  265. 'This method will be removed in 4.0.0.'
  266. );
  267. $plugin = static::getCollection()->get($name);
  268. if (!$plugin->isEnabled('bootstrap')) {
  269. return false;
  270. }
  271. // Disable bootstrapping for this plugin as it will have
  272. // been bootstrapped.
  273. $plugin->disable('bootstrap');
  274. return static::_includeFile(
  275. $plugin->getConfigPath() . 'bootstrap.php',
  276. true
  277. );
  278. }
  279. /**
  280. * Loads the routes file for a plugin, or all plugins configured to load their respective routes file.
  281. *
  282. * If you need fine grained control over how routes are loaded for plugins, you
  283. * can use {@see Cake\Routing\RouteBuilder::loadPlugin()}
  284. *
  285. * @param string|null $name name of the plugin, if null will operate on all
  286. * plugins having enabled the loading of routes files.
  287. * @return bool
  288. * @deprecated 3.6.5 This method is no longer needed when using HttpApplicationInterface based applications.
  289. * This method will be removed in 4.0.0
  290. */
  291. public static function routes($name = null)
  292. {
  293. deprecationWarning(
  294. 'You no longer need to call `Plugin::routes()` after upgrading to use Http\Server. ' .
  295. 'See https://book.cakephp.org/3.0/en/development/application.html#adding-the-new-http-stack-to-an-existing-application ' .
  296. 'for upgrade information.'
  297. );
  298. if ($name === null) {
  299. foreach (static::loaded() as $p) {
  300. static::routes($p);
  301. }
  302. return true;
  303. }
  304. $plugin = static::getCollection()->get($name);
  305. if (!$plugin->isEnabled('routes')) {
  306. return false;
  307. }
  308. return (bool)static::_includeFile(
  309. $plugin->getConfigPath() . 'routes.php',
  310. true
  311. );
  312. }
  313. /**
  314. * Check whether or not a plugin is loaded.
  315. *
  316. * @param string $plugin The name of the plugin to check.
  317. * @return bool
  318. * @since 3.7.0
  319. */
  320. public static function isLoaded($plugin)
  321. {
  322. return static::getCollection()->has($plugin);
  323. }
  324. /**
  325. * Return a list of loaded plugins.
  326. *
  327. * If a plugin name is provided, the return value will be a bool
  328. * indicating whether or not the named plugin is loaded. This usage
  329. * is deprecated. Instead you should use Plugin::isLoaded($name)
  330. *
  331. * @param string|null $plugin Plugin name.
  332. * @return bool|array Boolean true if $plugin is already loaded.
  333. * If $plugin is null, returns a list of plugins that have been loaded
  334. */
  335. public static function loaded($plugin = null)
  336. {
  337. if ($plugin !== null) {
  338. deprecationWarning(
  339. 'Checking a single plugin with Plugin::loaded() is deprecated. ' .
  340. 'Use Plugin::isLoaded() instead.'
  341. );
  342. return static::getCollection()->has($plugin);
  343. }
  344. $names = [];
  345. foreach (static::getCollection() as $plugin) {
  346. $names[] = $plugin->getName();
  347. }
  348. sort($names);
  349. return $names;
  350. }
  351. /**
  352. * Forgets a loaded plugin or all of them if first parameter is null
  353. *
  354. * @param string|null $plugin name of the plugin to forget
  355. * @deprecated 3.7.0 This method will be removed in 4.0.0. Use PluginCollection::remove() or clear() instead.
  356. * @return void
  357. */
  358. public static function unload($plugin = null)
  359. {
  360. deprecationWarning('Plugin::unload() will be removed in 4.0. Use PluginCollection::remove() or clear()');
  361. if ($plugin === null) {
  362. static::getCollection()->clear();
  363. } else {
  364. static::getCollection()->remove($plugin);
  365. }
  366. }
  367. /**
  368. * Include file, ignoring include error if needed if file is missing
  369. *
  370. * @param string $file File to include
  371. * @param bool $ignoreMissing Whether to ignore include error for missing files
  372. * @return mixed
  373. */
  374. protected static function _includeFile($file, $ignoreMissing = false)
  375. {
  376. if ($ignoreMissing && !is_file($file)) {
  377. return false;
  378. }
  379. return include $file;
  380. }
  381. /**
  382. * Get the shared plugin collection.
  383. *
  384. * This method should generally not be used during application
  385. * runtime as plugins should be set during Application startup.
  386. *
  387. * @return \Cake\Core\PluginCollection
  388. */
  389. public static function getCollection()
  390. {
  391. if (!isset(static::$plugins)) {
  392. static::$plugins = new PluginCollection();
  393. }
  394. return static::$plugins;
  395. }
  396. }