Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 1
<?php
2
/**
3
 * CakePlugin class
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://cakephp.org CakePHP(tm) Project
14
 * @package       Cake.Core
15
 * @since         CakePHP(tm) v 2.0.0
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
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
/**
29
 * Holds a list of all loaded plugins and their configuration
30
 *
31
 * @var array
32
 */
33
	protected static $_plugins = array();
34
 
35
/**
36
 * Loads a plugin and optionally loads bootstrapping, routing files or loads an initialization function
37
 *
38
 * Examples:
39
 *
40
 * 	`CakePlugin::load('DebugKit')` will load the DebugKit plugin and will not load any bootstrap nor route files
41
 *	`CakePlugin::load('DebugKit', array('bootstrap' => true, 'routes' => true))` will load the bootstrap.php and routes.php files
42
 * 	`CakePlugin::load('DebugKit', array('bootstrap' => false, 'routes' => true))` will load routes.php file but not bootstrap.php
43
 * 	`CakePlugin::load('DebugKit', array('bootstrap' => array('config1', 'config2')))` will load config1.php and config2.php files
44
 *	`CakePlugin::load('DebugKit', array('bootstrap' => 'aCallableMethod'))` will run the aCallableMethod function to initialize it
45
 *
46
 * Bootstrap initialization functions can be expressed as a PHP callback type, including closures. Callbacks will receive two
47
 * parameters (plugin name, plugin configuration)
48
 *
49
 * It is also possible to load multiple plugins at once. Examples:
50
 *
51
 * `CakePlugin::load(array('DebugKit', 'ApiGenerator'))`
52
 *
53
 * will load the DebugKit and ApiGenerator plugins
54
 *
55
 * `CakePlugin::load(array('DebugKit', 'ApiGenerator'), array('bootstrap' => true))`
56
 *
57
 * will load bootstrap file for both plugins
58
 *
59
 * ```
60
 * 	CakePlugin::load(array(
61
 * 		'DebugKit' => array('routes' => true),
62
 * 		'ApiGenerator'
63
 * 		), array('bootstrap' => true))
64
 * ```
65
 *
66
 * Will only load the bootstrap for ApiGenerator and only the routes for DebugKit
67
 *
68
 * @param string|array $plugin name of the plugin to be loaded in CamelCase format or array or plugins to load
69
 * @param array $config configuration options for the plugin
70
 * @throws MissingPluginException if the folder for the plugin to be loaded is not found
71
 * @return void
72
 */
73
	public static function load($plugin, $config = array()) {
74
		if (is_array($plugin)) {
75
			foreach ($plugin as $name => $conf) {
76
				list($name, $conf) = (is_numeric($name)) ? array($conf, $config) : array($name, $conf);
77
				self::load($name, $conf);
78
			}
79
			return;
80
		}
81
		$config += array('bootstrap' => false, 'routes' => false, 'ignoreMissing' => false);
82
		if (empty($config['path'])) {
83
			foreach (App::path('plugins') as $path) {
84
				if (is_dir($path . $plugin)) {
85
					self::$_plugins[$plugin] = $config + array('path' => $path . $plugin . DS);
86
					break;
87
				}
88
 
89
				//Backwards compatibility to make easier to migrate to 2.0
90
				$underscored = Inflector::underscore($plugin);
91
				if (is_dir($path . $underscored)) {
92
					self::$_plugins[$plugin] = $config + array('path' => $path . $underscored . DS);
93
					break;
94
				}
95
			}
96
		} else {
97
			self::$_plugins[$plugin] = $config;
98
		}
99
 
100
		if (empty(self::$_plugins[$plugin]['path'])) {
101
			throw new MissingPluginException(array('plugin' => $plugin));
102
		}
103
		if (!empty(self::$_plugins[$plugin]['bootstrap'])) {
104
			self::bootstrap($plugin);
105
		}
106
	}
107
 
108
/**
109
 * Will load all the plugins located in the configured plugins folders
110
 * If passed an options array, it will be used as a common default for all plugins to be loaded
111
 * It is possible to set specific defaults for each plugins in the options array. Examples:
112
 *
113
 * ```
114
 * 	CakePlugin::loadAll(array(
115
 * 		array('bootstrap' => true),
116
 * 		'DebugKit' => array('routes' => true, 'bootstrap' => false),
117
 * 	))
118
 * ```
119
 *
120
 * The above example will load the bootstrap file for all plugins, but for DebugKit it will only load
121
 * the routes file and will not look for any bootstrap script. If you are loading
122
 * many plugins that inconsistently support routes/bootstrap files, instead of detailing
123
 * each plugin you can use the `ignoreMissing` option:
124
 *
125
 * ```
126
 * CakePlugin::loadAll(array(
127
 *   'ignoreMissing' => true,
128
 *   'bootstrap' => true,
129
 *   'routes' => true,
130
 * ));
131
 * ```
132
 *
133
 * The ignoreMissing option will do additional file_exists() calls but is simpler
134
 * to use.
135
 *
136
 * @param array $options Options list. See CakePlugin::load() for valid options.
137
 * @return void
138
 */
