PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/vannh/portal_training
PHP | 392 lines | 185 code | 27 blank | 180 comment | 30 complexity | 5db875c502d38397789c89f86e1e4964 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\ClassLoader;
  17. use Cake\Core\Configure;
  18. use Cake\Core\Exception\MissingPluginException;
  19. use DirectoryIterator;
  20. /**
  21. * Plugin is used to load and locate plugins.
  22. *
  23. * It also can retrieve plugin paths and load their bootstrap and routes files.
  24. *
  25. * @link http://book.cakephp.org/3.0/en/plugins.html
  26. */
  27. class Plugin
  28. {
  29. /**
  30. * Holds a list of all loaded plugins and their configuration
  31. *
  32. * @var array
  33. */
  34. protected static $_plugins = [];
  35. /**
  36. * Class loader instance
  37. *
  38. * @var \Cake\Core\ClassLoader
  39. */
  40. protected static $_loader;
  41. /**
  42. * Loads a plugin and optionally loads bootstrapping,
  43. * routing files or runs an initialization function.
  44. *
  45. * Plugins only need to be loaded if you want bootstrapping/routes/cli commands to
  46. * be exposed. If your plugin does not expose any of these features you do not need
  47. * to load them.
  48. *
  49. * This method does not configure any autoloaders. That must be done separately either
  50. * through composer, or your own code during config/bootstrap.php.
  51. *
  52. * ### Examples:
  53. *
  54. * `Plugin::load('DebugKit')`
  55. *
  56. * Will load the DebugKit plugin and will not load any bootstrap nor route files.
  57. * However, the plugin will be part of the framework default routes, and have its
  58. * CLI tools (if any) available for use.
  59. *
  60. * `Plugin::load('DebugKit', ['bootstrap' => true, 'routes' => true])`
  61. *
  62. * Will load the bootstrap.php and routes.php files.
  63. *
  64. * `Plugin::load('DebugKit', ['bootstrap' => false, 'routes' => true])`
  65. *
  66. * Will load routes.php file but not bootstrap.php
  67. *
  68. * `Plugin::load('FOC/Authenticate')`
  69. *
  70. * Will load plugin from `plugins/FOC/Authenticate`.
  71. *
  72. * It is also possible to load multiple plugins at once. Examples:
  73. *
  74. * `Plugin::load(['DebugKit', 'ApiGenerator'])`
  75. *
  76. * Will load the DebugKit and ApiGenerator plugins.
  77. *
  78. * `Plugin::load(['DebugKit', 'ApiGenerator'], ['bootstrap' => true])`
  79. *
  80. * Will load bootstrap file for both plugins
  81. *
  82. * ```
  83. * Plugin::load([
  84. * 'DebugKit' => ['routes' => true],
  85. * 'ApiGenerator'
  86. * ],
  87. * ['bootstrap' => true])
  88. * ```
  89. *
  90. * Will only load the bootstrap for ApiGenerator and only the routes for DebugKit
  91. *
  92. * ### Configuration options
  93. *
  94. * - `bootstrap` - array - Whether or not you want the $plugin/config/bootstrap.php file loaded.
  95. * - `routes` - boolean - Whether or not you want to load the $plugin/config/routes.php file.
  96. * - `ignoreMissing` - boolean - Set to true to ignore missing bootstrap/routes files.
  97. * - `path` - string - The path the plugin can be found on. If empty the default plugin path (App.pluginPaths) will be used.
  98. * - `classBase` - The path relative to `path` which contains the folders with class files.
  99. * Defaults to "src".
  100. * - `autoload` - boolean - Whether or not you want an autoloader registered. This defaults to false. The framework
  101. * assumes you have configured autoloaders using composer. However, if your application source tree is made up of
  102. * plugins, this can be a useful option.
  103. *
  104. * @param string|array $plugin name of the plugin to be loaded in CamelCase format or array or plugins to load
  105. * @param array $config configuration options for the plugin
  106. * @throws \Cake\Core\Exception\MissingPluginException if the folder for the plugin to be loaded is not found
  107. * @return void
  108. */
  109. public static function load($plugin, array $config = [])
  110. {
  111. if (is_array($plugin)) {
  112. foreach ($plugin as $name => $conf) {
  113. list($name, $conf) = (is_numeric($name)) ? [$conf, $config] : [$name, $conf];
  114. static::load($name, $conf);
  115. }
  116. return;
  117. }
  118. static::_loadConfig();
  119. $config += [
  120. 'autoload' => false,
  121. 'bootstrap' => false,
  122. 'routes' => false,
  123. 'classBase' => 'src',
  124. 'ignoreMissing' => false
  125. ];
  126. if (!isset($config['path'])) {
  127. $config['path'] = Configure::read('plugins.' . $plugin);
  128. }
  129. if (empty($config['path'])) {
  130. $paths = App::path('Plugin');
  131. $pluginPath = str_replace('/', DS, $plugin);
  132. foreach ($paths as $path) {
  133. if (is_dir($path . $pluginPath)) {
  134. $config['path'] = $path . $pluginPath . DS;
  135. break;
  136. }
  137. }
  138. }
  139. if (empty($config['path'])) {
  140. throw new MissingPluginException(['plugin' => $plugin]);
  141. }
  142. $config['classPath'] = $config['path'] . $config['classBase'] . DS;
  143. if (!isset($config['configPath'])) {
  144. $config['configPath'] = $config['path'] . 'config' . DS;
  145. }
  146. static::$_plugins[$plugin] = $config;
  147. if ($config['autoload'] === true) {
  148. if (empty(static::$_loader)) {
  149. static::$_loader = new ClassLoader;
  150. static::$_loader->register();
  151. }
  152. static::$_loader->addNamespace(
  153. str_replace('/', '\\', $plugin),
  154. $config['path'] . $config['classBase'] . DS
  155. );
  156. static::$_loader->addNamespace(
  157. str_replace('/', '\\', $plugin) . '\Test',
  158. $config['path'] . 'tests' . DS
  159. );
  160. }
  161. if ($config['bootstrap'] === true) {
  162. static::bootstrap($plugin);
  163. }
  164. }
  165. /**
  166. * Load the plugin path configuration file.
  167. *
  168. * @return void
  169. */
  170. protected static function _loadConfig()
  171. {
  172. if (Configure::check('plugins')) {
  173. return;
  174. }
  175. $vendorFile = dirname(dirname(dirname(dirname(__DIR__)))) . DS . 'cakephp-plugins.php';
  176. if (!file_exists($vendorFile)) {
  177. Configure::write(['plugins' => []]);
  178. return;
  179. }
  180. $config = require $vendorFile;
  181. Configure::write($config);
  182. }
  183. /**
  184. * Will load all the plugins located in the default plugin folder.
  185. *
  186. * If passed an options array, it will be used as a common default for all plugins to be loaded
  187. * It is possible to set specific defaults for each plugins in the options array. Examples:
  188. *
  189. * ```
  190. * Plugin::loadAll([
  191. * ['bootstrap' => true],
  192. * 'DebugKit' => ['routes' => true],
  193. * ]);
  194. * ```
  195. *
  196. * The above example will load the bootstrap file for all plugins, but for DebugKit it will only load the routes file
  197. * and will not look for any bootstrap script.
  198. *
  199. * If a plugin has been loaded already, it will not be reloaded by loadAll().
  200. *
  201. * @param array $options Options.
  202. * @return void
  203. */
  204. public static function loadAll(array $options = [])
  205. {
  206. static::_loadConfig();
  207. $plugins = [];
  208. foreach (App::path('Plugin') as $path) {
  209. if (!is_dir($path)) {
  210. continue;
  211. }
  212. $dir = new DirectoryIterator($path);
  213. foreach ($dir as $path) {
  214. if ($path->isDir() && !$path->isDot()) {
  215. $plugins[] = $path->getBaseName();
  216. }
  217. }
  218. }
  219. if (Configure::check('plugins')) {
  220. $plugins = array_merge($plugins, array_keys(Configure::read('plugins')));
  221. $plugins = array_unique($plugins);
  222. }
  223. foreach ($plugins as $p) {
  224. $opts = isset($options[$p]) ? $options[$p] : null;
  225. if ($opts === null && isset($options[0])) {
  226. $opts = $options[0];
  227. }
  228. if (isset(static::$_plugins[$p])) {
  229. continue;
  230. }
  231. static::load($p, (array)$opts);
  232. }
  233. }
  234. /**
  235. * Returns the filesystem path for a plugin
  236. *
  237. * @param string $plugin name of the plugin in CamelCase format
  238. * @return string path to the plugin folder
  239. * @throws \Cake\Core\Exception\MissingPluginException if the folder for plugin was not found or plugin has not been loaded
  240. */
  241. public static function path($plugin)
  242. {
  243. if (empty(static::$_plugins[$plugin])) {
  244. throw new MissingPluginException(['plugin' => $plugin]);
  245. }
  246. return static::$_plugins[$plugin]['path'];
  247. }
  248. /**
  249. * Returns the filesystem path for plugin's folder containing class folders.
  250. *
  251. * @param string $plugin name of the plugin in CamelCase format.
  252. * @return string Path to the plugin folder container class folders.
  253. * @throws \Cake\Core\Exception\MissingPluginException If plugin has not been loaded.
  254. */
  255. public static function classPath($plugin)
  256. {
  257. if (empty(static::$_plugins[$plugin])) {
  258. throw new MissingPluginException(['plugin' => $plugin]);
  259. }
  260. return static::$_plugins[$plugin]['classPath'];
  261. }
  262. /**
  263. * Returns the filesystem path for plugin's folder containing config files.
  264. *
  265. * @param string $plugin name of the plugin in CamelCase format.
  266. * @return string Path to the plugin folder container config files.
  267. * @throws \Cake\Core\Exception\MissingPluginException If plugin has not been loaded.
  268. */
  269. public static function configPath($plugin)
  270. {
  271. if (empty(static::$_plugins[$plugin])) {
  272. throw new MissingPluginException(['plugin' => $plugin]);
  273. }
  274. return static::$_plugins[$plugin]['configPath'];
  275. }
  276. /**
  277. * Loads the bootstrapping files for a plugin, or calls the initialization setup in the configuration
  278. *
  279. * @param string $plugin name of the plugin
  280. * @return mixed
  281. * @see Plugin::load() for examples of bootstrap configuration
  282. */
  283. public static function bootstrap($plugin)
  284. {
  285. $config = static::$_plugins[$plugin];
  286. if ($config['bootstrap'] === false) {
  287. return false;
  288. }
  289. if ($config['bootstrap'] === true) {
  290. return static::_includeFile(
  291. $config['configPath'] . 'bootstrap.php',
  292. $config['ignoreMissing']
  293. );
  294. }
  295. }
  296. /**
  297. * Loads the routes file for a plugin, or all plugins configured to load their respective routes file
  298. *
  299. * @param string $plugin name of the plugin, if null will operate on all plugins having enabled the
  300. * loading of routes files
  301. * @return bool
  302. */
  303. public static function routes($plugin = null)
  304. {
  305. if ($plugin === null) {
  306. foreach (static::loaded() as $p) {
  307. static::routes($p);
  308. }
  309. return true;
  310. }
  311. $config = static::$_plugins[$plugin];
  312. if ($config['routes'] === false) {
  313. return false;
  314. }
  315. return (bool)static::_includeFile(
  316. $config['configPath'] . 'routes.php',
  317. $config['ignoreMissing']
  318. );
  319. }
  320. /**
  321. * Returns true if the plugin $plugin is already loaded
  322. * If plugin is null, it will return a list of all loaded plugins
  323. *
  324. * @param string $plugin Plugin name.
  325. * @return mixed boolean true if $plugin is already loaded.
  326. * If $plugin is null, returns a list of plugins that have been loaded
  327. */
  328. public static function loaded($plugin = null)
  329. {
  330. if ($plugin) {
  331. return isset(static::$_plugins[$plugin]);
  332. }
  333. $return = array_keys(static::$_plugins);
  334. sort($return);
  335. return $return;
  336. }
  337. /**
  338. * Forgets a loaded plugin or all of them if first parameter is null
  339. *
  340. * @param string $plugin name of the plugin to forget
  341. * @return void
  342. */
  343. public static function unload($plugin = null)
  344. {
  345. if ($plugin === null) {
  346. static::$_plugins = [];
  347. } else {
  348. unset(static::$_plugins[$plugin]);
  349. }
  350. }
  351. /**
  352. * Include file, ignoring include error if needed if file is missing
  353. *
  354. * @param string $file File to include
  355. * @param bool $ignoreMissing Whether to ignore include error for missing files
  356. * @return mixed
  357. */
  358. protected static function _includeFile($file, $ignoreMissing = false)
  359. {
  360. if ($ignoreMissing && !is_file($file)) {
  361. return false;
  362. }
  363. return include $file;
  364. }
  365. }