2011-04-17 06:07:31 +00:00
|
|
|
<?php
|
2011-07-29 03:24:31 +00:00
|
|
|
/**
|
|
|
|
* CakePlugin class
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-07-29 03:24:31 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2011-07-29 03:24:31 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-07-29 03:24:31 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @package Cake.Core
|
|
|
|
* @since CakePHP(tm) v 2.0.0
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2011-07-29 03:24:31 +00:00
|
|
|
*/
|
2011-04-17 06:07:31 +00:00
|
|
|
|
2011-07-29 03:24:31 +00:00
|
|
|
/**
|
2012-07-18 01:55:29 +00:00
|
|
|
* CakePlugin is responsible for loading and unloading plugins. It also can
|
2011-12-08 15:35:02 +00:00
|
|
|
* retrieve plugin paths and load their bootstrap and routes files.
|
2011-07-29 03:24:31 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Core
|
2011-10-15 16:08:49 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/plugins.html
|
2011-07-29 03:24:31 +00:00
|
|
|
*/
|
2011-04-17 06:07:31 +00:00
|
|
|
class CakePlugin {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds a list of all loaded plugins and their configuration
|
|
|
|
*
|
2011-07-31 21:05:20 +00:00
|
|
|
* @var array
|
2011-04-17 06:07:31 +00:00
|
|
|
*/
|
2011-08-20 04:43:34 +00:00
|
|
|
protected static $_plugins = array();
|
2011-04-17 06:07:31 +00:00
|
|
|
|
|
|
|
/**
|
2014-07-30 20:11:03 +00:00
|
|
|
* Loads a plugin and optionally loads bootstrapping, routing files or loads an initialization function
|
2011-04-17 06:07:31 +00:00
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
*
|
|
|
|
* `CakePlugin::load('DebugKit')` will load the DebugKit plugin and will not load any bootstrap nor route files
|
|
|
|
* `CakePlugin::load('DebugKit', array('bootstrap' => true, 'routes' => true))` will load the bootstrap.php and routes.php files
|
|
|
|
* `CakePlugin::load('DebugKit', array('bootstrap' => false, 'routes' => true))` will load routes.php file but not bootstrap.php
|
|
|
|
* `CakePlugin::load('DebugKit', array('bootstrap' => array('config1', 'config2')))` will load config1.php and config2.php files
|
|
|
|
* `CakePlugin::load('DebugKit', array('bootstrap' => 'aCallableMethod'))` will run the aCallableMethod function to initialize it
|
|
|
|
*
|
|
|
|
* Bootstrap initialization functions can be expressed as a PHP callback type, including closures. Callbacks will receive two
|
|
|
|
* parameters (plugin name, plugin configuration)
|
|
|
|
*
|
|
|
|
* It is also possible to load multiple plugins at once. Examples:
|
2011-05-16 22:01:20 +00:00
|
|
|
*
|
2015-05-14 02:34:46 +00:00
|
|
|
* `CakePlugin::load(array('DebugKit', 'ApiGenerator'))`
|
|
|
|
*
|
|
|
|
* will load the DebugKit and ApiGenerator plugins
|
|
|
|
*
|
|
|
|
* `CakePlugin::load(array('DebugKit', 'ApiGenerator'), array('bootstrap' => true))`
|
|
|
|
*
|
|
|
|
* will load bootstrap file for both plugins
|
2011-05-16 22:01:20 +00:00
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-04-17 06:07:31 +00:00
|
|
|
* CakePlugin::load(array(
|
|
|
|
* 'DebugKit' => array('routes' => true),
|
|
|
|
* 'ApiGenerator'
|
|
|
|
* ), array('bootstrap' => true))
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-05-16 22:01:20 +00:00
|
|
|
*
|
2011-04-17 06:07:31 +00:00
|
|
|
* Will only load the bootstrap for ApiGenerator and only the routes for DebugKit
|
2011-05-16 22:01:20 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $plugin name of the plugin to be loaded in CamelCase format or array or plugins to load
|
2011-04-17 06:07:31 +00:00
|
|
|
* @param array $config configuration options for the plugin
|
|
|
|
* @throws MissingPluginException if the folder for the plugin to be loaded is not found
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function load($plugin, $config = array()) {
|
|
|
|
if (is_array($plugin)) {
|
|
|
|
foreach ($plugin as $name => $conf) {
|
|
|
|
list($name, $conf) = (is_numeric($name)) ? array($conf, $config) : array($name, $conf);
|
2011-04-26 05:26:19 +00:00
|
|
|
self::load($name, $conf);
|
2011-04-17 06:07:31 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2013-01-08 18:52:47 +00:00
|
|
|
$config += array('bootstrap' => false, 'routes' => false, 'ignoreMissing' => false);
|
2011-04-26 05:26:19 +00:00
|
|
|
if (empty($config['path'])) {
|
2011-04-17 06:07:31 +00:00
|
|
|
foreach (App::path('plugins') as $path) {
|
2011-04-29 17:19:46 +00:00
|
|
|
if (is_dir($path . $plugin)) {
|
|
|
|
self::$_plugins[$plugin] = $config + array('path' => $path . $plugin . DS);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Backwards compatibility to make easier to migrate to 2.0
|
|
|
|
$underscored = Inflector::underscore($plugin);
|
2011-04-17 06:07:31 +00:00
|
|
|
if (is_dir($path . $underscored)) {
|
|
|
|
self::$_plugins[$plugin] = $config + array('path' => $path . $underscored . DS);
|
2011-04-29 17:19:46 +00:00
|
|
|
break;
|
2011-04-17 06:07:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2012-06-07 21:32:43 +00:00
|
|
|
self::$_plugins[$plugin] = $config;
|
2011-04-17 06:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty(self::$_plugins[$plugin]['path'])) {
|
|
|
|
throw new MissingPluginException(array('plugin' => $plugin));
|
|
|
|
}
|
|
|
|
if (!empty(self::$_plugins[$plugin]['bootstrap'])) {
|
|
|
|
self::bootstrap($plugin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-06 05:23:30 +00:00
|
|
|
/**
|
|
|
|
* Will load all the plugins located in the configured plugins folders
|
|
|
|
* If passed an options array, it will be used as a common default for all plugins to be loaded
|
|
|
|
* It is possible to set specific defaults for each plugins in the options array. Examples:
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-05-06 05:23:30 +00:00
|
|
|
* CakePlugin::loadAll(array(
|
2015-05-14 02:34:46 +00:00
|
|
|
* array('bootstrap' => true),
|
2014-04-08 10:19:29 +00:00
|
|
|
* 'DebugKit' => array('routes' => true, 'bootstrap' => false),
|
2011-05-06 05:23:30 +00:00
|
|
|
* ))
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-05-06 05:23:30 +00:00
|
|
|
*
|
2014-04-08 10:19:29 +00:00
|
|
|
* The above example will load the bootstrap file for all plugins, but for DebugKit it will only load
|
2015-05-14 02:34:46 +00:00
|
|
|
* the routes file and will not look for any bootstrap script. If you are loading
|
|
|
|
* many plugins that inconsistently support routes/bootstrap files, instead of detailing
|
|
|
|
* each plugin you can use the `ignoreMissing` option:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* CakePlugin::loadAll(array(
|
|
|
|
* 'ignoreMissing' => true,
|
|
|
|
* 'bootstrap' => true,
|
|
|
|
* 'routes' => true,
|
|
|
|
* ));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* The ignoreMissing option will do additional file_exists() calls but is simpler
|
|
|
|
* to use.
|
2011-05-06 05:23:30 +00:00
|
|
|
*
|
2014-06-02 12:57:11 +00:00
|
|
|
* @param array $options Options list. See CakePlugin::load() for valid options.
|
2011-05-06 05:23:30 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-09-21 18:01:11 +00:00
|
|
|
public static function loadAll($options = array()) {
|
2011-05-06 05:23:30 +00:00
|
|
|
$plugins = App::objects('plugins');
|
|
|
|
foreach ($plugins as $p) {
|
2014-04-08 10:22:29 +00:00
|
|
|
$opts = isset($options[$p]) ? (array)$options[$p] : array();
|
2014-04-08 10:18:17 +00:00
|
|
|
if (isset($options[0])) {
|
|
|
|
$opts += $options[0];
|
2011-07-15 05:35:53 +00:00
|
|
|
}
|
2014-04-08 10:22:29 +00:00
|
|
|
self::load($p, $opts);
|
2011-05-06 05:23:30 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-17 06:07:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the filesystem path for a plugin
|
|
|
|
*
|
|
|
|
* @param string $plugin name of the plugin in CamelCase format
|
|
|
|
* @return string path to the plugin folder
|
|
|
|
* @throws MissingPluginException if the folder for plugin was not found or plugin has not been loaded
|
|
|
|
*/
|
|
|
|
public static function path($plugin) {
|
2011-04-26 03:12:33 +00:00
|
|
|
if (empty(self::$_plugins[$plugin])) {
|
2011-04-17 06:07:31 +00:00
|
|
|
throw new MissingPluginException(array('plugin' => $plugin));
|
|
|
|
}
|
2011-04-26 03:12:33 +00:00
|
|
|
return self::$_plugins[$plugin]['path'];
|
2011-04-17 06:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the bootstrapping files for a plugin, or calls the initialization setup in the configuration
|
|
|
|
*
|
|
|
|
* @param string $plugin name of the plugin
|
|
|
|
* @return mixed
|
|
|
|
* @see CakePlugin::load() for examples of bootstrap configuration
|
|
|
|
*/
|
|
|
|
public static function bootstrap($plugin) {
|
|
|
|
$config = self::$_plugins[$plugin];
|
|
|
|
if ($config['bootstrap'] === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (is_callable($config['bootstrap'])) {
|
|
|
|
return call_user_func_array($config['bootstrap'], array($plugin, $config));
|
|
|
|
}
|
|
|
|
|
|
|
|
$path = self::path($plugin);
|
2011-04-26 05:26:19 +00:00
|
|
|
if ($config['bootstrap'] === true) {
|
2013-01-08 18:52:47 +00:00
|
|
|
return self::_includeFile(
|
|
|
|
$path . 'Config' . DS . 'bootstrap.php',
|
|
|
|
$config['ignoreMissing']
|
|
|
|
);
|
2011-04-17 06:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$bootstrap = (array)$config['bootstrap'];
|
|
|
|
foreach ($bootstrap as $file) {
|
2013-01-08 18:52:47 +00:00
|
|
|
self::_includeFile(
|
|
|
|
$path . 'Config' . DS . $file . '.php',
|
|
|
|
$config['ignoreMissing']
|
|
|
|
);
|
2011-04-17 06:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-23 02:20:56 +00:00
|
|
|
* Loads the routes file for a plugin, or all plugins configured to load their respective routes file
|
2011-04-17 06:07:31 +00:00
|
|
|
*
|
2011-05-23 02:20:56 +00:00
|
|
|
* @param string $plugin name of the plugin, if null will operate on all plugins having enabled the
|
|
|
|
* loading of routes files
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2011-04-17 06:07:31 +00:00
|
|
|
*/
|
2011-05-23 02:20:56 +00:00
|
|
|
public static function routes($plugin = null) {
|
|
|
|
if ($plugin === null) {
|
|
|
|
foreach (self::loaded() as $p) {
|
|
|
|
self::routes($p);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2011-04-17 06:07:31 +00:00
|
|
|
$config = self::$_plugins[$plugin];
|
|
|
|
if ($config['routes'] === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-08 18:52:47 +00:00
|
|
|
return (bool)self::_includeFile(
|
|
|
|
self::path($plugin) . 'Config' . DS . 'routes.php',
|
|
|
|
$config['ignoreMissing']
|
|
|
|
);
|
2011-04-17 06:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-18 17:06:29 +00:00
|
|
|
* Returns true if the plugin $plugin is already loaded
|
2011-05-09 03:49:00 +00:00
|
|
|
* If plugin is null, it will return a list of all loaded plugins
|
2011-04-17 06:07:31 +00:00
|
|
|
*
|
2014-06-02 12:57:11 +00:00
|
|
|
* @param string $plugin Plugin name to check.
|
2011-05-09 03:49:00 +00:00
|
|
|
* @return mixed boolean true if $plugin is already loaded.
|
|
|
|
* If $plugin is null, returns a list of plugins that have been loaded
|
2011-04-17 06:07:31 +00:00
|
|
|
*/
|
2011-05-09 03:49:00 +00:00
|
|
|
public static function loaded($plugin = null) {
|
|
|
|
if ($plugin) {
|
|
|
|
return isset(self::$_plugins[$plugin]);
|
|
|
|
}
|
2011-05-16 22:01:20 +00:00
|
|
|
$return = array_keys(self::$_plugins);
|
|
|
|
sort($return);
|
|
|
|
return $return;
|
2011-04-17 06:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forgets a loaded plugin or all of them if first parameter is null
|
|
|
|
*
|
|
|
|
* @param string $plugin name of the plugin to forget
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function unload($plugin = null) {
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($plugin === null) {
|
2011-04-17 06:07:31 +00:00
|
|
|
self::$_plugins = array();
|
|
|
|
} else {
|
2011-04-26 05:26:19 +00:00
|
|
|
unset(self::$_plugins[$plugin]);
|
2011-04-17 06:07:31 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-04 00:34:07 +00:00
|
|
|
|
2013-01-08 18:52:47 +00:00
|
|
|
/**
|
|
|
|
* Include file, ignoring include error if needed if file is missing
|
|
|
|
*
|
|
|
|
* @param string $file File to include
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $ignoreMissing Whether to ignore include error for missing files
|
2013-01-08 18:52:47 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected static function _includeFile($file, $ignoreMissing = false) {
|
|
|
|
if ($ignoreMissing && !is_file($file)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return include $file;
|
|
|
|
}
|
|
|
|
|
2011-05-16 22:01:20 +00:00
|
|
|
}
|