139
	public static function loadAll($options = array()) {
140
		$plugins = App::objects('plugins');
141
		foreach ($plugins as $p) {
142
			$opts = isset($options[$p]) ? (array)$options[$p] : array();
143
			if (isset($options[0])) {
144
				$opts += $options[0];
145
			}
146
			self::load($p, $opts);
147
		}
148
	}
149
 
150
/**
151
 * Returns the filesystem path for a plugin
152
 *
153
 * @param string $plugin name of the plugin in CamelCase format
154
 * @return string path to the plugin folder
155
 * @throws MissingPluginException if the folder for plugin was not found or plugin has not been loaded
156
 */
157
	public static function path($plugin) {
158
		if (empty(self::$_plugins[$plugin])) {
159
			throw new MissingPluginException(array('plugin' => $plugin));
160
		}
161
		return self::$_plugins[$plugin]['path'];
162
	}
163
 
164
/**
165
 * Loads the bootstrapping files for a plugin, or calls the initialization setup in the configuration
166
 *
167
 * @param string $plugin name of the plugin
168
 * @return mixed
169
 * @see CakePlugin::load() for examples of bootstrap configuration
170
 */
171
	public static function bootstrap($plugin) {
172
		$config = self::$_plugins[$plugin];
173
		if ($config['bootstrap'] === false) {
174
			return false;
175
		}
176
		if (is_callable($config['bootstrap'])) {
177
			return call_user_func_array($config['bootstrap'], array($plugin, $config));
178
		}
179
 
180
		$path = self::path($plugin);
181
		if ($config['bootstrap'] === true) {
182
			return self::_includeFile(
183
				$path . 'Config' . DS . 'bootstrap.php',
184
				$config['ignoreMissing']
185
			);
186
		}
187
 
188
		$bootstrap = (array)$config['bootstrap'];
189
		foreach ($bootstrap as $file) {
190
			self::_includeFile(
191
				$path . 'Config' . DS . $file . '.php',
192
				$config['ignoreMissing']
193
			);
194
		}
195
 
196
		return true;
197
	}
198
 
199
/**
200
 * Loads the routes file for a plugin, or all plugins configured to load their respective routes file
201
 *
202
 * @param string $plugin name of the plugin, if null will operate on all plugins having enabled the
203
 * loading of routes files
204
 * @return bool
205
 */
206
	public static function routes($plugin = null) {
207
		if ($plugin === null) {
208
			foreach (self::loaded() as $p) {
209
				self::routes($p);
210
			}
211
			return true;
212
		}
213
		$config = self::$_plugins[$plugin];
214
		if ($config['routes'] === false) {
215
			return false;
216
		}
217
		return (bool)self::_includeFile(
218
			self::path($plugin) . 'Config' . DS . 'routes.php',
219
			$config['ignoreMissing']
220
		);
221
	}
222
 
223
/**
224
 * Returns true if the plugin $plugin is already loaded
225
 * If plugin is null, it will return a list of all loaded plugins
226
 *
227
 * @param string $plugin Plugin name to check.
228
 * @return mixed boolean true if $plugin is already loaded.
229
 * If $plugin is null, returns a list of plugins that have been loaded
230
 */
231
	public static function loaded($plugin = null) {
232
		if ($plugin) {
233
			return isset(self::$_plugins[$plugin]);
234
		}
235
		$return = array_keys(self::$_plugins);
236
		sort($return);
237
		return $return;
238
	}
239
 
240
/**
241
 * Forgets a loaded plugin or all of them if first parameter is null
242
 *
243
 * @param string $plugin name of the plugin to forget
244
 * @return void
245
 */
246
	public static function unload($plugin = null) {
247
		if ($plugin === null) {
248
			self::$_plugins = array();
249
		} else {
250
			unset(self::$_plugins[$plugin]);
251
		}
252
	}
253
 
254
/**
255
 * Include file, ignoring include error if needed if file is missing
256
 *
257
 * @param string $file File to include
258
 * @param bool $ignoreMissing Whether to ignore include error for missing files
259
 * @return mixed
260
 */
261
	protected static function _includeFile($file, $ignoreMissing = false) {
262
		if ($ignoreMissing && !is_file($file)) {
263
			return false;
264
		}
265
		return include $file;
266
	}
267
 
268
}