CakePlugin.php 6.2 KB

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