CakePlugin.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * CakePlugin class
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, 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-2011, 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 class
  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 a 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 mixed $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);
  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),
  106. * ))
  107. * }}}
  108. *
  109. * The above example will load the bootstrap file for all plugins, but for DebugKit it will only load the routes file
  110. * and will not look for any bootstrap script.
  111. *
  112. * @param array $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]) ? $options[$p] : null;
  119. if ($opts === null && isset($options[0])) {
  120. $opts = $options[0];
  121. }
  122. self::load($p, (array) $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 include($path . 'Config' . DS . 'bootstrap.php');
  156. }
  157. $bootstrap = (array)$config['bootstrap'];
  158. foreach ($bootstrap as $file) {
  159. include $path . 'Config' . DS . $file . '.php';
  160. }
  161. return true;
  162. }
  163. /**
  164. * Loads the routes file for a plugin, or all plugins configured to load their respective routes file
  165. *
  166. * @param string $plugin name of the plugin, if null will operate on all plugins having enabled the
  167. * loading of routes files
  168. * @return boolean
  169. */
  170. public static function routes($plugin = null) {
  171. if ($plugin === null) {
  172. foreach (self::loaded() as $p) {
  173. self::routes($p);
  174. }
  175. return true;
  176. }
  177. $config = self::$_plugins[$plugin];
  178. if ($config['routes'] === false) {
  179. return false;
  180. }
  181. return (bool) include self::path($plugin) . 'Config' . DS . 'routes.php';
  182. }
  183. /**
  184. * Returns true if the plugin $plugin is already loaded
  185. * If plugin is null, it will return a list of all loaded plugins
  186. *
  187. * @param string $plugin
  188. * @return mixed boolean true if $plugin is already loaded.
  189. * If $plugin is null, returns a list of plugins that have been loaded
  190. */
  191. public static function loaded($plugin = null) {
  192. if ($plugin) {
  193. return isset(self::$_plugins[$plugin]);
  194. }
  195. $return = array_keys(self::$_plugins);
  196. sort($return);
  197. return $return;
  198. }
  199. /**
  200. * Forgets a loaded plugin or all of them if first parameter is null
  201. *
  202. * @param string $plugin name of the plugin to forget
  203. * @return void
  204. */
  205. public static function unload($plugin = null) {
  206. if (is_null($plugin)) {
  207. self::$_plugins = array();
  208. } else {
  209. unset(self::$_plugins[$plugin]);
  210. }
  211. }
  212. }