PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/cakephp/core/Plugin.php

http://github.com/josegonzalez/git-php
PHP | 393 lines | 216 code | 24 blank | 153 comment | 12 complexity | df225217a9355188d7f1b14486d1329b MD5 | raw file
  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. */
  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. $config += [
  117. 'autoload' => false,
  118. 'bootstrap' => false,
  119. 'routes' => false,
  120. 'console' => true,
  121. 'classBase' => 'src',
  122. 'ignoreMissing' => false,
  123. 'name' => $plugin
  124. ];
  125. if (!isset($config['path'])) {
  126. $config['path'] = static::getCollection()->findPath($plugin);
  127. }
  128. $config['classPath'] = $config['path'] . $config['classBase'] . DIRECTORY_SEPARATOR;
  129. if (!isset($config['configPath'])) {
  130. $config['configPath'] = $config['path'] . 'config' . DIRECTORY_SEPARATOR;
  131. }
  132. $pluginClass = str_replace('/', '\\', $plugin) . '\\Plugin';
  133. if (class_exists($pluginClass)) {
  134. $instance = new $pluginClass($config);
  135. } else {
  136. // Use stub plugin as this method will be removed long term.
  137. $instance = new BasePlugin($config);
  138. }
  139. static::getCollection()->add($instance);
  140. if ($config['autoload'] === true) {
  141. if (empty(static::$_loader)) {
  142. static::$_loader = new ClassLoader();
  143. static::$_loader->register();
  144. }
  145. static::$_loader->addNamespace(
  146. str_replace('/', '\\', $plugin),
  147. $config['path'] . $config['classBase'] . DIRECTORY_SEPARATOR
  148. );
  149. static::$_loader->addNamespace(
  150. str_replace('/', '\\', $plugin) . '\Test',
  151. $config['path'] . 'tests' . DIRECTORY_SEPARATOR
  152. );
  153. }
  154. if ($config['bootstrap'] === true) {
  155. static::bootstrap($plugin);
  156. }
  157. }
  158. /**
  159. * Will load all the plugins located in the default plugin folder.
  160. *
  161. * If passed an options array, it will be used as a common default for all plugins to be loaded
  162. * It is possible to set specific defaults for each plugins in the options array. Examples:
  163. *
  164. * ```
  165. * Plugin::loadAll([
  166. * ['bootstrap' => true],
  167. * 'DebugKit' => ['routes' => true],
  168. * ]);
  169. * ```
  170. *
  171. * The above example will load the bootstrap file for all plugins, but for DebugKit it will only load the routes file
  172. * and will not look for any bootstrap script.
  173. *
  174. * If a plugin has been loaded already, it will not be reloaded by loadAll().
  175. *
  176. * @param array $options Options.
  177. * @return void
  178. * @throws \Cake\Core\Exception\MissingPluginException
  179. */
  180. public static function loadAll(array $options = [])
  181. {
  182. $plugins = [];
  183. foreach (App::path('Plugin') as $path) {
  184. if (!is_dir($path)) {
  185. continue;
  186. }
  187. $dir = new DirectoryIterator($path);
  188. foreach ($dir as $dirPath) {
  189. if ($dirPath->isDir() && !$dirPath->isDot()) {
  190. $plugins[] = $dirPath->getBasename();
  191. }
  192. }
  193. }
  194. if (Configure::check('plugins')) {
  195. $plugins = array_merge($plugins, array_keys(Configure::read('plugins')));
  196. $plugins = array_unique($plugins);
  197. }
  198. $collection = static::getCollection();
  199. foreach ($plugins as $p) {
  200. $opts = isset($options[$p]) ? $options[$p] : null;
  201. if ($opts === null && isset($options[0])) {
  202. $opts = $options[0];
  203. }
  204. if ($collection->has($p)) {
  205. continue;
  206. }
  207. static::load($p, (array)$opts);
  208. }
  209. }
  210. /**
  211. * Returns the filesystem path for a plugin
  212. *
  213. * @param string $name name of the plugin in CamelCase format
  214. * @return string path to the plugin folder
  215. * @throws \Cake\Core\Exception\MissingPluginException if the folder for plugin was not found or plugin has not been loaded
  216. */
  217. public static function path($name)
  218. {
  219. $plugin = static::getCollection()->get($name);
  220. return $plugin->getPath();
  221. }
  222. /**
  223. * Returns the filesystem path for plugin's folder containing class folders.
  224. *
  225. * @param string $name name of the plugin in CamelCase format.
  226. * @return string Path to the plugin folder container class folders.
  227. * @throws \Cake\Core\Exception\MissingPluginException If plugin has not been loaded.
  228. */
  229. public static function classPath($name)
  230. {
  231. $plugin = static::getCollection()->get($name);
  232. return $plugin->getClassPath();
  233. }
  234. /**
  235. * Returns the filesystem path for plugin's folder containing config files.
  236. *
  237. * @param string $name name of the plugin in CamelCase format.
  238. * @return string Path to the plugin folder container config files.
  239. * @throws \Cake\Core\Exception\MissingPluginException If plugin has not been loaded.
  240. */
  241. public static function configPath($name)
  242. {
  243. $plugin = static::getCollection()->get($name);
  244. return $plugin->getConfigPath();
  245. }
  246. /**
  247. * Loads the bootstrapping files for a plugin, or calls the initialization setup in the configuration
  248. *
  249. * @param string $name name of the plugin
  250. * @return mixed
  251. * @see \Cake\Core\Plugin::load() for examples of bootstrap configuration
  252. */
  253. public static function bootstrap($name)
  254. {
  255. $plugin = static::getCollection()->get($name);
  256. if (!$plugin->isEnabled('bootstrap')) {
  257. return false;
  258. }
  259. // Disable bootstrapping for this plugin as it will have
  260. // been bootstrapped.
  261. $plugin->disable('bootstrap');
  262. return static::_includeFile(
  263. $plugin->getConfigPath() . 'bootstrap.php',
  264. true
  265. );
  266. }
  267. /**
  268. * Loads the routes file for a plugin, or all plugins configured to load their respective routes file.
  269. *
  270. * If you need fine grained control over how routes are loaded for plugins, you
  271. * can use {@see Cake\Routing\RouteBuilder::loadPlugin()}
  272. *
  273. * @param string|null $name name of the plugin, if null will operate on all
  274. * plugins having enabled the loading of routes files.
  275. * @return bool
  276. * @deprecated 3.6.5 This method is no longer needed when using HttpApplicationInterface based applications.
  277. * This method will be removed in 4.0.0
  278. */
  279. public static function routes($name = null)
  280. {
  281. deprecationWarning(
  282. 'You no longer need to call `Plugin::routes()` after upgrading to use Http\Server. ' .
  283. 'See https://book.cakephp.org/3.0/en/development/application.html#adding-the-new-http-stack-to-an-existing-application ' .
  284. 'for upgrade information.'
  285. );
  286. if ($name === null) {
  287. foreach (static::loaded() as $p) {
  288. static::routes($p);
  289. }
  290. return true;
  291. }
  292. $plugin = static::getCollection()->get($name);
  293. if (!$plugin->isEnabled('routes')) {
  294. return false;
  295. }
  296. return (bool)static::_includeFile(
  297. $plugin->getConfigPath() . 'routes.php',
  298. true
  299. );
  300. }
  301. /**
  302. * Returns true if the plugin $plugin is already loaded
  303. * If plugin is null, it will return a list of all loaded plugins
  304. *
  305. * @param string|null $plugin Plugin name.
  306. * @return bool|array Boolean true if $plugin is already loaded.
  307. * If $plugin is null, returns a list of plugins that have been loaded
  308. */
  309. public static function loaded($plugin = null)
  310. {
  311. if ($plugin !== null) {
  312. return static::getCollection()->has($plugin);
  313. }
  314. $names = [];
  315. foreach (static::getCollection() as $plugin) {
  316. $names[] = $plugin->getName();
  317. }
  318. sort($names);
  319. return $names;
  320. }
  321. /**
  322. * Forgets a loaded plugin or all of them if first parameter is null
  323. *
  324. * @param string|null $plugin name of the plugin to forget
  325. * @return void
  326. */
  327. public static function unload($plugin = null)
  328. {
  329. if ($plugin === null) {
  330. static::$plugins = null;
  331. } else {
  332. static::getCollection()->remove($plugin);
  333. }
  334. }
  335. /**
  336. * Include file, ignoring include error if needed if file is missing
  337. *
  338. * @param string $file File to include
  339. * @param bool $ignoreMissing Whether to ignore include error for missing files
  340. * @return mixed
  341. */
  342. protected static function _includeFile($file, $ignoreMissing = false)
  343. {
  344. if ($ignoreMissing && !is_file($file)) {
  345. return false;
  346. }
  347. return include $file;
  348. }
  349. /**
  350. * Get the shared plugin collection.
  351. *
  352. * @internal
  353. * @return \Cake\Core\PluginCollection
  354. */
  355. public static function getCollection()
  356. {
  357. if (!isset(static::$plugins)) {
  358. static::$plugins = new PluginCollection();
  359. }
  360. return static::$plugins;
  361. }
  362. }