Plugin.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.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 http://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 array
  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. */
  107. public static function load($plugin, array $config = [])
  108. {
  109. if (is_array($plugin)) {
  110. foreach ($plugin as $name => $conf) {
  111. list($name, $conf) = is_numeric($name) ? [$conf, $config] : [$name, $conf];
  112. static::load($name, $conf);
  113. }
  114. return;
  115. }
  116. static::_loadConfig();
  117. $config += [
  118. 'autoload' => false,
  119. 'bootstrap' => false,
  120. 'routes' => false,
  121. 'classBase' => 'src',
  122. 'ignoreMissing' => false
  123. ];
  124. if (!isset($config['path'])) {
  125. $config['path'] = Configure::read('plugins.' . $plugin);
  126. }
  127. if (empty($config['path'])) {
  128. $paths = App::path('Plugin');
  129. $pluginPath = str_replace('/', DIRECTORY_SEPARATOR, $plugin);
  130. foreach ($paths as $path) {
  131. if (is_dir($path . $pluginPath)) {
  132. $config['path'] = $path . $pluginPath . DIRECTORY_SEPARATOR;
  133. break;
  134. }
  135. }
  136. }
  137. if (empty($config['path'])) {
  138. throw new MissingPluginException(['plugin' => $plugin]);
  139. }
  140. $config['classPath'] = $config['path'] . $config['classBase'] . DIRECTORY_SEPARATOR;
  141. if (!isset($config['configPath'])) {
  142. $config['configPath'] = $config['path'] . 'config' . DIRECTORY_SEPARATOR;
  143. }
  144. static::$_plugins[$plugin] = $config;
  145. if ($config['autoload'] === true) {
  146. if (empty(static::$_loader)) {
  147. static::$_loader = new ClassLoader();
  148. static::$_loader->register();
  149. }
  150. static::$_loader->addNamespace(
  151. str_replace('/', '\\', $plugin),
  152. $config['path'] . $config['classBase'] . DIRECTORY_SEPARATOR
  153. );
  154. static::$_loader->addNamespace(
  155. str_replace('/', '\\', $plugin) . '\Test',
  156. $config['path'] . 'tests' . DIRECTORY_SEPARATOR
  157. );
  158. }
  159. if ($config['bootstrap'] === true) {
  160. static::bootstrap($plugin);
  161. }
  162. }
  163. /**
  164. * Load the plugin path configuration file.
  165. *
  166. * @return void
  167. */
  168. protected static function _loadConfig()
  169. {
  170. if (Configure::check('plugins')) {
  171. return;
  172. }
  173. $vendorFile = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'cakephp-plugins.php';
  174. if (!file_exists($vendorFile)) {
  175. $vendorFile = dirname(dirname(dirname(dirname(__DIR__)))) . DIRECTORY_SEPARATOR . 'cakephp-plugins.php';
  176. if (!file_exists($vendorFile)) {
  177. Configure::write(['plugins' => []]);
  178. return;
  179. }
  180. }
  181. $config = require $vendorFile;
  182. Configure::write($config);
  183. }
  184. /**
  185. * Will load all the plugins located in the default plugin folder.
  186. *
  187. * If passed an options array, it will be used as a common default for all plugins to be loaded
  188. * It is possible to set specific defaults for each plugins in the options array. Examples:
  189. *
  190. * ```
  191. * Plugin::loadAll([
  192. * ['bootstrap' => true],
  193. * 'DebugKit' => ['routes' => true],
  194. * ]);
  195. * ```
  196. *
  197. * The above example will load the bootstrap file for all plugins, but for DebugKit it will only load the routes file
  198. * and will not look for any bootstrap script.
  199. *
  200. * If a plugin has been loaded already, it will not be reloaded by loadAll().
  201. *
  202. * @param array $options Options.
  203. * @return void
  204. * @throws \Cake\Core\Exception\MissingPluginException
  205. */
  206. public static function loadAll(array $options = [])
  207. {
  208. static::_loadConfig();
  209. $plugins = [];
  210. foreach (App::path('Plugin') as $path) {
  211. if (!is_dir($path)) {
  212. continue;
  213. }
  214. $dir = new DirectoryIterator($path);
  215. foreach ($dir as $dirPath) {
  216. if ($dirPath->isDir() && !$dirPath->isDot()) {
  217. $plugins[] = $dirPath->getBasename();
  218. }
  219. }
  220. }
  221. if (Configure::check('plugins')) {
  222. $plugins = array_merge($plugins, array_keys(Configure::read('plugins')));
  223. $plugins = array_unique($plugins);
  224. }
  225. foreach ($plugins as $p) {
  226. $opts = isset($options[$p]) ? $options[$p] : null;
  227. if ($opts === null && isset($options[0])) {
  228. $opts = $options[0];
  229. }
  230. if (isset(static::$_plugins[$p])) {
  231. continue;
  232. }
  233. static::load($p, (array)$opts);
  234. }
  235. }
  236. /**
  237. * Returns the filesystem path for a plugin
  238. *
  239. * @param string $plugin name of the plugin in CamelCase format
  240. * @return string path to the plugin folder
  241. * @throws \Cake\Core\Exception\MissingPluginException if the folder for plugin was not found or plugin has not been loaded
  242. */
  243. public static function path($plugin)
  244. {
  245. if (empty(static::$_plugins[$plugin])) {
  246. throw new MissingPluginException(['plugin' => $plugin]);
  247. }
  248. return static::$_plugins[$plugin]['path'];
  249. }
  250. /**
  251. * Returns the filesystem path for plugin's folder containing class folders.
  252. *
  253. * @param string $plugin name of the plugin in CamelCase format.
  254. * @return string Path to the plugin folder container class folders.
  255. * @throws \Cake\Core\Exception\MissingPluginException If plugin has not been loaded.
  256. */
  257. public static function classPath($plugin)
  258. {
  259. if (empty(static::$_plugins[$plugin])) {
  260. throw new MissingPluginException(['plugin' => $plugin]);
  261. }
  262. return static::$_plugins[$plugin]['classPath'];
  263. }
  264. /**
  265. * Returns the filesystem path for plugin's folder containing config files.
  266. *
  267. * @param string $plugin name of the plugin in CamelCase format.
  268. * @return string Path to the plugin folder container config files.
  269. * @throws \Cake\Core\Exception\MissingPluginException If plugin has not been loaded.
  270. */
  271. public static function configPath($plugin)
  272. {
  273. if (empty(static::$_plugins[$plugin])) {
  274. throw new MissingPluginException(['plugin' => $plugin]);
  275. }
  276. return static::$_plugins[$plugin]['configPath'];
  277. }
  278. /**
  279. * Loads the bootstrapping files for a plugin, or calls the initialization setup in the configuration
  280. *
  281. * @param string $plugin name of the plugin
  282. * @return mixed
  283. * @see \Cake\Core\Plugin::load() for examples of bootstrap configuration
  284. */
  285. public static function bootstrap($plugin)
  286. {
  287. $config = static::$_plugins[$plugin];
  288. if ($config['bootstrap'] === false) {
  289. return false;
  290. }
  291. if ($config['bootstrap'] === true) {
  292. return static::_includeFile(
  293. $config['configPath'] . 'bootstrap.php',
  294. $config['ignoreMissing']
  295. );
  296. }
  297. }
  298. /**
  299. * Loads the routes file for a plugin, or all plugins configured to load their respective routes file.
  300. *
  301. * If you need fine grained control over how routes are loaded for plugins, you
  302. * can use {@see Cake\Routing\RouteBuilder::loadPlugin()}
  303. *
  304. * @param string|null $plugin name of the plugin, if null will operate on all
  305. * plugins having enabled the loading of routes files.
  306. * @return bool
  307. */
  308. public static function routes($plugin = null)
  309. {
  310. if ($plugin === null) {
  311. foreach (static::loaded() as $p) {
  312. static::routes($p);
  313. }
  314. return true;
  315. }
  316. $config = static::$_plugins[$plugin];
  317. if ($config['routes'] === false) {
  318. return false;
  319. }
  320. return (bool)static::_includeFile(
  321. $config['configPath'] . 'routes.php',
  322. $config['ignoreMissing']
  323. );
  324. }
  325. /**
  326. * Returns true if the plugin $plugin is already loaded
  327. * If plugin is null, it will return a list of all loaded plugins
  328. *
  329. * @param string|null $plugin Plugin name.
  330. * @return bool|array Boolean true if $plugin is already loaded.
  331. * If $plugin is null, returns a list of plugins that have been loaded
  332. */
  333. public static function loaded($plugin = null)
  334. {
  335. if ($plugin !== null) {
  336. return isset(static::$_plugins[$plugin]);
  337. }
  338. $return = array_keys(static::$_plugins);
  339. sort($return);
  340. return $return;
  341. }
  342. /**
  343. * Forgets a loaded plugin or all of them if first parameter is null
  344. *
  345. * @param string|null $plugin name of the plugin to forget
  346. * @return void
  347. */
  348. public static function unload($plugin = null)
  349. {
  350. if ($plugin === null) {
  351. static::$_plugins = [];
  352. } else {
  353. unset(static::$_plugins[$plugin]);
  354. }
  355. }
  356. /**
  357. * Include file, ignoring include error if needed if file is missing
  358. *
  359. * @param string $file File to include
  360. * @param bool $ignoreMissing Whether to ignore include error for missing files
  361. * @return mixed
  362. */
  363. protected static function _includeFile($file, $ignoreMissing = false)
  364. {
  365. if ($ignoreMissing && !is_file($file)) {
  366. return false;
  367. }
  368. return include $file;
  369. }
  370. }