CakePlugin.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * CakePlugin class
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Core
  15. * @since CakePHP(tm) v 2.0.0
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. /**
  19. * CakePlugin is responsible for loading and unloading plugins. It also can
  20. * retrieve plugin paths and load their bootstrap and routes files.
  21. *
  22. * @package Cake.Core
  23. * @link http://book.cakephp.org/2.0/en/plugins.html
  24. */
  25. class CakePlugin {
  26. /**
  27. * Holds a list of all loaded plugins and their configuration
  28. *
  29. * @var array
  30. */
  31. protected static $_plugins = array();
  32. /**
  33. * Loads a plugin and optionally loads bootstrapping, routing files or loads an initialization function
  34. *
  35. * Examples:
  36. *
  37. * `CakePlugin::load('DebugKit')` will load the DebugKit plugin and will not load any bootstrap nor route files
  38. * `CakePlugin::load('DebugKit', array('bootstrap' => true, 'routes' => true))` will load the bootstrap.php and routes.php files
  39. * `CakePlugin::load('DebugKit', array('bootstrap' => false, 'routes' => true))` will load routes.php file but not bootstrap.php
  40. * `CakePlugin::load('DebugKit', array('bootstrap' => array('config1', 'config2')))` will load config1.php and config2.php files
  41. * `CakePlugin::load('DebugKit', array('bootstrap' => 'aCallableMethod'))` will run the aCallableMethod function to initialize it
  42. *
  43. * Bootstrap initialization functions can be expressed as a PHP callback type, including closures. Callbacks will receive two
  44. * parameters (plugin name, plugin configuration)
  45. *
  46. * It is also possible to load multiple plugins at once. Examples:
  47. *
  48. * `CakePlugin::load(array('DebugKit', 'ApiGenerator'))` will load the DebugKit and ApiGenerator plugins
  49. * `CakePlugin::load(array('DebugKit', 'ApiGenerator'), array('bootstrap' => true))` will load bootstrap file for both plugins
  50. *
  51. * {{{
  52. * CakePlugin::load(array(
  53. * 'DebugKit' => array('routes' => true),
  54. * 'ApiGenerator'
  55. * ), array('bootstrap' => true))
  56. * }}}
  57. *
  58. * Will only load the bootstrap for ApiGenerator and only the routes for DebugKit
  59. *
  60. * @param string|array $plugin name of the plugin to be loaded in CamelCase format or array or plugins to load
  61. * @param array $config configuration options for the plugin
  62. * @throws MissingPluginException if the folder for the plugin to be loaded is not found
  63. * @return void
  64. */
  65. public static function load($plugin, $config = array()) {
  66. if (is_array($plugin)) {
  67. foreach ($plugin as $name => $conf) {
  68. list($name, $conf) = (is_numeric($name)) ? array($conf, $config) : array($name, $conf);
  69. self::load($name, $conf);
  70. }
  71. return;
  72. }
  73. $config += array('bootstrap' => false, 'routes' => false, 'ignoreMissing' => false);
  74. if (empty($config['path'])) {
  75. foreach (App::path('plugins') as $path) {
  76. if (is_dir($path . $plugin)) {
  77. self::$_plugins[$plugin] = $config + array('path' => $path . $plugin . DS);
  78. break;
  79. }
  80. //Backwards compatibility to make easier to migrate to 2.0
  81. $underscored = Inflector::underscore($plugin);
  82. if (is_dir($path . $underscored)) {
  83. self::$_plugins[$plugin] = $config + array('path' => $path . $underscored . DS);
  84. break;
  85. }
  86. }
  87. } else {
  88. self::$_plugins[$plugin] = $config;
  89. }
  90. if (empty(self::$_plugins[$plugin]['path'])) {
  91. throw new MissingPluginException(array('plugin' => $plugin));
  92. }
  93. if (!empty(self::$_plugins[$plugin]['bootstrap'])) {
  94. self::bootstrap($plugin);
  95. }
  96. }
  97. /**
  98. * Will load all the plugins located in the configured plugins folders
  99. * If passed an options array, it will be used as a common default for all plugins to be loaded
  100. * It is possible to set specific defaults for each plugins in the options array. Examples:
  101. *
  102. * {{{
  103. * CakePlugin::loadAll(array(
  104. * array('bootstrap' => true),
  105. * 'DebugKit' => array('routes' => true, 'bootstrap' => false),
  106. * ))
  107. * }}}
  108. *
  109. * The above example will load the bootstrap file for all plugins, but for DebugKit it will only load
  110. * the routes file and will not look for any bootstrap script.
  111. *
  112. * @param array $options Options list. See CakePlugin::load() for valid options.
  113. * @return void
  114. */
  115. public static function loadAll($options = array()) {
  116. $plugins = App::objects('plugins');
  117. foreach ($plugins as $p) {
  118. $opts = isset($options[$p]) ? (array)$options[$p] : array();
  119. if (isset($options[0])) {
  120. $opts += $options[0];
  121. }
  122. self::load($p, $opts);
  123. }
  124. }
  125. /**
  126. * Returns the filesystem path for a plugin
  127. *
  128. * @param string $plugin name of the plugin in CamelCase format
  129. * @return string path to the plugin folder
  130. * @throws MissingPluginException if the folder for plugin was not found or plugin has not been loaded
  131. */
  132. public static function path($plugin) {
  133. if (empty(self::$_plugins[$plugin])) {
  134. throw new MissingPluginException(array('plugin' => $plugin));
  135. }
  136. return self::$_plugins[$plugin]['path'];
  137. }
  138. /**
  139. * Loads the bootstrapping files for a plugin, or calls the initialization setup in the configuration
  140. *
  141. * @param string $plugin name of the plugin
  142. * @return mixed
  143. * @see CakePlugin::load() for examples of bootstrap configuration
  144. */
  145. public static function bootstrap($plugin) {
  146. $config = self::$_plugins[$plugin];
  147. if ($config['bootstrap'] === false) {
  148. return false;
  149. }
  150. if (is_callable($config['bootstrap'])) {
  151. return call_user_func_array($config['bootstrap'], array($plugin, $config));
  152. }
  153. $path = self::path($plugin);
  154. if ($config['bootstrap'] === true) {
  155. return self::_includeFile(
  156. $path . 'Config' . DS . 'bootstrap.php',
  157. $config['ignoreMissing']
  158. );
  159. }
  160. $bootstrap = (array)$config['bootstrap'];
  161. foreach ($bootstrap as $file) {
  162. self::_includeFile(
  163. $path . 'Config' . DS . $file . '.php',
  164. $config['ignoreMissing']
  165. );
  166. }
  167. return true;
  168. }
  169. /**
  170. * Loads the routes file for a plugin, or all plugins configured to load their respective routes file
  171. *
  172. * @param string $plugin name of the plugin, if null will operate on all plugins having enabled the
  173. * loading of routes files
  174. * @return bool
  175. */
  176. public static function routes($plugin = null) {
  177. if ($plugin === null) {
  178. foreach (self::loaded() as $p) {
  179. self::routes($p);
  180. }
  181. return true;
  182. }
  183. $config = self::$_plugins[$plugin];
  184. if ($config['routes'] === false) {
  185. return false;
  186. }
  187. return (bool)self::_includeFile(
  188. self::path($plugin) . 'Config' . DS . 'routes.php',
  189. $config['ignoreMissing']
  190. );
  191. }
  192. /**
  193. * Returns true if the plugin $plugin is already loaded
  194. * If plugin is null, it will return a list of all loaded plugins
  195. *
  196. * @param string $plugin Plugin name to check.
  197. * @return mixed boolean true if $plugin is already loaded.
  198. * If $plugin is null, returns a list of plugins that have been loaded
  199. */
  200. public static function loaded($plugin = null) {
  201. if ($plugin) {
  202. return isset(self::$_plugins[$plugin]);
  203. }
  204. $return = array_keys(self::$_plugins);
  205. sort($return);
  206. return $return;
  207. }
  208. /**
  209. * Forgets a loaded plugin or all of them if first parameter is null
  210. *
  211. * @param string $plugin name of the plugin to forget
  212. * @return void
  213. */
  214. public static function unload($plugin = null) {
  215. if ($plugin === null) {
  216. self::$_plugins = array();
  217. } else {
  218. unset(self::$_plugins[$plugin]);
  219. }
  220. }
  221. /**
  222. * Include file, ignoring include error if needed if file is missing
  223. *
  224. * @param string $file File to include
  225. * @param bool $ignoreMissing Whether to ignore include error for missing files
  226. * @return mixed
  227. */
  228. protected static function _includeFile($file, $ignoreMissing = false) {
  229. if ($ignoreMissing && !is_file($file)) {
  230. return false;
  231. }
  232. return include $file;
  233. }
  234. }