forked from cakephp/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
430 lines (396 loc) · 14.6 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 2.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Core;
use Cake\Core\Exception\MissingPluginException;
use DirectoryIterator;
/**
* Plugin is used to load and locate plugins.
*
* It also can retrieve plugin paths and load their bootstrap and routes files.
*
* @link https://book.cakephp.org/3.0/en/plugins.html
*/
class Plugin
{
/**
* Holds a list of all loaded plugins and their configuration
*
* @var \Cake\Core\PluginCollection|null
*/
protected static $plugins;
/**
* Class loader instance
*
* @var \Cake\Core\ClassLoader
*/
protected static $_loader;
/**
* Loads a plugin and optionally loads bootstrapping,
* routing files or runs an initialization function.
*
* Plugins only need to be loaded if you want bootstrapping/routes/cli commands to
* be exposed. If your plugin does not expose any of these features you do not need
* to load them.
*
* This method does not configure any autoloaders. That must be done separately either
* through composer, or your own code during config/bootstrap.php.
*
* ### Examples:
*
* `Plugin::load('DebugKit')`
*
* Will load the DebugKit plugin and will not load any bootstrap nor route files.
* However, the plugin will be part of the framework default routes, and have its
* CLI tools (if any) available for use.
*
* `Plugin::load('DebugKit', ['bootstrap' => true, 'routes' => true])`
*
* Will load the bootstrap.php and routes.php files.
*
* `Plugin::load('DebugKit', ['bootstrap' => false, 'routes' => true])`
*
* Will load routes.php file but not bootstrap.php
*
* `Plugin::load('FOC/Authenticate')`
*
* Will load plugin from `plugins/FOC/Authenticate`.
*
* It is also possible to load multiple plugins at once. Examples:
*
* `Plugin::load(['DebugKit', 'ApiGenerator'])`
*
* Will load the DebugKit and ApiGenerator plugins.
*
* `Plugin::load(['DebugKit', 'ApiGenerator'], ['bootstrap' => true])`
*
* Will load bootstrap file for both plugins
*
* ```
* Plugin::load([
* 'DebugKit' => ['routes' => true],
* 'ApiGenerator'
* ],
* ['bootstrap' => true])
* ```
*
* Will only load the bootstrap for ApiGenerator and only the routes for DebugKit
*
* ### Configuration options
*
* - `bootstrap` - array - Whether or not you want the $plugin/config/bootstrap.php file loaded.
* - `routes` - boolean - Whether or not you want to load the $plugin/config/routes.php file.
* - `ignoreMissing` - boolean - Set to true to ignore missing bootstrap/routes files.
* - `path` - string - The path the plugin can be found on. If empty the default plugin path (App.pluginPaths) will be used.
* - `classBase` - The path relative to `path` which contains the folders with class files.
* Defaults to "src".
* - `autoload` - boolean - Whether or not you want an autoloader registered. This defaults to false. The framework
* assumes you have configured autoloaders using composer. However, if your application source tree is made up of
* plugins, this can be a useful option.
*
* @param string|array $plugin name of the plugin to be loaded in CamelCase format or array or plugins to load
* @param array $config configuration options for the plugin
* @throws \Cake\Core\Exception\MissingPluginException if the folder for the plugin to be loaded is not found
* @return void
* @deprecated 3.7.0 This method will be removed in 4.0.0. Use Application::addPlugin() instead.
*/
public static function load($plugin, array $config = [])
{
deprecationWarning(
'Plugin::load() is deprecated. ' .
'Use Application::addPlugin() instead. ' .
'This method will be removed in 4.0.0.'
);
if (is_array($plugin)) {
foreach ($plugin as $name => $conf) {
list($name, $conf) = is_numeric($name) ? [$conf, $config] : [$name, $conf];
static::load($name, $conf);
}
return;
}
$config += [
'autoload' => false,
'bootstrap' => false,
'routes' => false,
'console' => true,
'classBase' => 'src',
'ignoreMissing' => false,
'name' => $plugin
];
if (!isset($config['path'])) {
$config['path'] = static::getCollection()->findPath($plugin);
}
$config['classPath'] = $config['path'] . $config['classBase'] . DIRECTORY_SEPARATOR;
if (!isset($config['configPath'])) {
$config['configPath'] = $config['path'] . 'config' . DIRECTORY_SEPARATOR;
}
$pluginClass = str_replace('/', '\\', $plugin) . '\\Plugin';
if (class_exists($pluginClass)) {
$instance = new $pluginClass($config);
} else {
// Use stub plugin as this method will be removed long term.
$instance = new BasePlugin($config);
}
static::getCollection()->add($instance);
if ($config['autoload'] === true) {
if (empty(static::$_loader)) {
static::$_loader = new ClassLoader();
static::$_loader->register();
}
static::$_loader->addNamespace(
str_replace('/', '\\', $plugin),
$config['path'] . $config['classBase'] . DIRECTORY_SEPARATOR
);
static::$_loader->addNamespace(
str_replace('/', '\\', $plugin) . '\Test',
$config['path'] . 'tests' . DIRECTORY_SEPARATOR
);
}
if ($config['bootstrap'] === true) {
static::bootstrap($plugin);
}
}
/**
* Will load all the plugins located in the default plugin folder.
*
* 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:
*
* ```
* Plugin::loadAll([
* ['bootstrap' => true],
* 'DebugKit' => ['routes' => true],
* ]);
* ```
*
* The above example will load the bootstrap file for all plugins, but for DebugKit it will only load the routes file
* and will not look for any bootstrap script.
*
* If a plugin has been loaded already, it will not be reloaded by loadAll().
*
* @param array $options Options.
* @return void
* @throws \Cake\Core\Exception\MissingPluginException
* @deprecated 3.7.0 This method will be removed in 4.0.0.
*/
public static function loadAll(array $options = [])
{
$plugins = [];
foreach (App::path('Plugin') as $path) {
if (!is_dir($path)) {
continue;
}
$dir = new DirectoryIterator($path);
foreach ($dir as $dirPath) {
if ($dirPath->isDir() && !$dirPath->isDot()) {
$plugins[] = $dirPath->getBasename();
}
}
}
if (Configure::check('plugins')) {
$plugins = array_merge($plugins, array_keys(Configure::read('plugins')));
$plugins = array_unique($plugins);
}
$collection = static::getCollection();
foreach ($plugins as $p) {
$opts = isset($options[$p]) ? $options[$p] : null;
if ($opts === null && isset($options[0])) {
$opts = $options[0];
}
if ($collection->has($p)) {
continue;
}
static::load($p, (array)$opts);
}
}
/**
* Returns the filesystem path for a plugin
*
* @param string $name name of the plugin in CamelCase format
* @return string path to the plugin folder
* @throws \Cake\Core\Exception\MissingPluginException if the folder for plugin was not found or plugin has not been loaded
*/
public static function path($name)
{
$plugin = static::getCollection()->get($name);
return $plugin->getPath();
}
/**
* Returns the filesystem path for plugin's folder containing class folders.
*
* @param string $name name of the plugin in CamelCase format.
* @return string Path to the plugin folder container class folders.
* @throws \Cake\Core\Exception\MissingPluginException If plugin has not been loaded.
*/
public static function classPath($name)
{
$plugin = static::getCollection()->get($name);
return $plugin->getClassPath();
}
/**
* Returns the filesystem path for plugin's folder containing config files.
*
* @param string $name name of the plugin in CamelCase format.
* @return string Path to the plugin folder container config files.
* @throws \Cake\Core\Exception\MissingPluginException If plugin has not been loaded.
*/
public static function configPath($name)
{
$plugin = static::getCollection()->get($name);
return $plugin->getConfigPath();
}
/**
* Loads the bootstrapping files for a plugin, or calls the initialization setup in the configuration
*
* @param string $name name of the plugin
* @return mixed
* @see \Cake\Core\Plugin::load() for examples of bootstrap configuration
* @deprecated 3.7.0 This method will be removed in 4.0.0.
*/
public static function bootstrap($name)
{
deprecationWarning(
'Plugin::bootstrap() is deprecated. ' .
'This method will be removed in 4.0.0.'
);
$plugin = static::getCollection()->get($name);
if (!$plugin->isEnabled('bootstrap')) {
return false;
}
// Disable bootstrapping for this plugin as it will have
// been bootstrapped.
$plugin->disable('bootstrap');
return static::_includeFile(
$plugin->getConfigPath() . 'bootstrap.php',
true
);
}
/**
* Loads the routes file for a plugin, or all plugins configured to load their respective routes file.
*
* If you need fine grained control over how routes are loaded for plugins, you
* can use {@see Cake\Routing\RouteBuilder::loadPlugin()}
*
* @param string|null $name name of the plugin, if null will operate on all
* plugins having enabled the loading of routes files.
* @return bool
* @deprecated 3.6.5 This method is no longer needed when using HttpApplicationInterface based applications.
* This method will be removed in 4.0.0
*/
public static function routes($name = null)
{
deprecationWarning(
'You no longer need to call `Plugin::routes()` after upgrading to use Http\Server. ' .
'See https://book.cakephp.org/3.0/en/development/application.html#adding-the-new-http-stack-to-an-existing-application ' .
'for upgrade information.'
);
if ($name === null) {
foreach (static::loaded() as $p) {
static::routes($p);
}
return true;
}
$plugin = static::getCollection()->get($name);
if (!$plugin->isEnabled('routes')) {
return false;
}
return (bool)static::_includeFile(
$plugin->getConfigPath() . 'routes.php',
true
);
}
/**
* Check whether or not a plugin is loaded.
*
* @param string $plugin The name of the plugin to check.
* @return bool
* @since 3.7.0
*/
public static function isLoaded($plugin)
{
return static::getCollection()->has($plugin);
}
/**
* Return a list of loaded plugins.
*
* If a plugin name is provided, the return value will be a bool
* indicating whether or not the named plugin is loaded. This usage
* is deprecated. Instead you should use Plugin::isLoaded($name)
*
* @param string|null $plugin Plugin name.
* @return bool|array Boolean true if $plugin is already loaded.
* If $plugin is null, returns a list of plugins that have been loaded
*/
public static function loaded($plugin = null)
{
if ($plugin !== null) {
deprecationWarning(
'Checking a single plugin with Plugin::loaded() is deprecated. ' .
'Use Plugin::isLoaded() instead.'
);
return static::getCollection()->has($plugin);
}
$names = [];
foreach (static::getCollection() as $plugin) {
$names[] = $plugin->getName();
}
sort($names);
return $names;
}
/**
* Forgets a loaded plugin or all of them if first parameter is null
*
* @param string|null $plugin name of the plugin to forget
* @deprecated 3.7.0 This method will be removed in 4.0.0. Use PluginCollection::remove() or clear() instead.
* @return void
*/
public static function unload($plugin = null)
{
deprecationWarning('Plugin::unload() will be removed in 4.0. Use PluginCollection::remove() or clear()');
if ($plugin === null) {
static::getCollection()->clear();
} else {
static::getCollection()->remove($plugin);
}
}
/**
* Include file, ignoring include error if needed if file is missing
*
* @param string $file File to include
* @param bool $ignoreMissing Whether to ignore include error for missing files
* @return mixed
*/
protected static function _includeFile($file, $ignoreMissing = false)
{
if ($ignoreMissing && !is_file($file)) {
return false;
}
return include $file;
}
/**
* Get the shared plugin collection.
*
* This method should generally not be used during application
* runtime as plugins should be set during Application startup.
*
* @return \Cake\Core\PluginCollection
*/
public static function getCollection()
{
if (!isset(static::$plugins)) {
static::$plugins = new PluginCollection();
}
return static::$plugins;
}
}