CakePlugin.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. */
  24. class CakePlugin {
  25. /**
  26. * Holds a list of all loaded plugins and their configuration
  27. *
  28. * @var array
  29. */
  30. protected static $_plugins = array();
  31. /**
  32. * Loads a plugin and optionally loads bootstrapping, routing files or loads a initialization function
  33. *
  34. * Examples:
  35. *
  36. * `CakePlugin::load('DebugKit')` will load the DebugKit plugin and will not load any bootstrap nor route files
  37. * `CakePlugin::load('DebugKit', array('bootstrap' => true, 'routes' => true))` will load the bootstrap.php and routes.php files
  38. * `CakePlugin::load('DebugKit', array('bootstrap' => false, 'routes' => true))` will load routes.php file but not bootstrap.php
  39. * `CakePlugin::load('DebugKit', array('bootstrap' => array('config1', 'config2')))` will load config1.php and config2.php files
  40. * `CakePlugin::load('DebugKit', array('bootstrap' => 'aCallableMethod'))` will run the aCallableMethod function to initialize it
  41. *
  42. * Bootstrap initialization functions can be expressed as a PHP callback type, including closures. Callbacks will receive two
  43. * parameters (plugin name, plugin configuration)
  44. *
  45. * It is also possible to load multiple plugins at once. Examples:
  46. *
  47. * `CakePlugin::load(array('DebugKit', 'ApiGenerator'))` will load the DebugKit and ApiGenerator plugins
  48. * `CakePlugin::load(array('DebugKit', 'ApiGenerator'), array('bootstrap' => true))` will load bootstrap file for both plugins
  49. *
  50. * {{{
  51. * CakePlugin::load(array(
  52. * 'DebugKit' => array('routes' => true),
  53. * 'ApiGenerator'
  54. * ), array('bootstrap' => true))
  55. * }}}
  56. *
  57. * Will only load the bootstrap for ApiGenerator and only the routes for DebugKit
  58. *
  59. * @param mixed $plugin name of the plugin to be loaded in CamelCase format or array or plugins to load
  60. * @param array $config configuration options for the plugin
  61. * @throws MissingPluginException if the folder for the plugin to be loaded is not found
  62. * @return void
  63. */
  64. public static function load($plugin, $config = array()) {
  65. if (is_array($plugin)) {
  66. foreach ($plugin as $name => $conf) {
  67. list($name, $conf) = (is_numeric($name)) ? array($conf, $config) : array($name, $conf);
  68. self::load($name, $conf);
  69. }
  70. return;
  71. }
  72. $config += array('bootstrap' => false, 'routes' => false);
  73. if (empty($config['path'])) {
  74. foreach (App::path('plugins') as $path) {
  75. if (is_dir($path . $plugin)) {
  76. self::$_plugins[$plugin] = $config + array('path' => $path . $plugin . DS);
  77. break;
  78. }
  79. //Backwards compatibility to make easier to migrate to 2.0
  80. $underscored = Inflector::underscore($plugin);
  81. if (is_dir($path . $underscored)) {
  82. self::$_plugins[$plugin] = $config + array('path' => $path . $underscored . DS);
  83. break;
  84. }
  85. }
  86. } else {
  87. self::$_plugins[$plugin] = $config;
  88. }
  89. if (empty(self::$_plugins[$plugin]['path'])) {
  90. throw new MissingPluginException(array('plugin' => $plugin));
  91. }
  92. if (!empty(self::$_plugins[$plugin]['bootstrap'])) {
  93. self::bootstrap($plugin);
  94. }
  95. }
  96. /**
  97. * Will load all the plugins located in the configured plugins folders
  98. * If passed an options array, it will be used as a common default for all plugins to be loaded
  99. * It is possible to set specific defaults for each plugins in the options array. Examples:
  100. *
  101. * {{{
  102. * CakePlugin::loadAll(array(
  103. * array('bootstrap' => true),
  104. * 'DebugKit' => array('routes' => true),
  105. * ))
  106. * }}}
  107. *
  108. * The above example will load the bootstrap file for all plugins, but for DebugKit it will only load the routes file
  109. * and will not look for any bootstrap script.
  110. *
  111. * @param array $options
  112. * @return void
  113. */
  114. public static function loadAll($options = array()) {
  115. $plugins = App::objects('plugins');
  116. foreach ($plugins as $p) {
  117. $opts = isset($options[$p]) ? $options[$p] : null;
  118. if ($opts === null && isset($options[0])) {
  119. $opts = $options[0];
  120. }
  121. self::load($p, (array) $opts);
  122. }
  123. }
  124. /**
  125. * Returns the filesystem path for a plugin
  126. *
  127. * @param string $plugin name of the plugin in CamelCase format
  128. * @return string path to the plugin folder
  129. * @throws MissingPluginException if the folder for plugin was not found or plugin has not been loaded
  130. */
  131. public static function path($plugin) {
  132. if (empty(self::$_plugins[$plugin])) {
  133. throw new MissingPluginException(array('plugin' => $plugin));
  134. }
  135. return self::$_plugins[$plugin]['path'];
  136. }
  137. /**
  138. * Loads the bootstrapping files for a plugin, or calls the initialization setup in the configuration
  139. *
  140. * @param string $plugin name of the plugin
  141. * @return mixed
  142. * @see CakePlugin::load() for examples of bootstrap configuration
  143. */
  144. public static function bootstrap($plugin) {
  145. $config = self::$_plugins[$plugin];
  146. if ($config['bootstrap'] === false) {
  147. return false;
  148. }
  149. if (is_callable($config['bootstrap'])) {
  150. return call_user_func_array($config['bootstrap'], array($plugin, $config));
  151. }
  152. $path = self::path($plugin);
  153. if ($config['bootstrap'] === true) {
  154. return include($path . 'Config' . DS . 'bootstrap.php');
  155. }
  156. $bootstrap = (array)$config['bootstrap'];
  157. foreach ($bootstrap as $file) {
  158. include $path . 'Config' . DS . $file . '.php';
  159. }
  160. return true;
  161. }
  162. /**
  163. * Loads the routes file for a plugin, or all plugins configured to load their respective routes file
  164. *
  165. * @param string $plugin name of the plugin, if null will operate on all plugins having enabled the
  166. * loading of routes files
  167. * @return boolean
  168. */
  169. public static function routes($plugin = null) {
  170. if ($plugin === null) {
  171. foreach (self::loaded() as $p) {
  172. self::routes($p);
  173. }
  174. return true;
  175. }
  176. $config = self::$_plugins[$plugin];
  177. if ($config['routes'] === false) {
  178. return false;
  179. }
  180. return (bool) include self::path($plugin) . 'Config' . DS . 'routes.php';
  181. }
  182. /**
  183. * Retruns true if the plugin $plugin is already loaded
  184. * If plugin is null, it will return a list of all loaded plugins
  185. *
  186. * @param string $plugin
  187. * @return mixed boolean true if $plugin is already loaded.
  188. * If $plugin is null, returns a list of plugins that have been loaded
  189. */
  190. public static function loaded($plugin = null) {
  191. if ($plugin) {
  192. return isset(self::$_plugins[$plugin]);
  193. }
  194. $return = array_keys(self::$_plugins);
  195. sort($return);
  196. return $return;
  197. }
  198. /**
  199. * Forgets a loaded plugin or all of them if first parameter is null
  200. *
  201. * @param string $plugin name of the plugin to forget
  202. * @return void
  203. */
  204. public static function unload($plugin = null) {
  205. if (is_null($plugin)) {
  206. self::$_plugins = array();
  207. } else {
  208. unset(self::$_plugins[$plugin]);
  209. }
  210. }
  211. }