PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Core/CakePlugin.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 227 lines | 97 code | 14 blank | 116 comment | 18 complexity | d96f70122aaa9fd7525c8fa290099fd5 MD5 | raw file
  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 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 mixed $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);
  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 include($path . 'Config' . DS . 'bootstrap.php');
  157. }
  158. $bootstrap = (array)$config['bootstrap'];
  159. foreach ($bootstrap as $file) {
  160. include $path . 'Config' . DS . $file . '.php';
  161. }
  162. return true;
  163. }
  164. /**
  165. * Loads the routes file for a plugin, or all plugins configured to load their respective routes file
  166. *
  167. * @param string $plugin name of the plugin, if null will operate on all plugins having enabled the
  168. * loading of routes files
  169. * @return boolean
  170. */
  171. public static function routes($plugin = null) {
  172. if ($plugin === null) {
  173. foreach (self::loaded() as $p) {
  174. self::routes($p);
  175. }
  176. return true;
  177. }
  178. $config = self::$_plugins[$plugin];
  179. if ($config['routes'] === false) {
  180. return false;
  181. }
  182. return (bool) include self::path($plugin) . 'Config' . DS . 'routes.php';
  183. }
  184. /**
  185. * Returns true if the plugin $plugin is already loaded
  186. * If plugin is null, it will return a list of all loaded plugins
  187. *
  188. * @param string $plugin
  189. * @return mixed boolean true if $plugin is already loaded.
  190. * If $plugin is null, returns a list of plugins that have been loaded
  191. */
  192. public static function loaded($plugin = null) {
  193. if ($plugin) {
  194. return isset(self::$_plugins[$plugin]);
  195. }
  196. $return = array_keys(self::$_plugins);
  197. sort($return);
  198. return $return;
  199. }
  200. /**
  201. * Forgets a loaded plugin or all of them if first parameter is null
  202. *
  203. * @param string $plugin name of the plugin to forget
  204. * @return void
  205. */
  206. public static function unload($plugin = null) {
  207. if (is_null($plugin)) {
  208. self::$_plugins = array();
  209. } else {
  210. unset(self::$_plugins[$plugin]);
  211. }
  212. }
  213. }