CakePlugin.php 7.7 KB

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