CakePlugin.php 7.8 KB

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