PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/cakephp/cakephp/src/Core/Plugin.php

https://gitlab.com/alexandresgv/siteentec
PHP | 390 lines | 183 code | 27 blank | 180 comment | 30 complexity | e8ad4991b5cf0aae513a91c1465d89fe MD5 | raw file
  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('/', DS, $plugin);
  130. foreach ($paths as $path) {
  131. if (is_dir($path . $pluginPath)) {
  132. $config['path'] = $path . $pluginPath . DS;
  133. break;
  134. }
  135. }
  136. }
  137. if (empty($config['path'])) {
  138. throw new MissingPluginException(['plugin' => $plugin]);
  139. }
  140. $config['classPath'] = $config['path'] . $config['classBase'] . DS;
  141. if (!isset($config['configPath'])) {
  142. $config['configPath'] = $config['path'] . 'config' . DS;
  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'] . DS
  153. );
  154. static::$_loader->addNamespace(
  155. str_replace('/', '\\', $plugin) . '\Test',
  156. $config['path'] . 'tests' . DS
  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(dirname(dirname(__DIR__)))) . DS . 'cakephp-plugins.php';
  174. if (!file_exists($vendorFile)) {
  175. Configure::write(['plugins' => []]);
  176. return;
  177. }
  178. $config = require $vendorFile;
  179. Configure::write($config);
  180. }
  181. /**
  182. * Will load all the plugins located in the default plugin folder.
  183. *
  184. * If passed an options array, it will be used as a common default for all plugins to be loaded
  185. * It is possible to set specific defaults for each plugins in the options array. Examples:
  186. *
  187. * ```
  188. * Plugin::loadAll([
  189. * ['bootstrap' => true],
  190. * 'DebugKit' => ['routes' => true],
  191. * ]);
  192. * ```
  193. *
  194. * The above example will load the bootstrap file for all plugins, but for DebugKit it will only load the routes file
  195. * and will not look for any bootstrap script.
  196. *
  197. * If a plugin has been loaded already, it will not be reloaded by loadAll().
  198. *
  199. * @param array $options Options.
  200. * @return void
  201. */
  202. public static function loadAll(array $options = [])
  203. {
  204. static::_loadConfig();
  205. $plugins = [];
  206. foreach (App::path('Plugin') as $path) {
  207. if (!is_dir($path)) {
  208. continue;
  209. }
  210. $dir = new DirectoryIterator($path);
  211. foreach ($dir as $path) {
  212. if ($path->isDir() && !$path->isDot()) {
  213. $plugins[] = $path->getBasename();
  214. }
  215. }
  216. }
  217. if (Configure::check('plugins')) {
  218. $plugins = array_merge($plugins, array_keys(Configure::read('plugins')));
  219. $plugins = array_unique($plugins);
  220. }
  221. foreach ($plugins as $p) {
  222. $opts = isset($options[$p]) ? $options[$p] : null;
  223. if ($opts === null && isset($options[0])) {
  224. $opts = $options[0];
  225. }
  226. if (isset(static::$_plugins[$p])) {
  227. continue;
  228. }
  229. static::load($p, (array)$opts);
  230. }
  231. }
  232. /**
  233. * Returns the filesystem path for a plugin
  234. *
  235. * @param string $plugin name of the plugin in CamelCase format
  236. * @return string path to the plugin folder
  237. * @throws \Cake\Core\Exception\MissingPluginException if the folder for plugin was not found or plugin has not been loaded
  238. */
  239. public static function path($plugin)
  240. {
  241. if (empty(static::$_plugins[$plugin])) {
  242. throw new MissingPluginException(['plugin' => $plugin]);
  243. }
  244. return static::$_plugins[$plugin]['path'];
  245. }
  246. /**
  247. * Returns the filesystem path for plugin's folder containing class folders.
  248. *
  249. * @param string $plugin name of the plugin in CamelCase format.
  250. * @return string Path to the plugin folder container class folders.
  251. * @throws \Cake\Core\Exception\MissingPluginException If plugin has not been loaded.
  252. */
  253. public static function classPath($plugin)
  254. {
  255. if (empty(static::$_plugins[$plugin])) {
  256. throw new MissingPluginException(['plugin' => $plugin]);
  257. }
  258. return static::$_plugins[$plugin]['classPath'];
  259. }
  260. /**
  261. * Returns the filesystem path for plugin's folder containing config files.
  262. *
  263. * @param string $plugin name of the plugin in CamelCase format.
  264. * @return string Path to the plugin folder container config files.
  265. * @throws \Cake\Core\Exception\MissingPluginException If plugin has not been loaded.
  266. */
  267. public static function configPath($plugin)
  268. {
  269. if (empty(static::$_plugins[$plugin])) {
  270. throw new MissingPluginException(['plugin' => $plugin]);
  271. }
  272. return static::$_plugins[$plugin]['configPath'];
  273. }
  274. /**
  275. * Loads the bootstrapping files for a plugin, or calls the initialization setup in the configuration
  276. *
  277. * @param string $plugin name of the plugin
  278. * @return mixed
  279. * @see Plugin::load() for examples of bootstrap configuration
  280. */
  281. public static function bootstrap($plugin)
  282. {
  283. $config = static::$_plugins[$plugin];
  284. if ($config['bootstrap'] === false) {
  285. return false;
  286. }
  287. if ($config['bootstrap'] === true) {
  288. return static::_includeFile(
  289. $config['configPath'] . 'bootstrap.php',
  290. $config['ignoreMissing']
  291. );
  292. }
  293. }
  294. /**
  295. * Loads the routes file for a plugin, or all plugins configured to load their respective routes file
  296. *
  297. * @param string $plugin name of the plugin, if null will operate on all plugins having enabled the
  298. * loading of routes files
  299. * @return bool
  300. */
  301. public static function routes($plugin = null)
  302. {
  303. if ($plugin === null) {
  304. foreach (static::loaded() as $p) {
  305. static::routes($p);
  306. }
  307. return true;
  308. }
  309. $config = static::$_plugins[$plugin];
  310. if ($config['routes'] === false) {
  311. return false;
  312. }
  313. return (bool)static::_includeFile(
  314. $config['configPath'] . 'routes.php',
  315. $config['ignoreMissing']
  316. );
  317. }
  318. /**
  319. * Returns true if the plugin $plugin is already loaded
  320. * If plugin is null, it will return a list of all loaded plugins
  321. *
  322. * @param string $plugin Plugin name.
  323. * @return mixed boolean true if $plugin is already loaded.
  324. * If $plugin is null, returns a list of plugins that have been loaded
  325. */
  326. public static function loaded($plugin = null)
  327. {
  328. if ($plugin !== null) {
  329. return isset(static::$_plugins[$plugin]);
  330. }
  331. $return = array_keys(static::$_plugins);
  332. sort($return);
  333. return $return;
  334. }
  335. /**
  336. * Forgets a loaded plugin or all of them if first parameter is null
  337. *
  338. * @param string $plugin name of the plugin to forget
  339. * @return void
  340. */
  341. public static function unload($plugin = null)
  342. {
  343. if ($plugin === null) {
  344. static::$_plugins = [];
  345. } else {
  346. unset(static::$_plugins[$plugin]);
  347. }
  348. }
  349. /**
  350. * Include file, ignoring include error if needed if file is missing
  351. *
  352. * @param string $file File to include
  353. * @param bool $ignoreMissing Whether to ignore include error for missing files
  354. * @return mixed
  355. */
  356. protected static function _includeFile($file, $ignoreMissing = false)
  357. {
  358. if ($ignoreMissing && !is_file($file)) {
  359. return false;
  360. }
  361. return include $file;
  362. }
  363. }