Class PluginCollection
Plugin Collection
Holds onto plugin objects loaded into an application, and provides methods for iterating, and finding plugins based on criteria.
This class implements the Iterator interface to allow plugins to be iterated, handling the situation where a plugin's hook method (usually bootstrap) loads another plugin during iteration.
While its implementation supported nested iteration it does not
support using continue
or break
inside loops.
Property Summary
-
$loopDepth protected
int
Loop depth
-
$names protected
array<string>
Names of plugins
-
$plugins protected
arrayCake\Core\PluginInterface>
Plugin list
-
$positions protected
array<int>
Iterator position stack.
Method Summary
-
__construct() public
Constructor
-
add() public
Add a plugin to the collection
-
clear() public
Remove all plugins from the collection
-
count() public
Implementation of Countable.
-
create() public
Create a plugin instance from a name/classname and configuration.
-
current() public
Part of Iterator Interface
-
findPath() public
Locate a plugin path by looking at configuration data.
-
get() public
Get the a plugin by name.
-
has() public
Check whether the named plugin exists in the collection.
-
key() public
Part of Iterator Interface
-
loadConfig() protected
Load the path information stored in vendor/cakephp-plugins.php
-
next() public
Part of Iterator Interface
-
remove() public
Remove a plugin from the collection if it exists.
-
rewind() public
Part of Iterator Interface
-
valid() public
Part of Iterator Interface
-
with() public
Filter the plugins to those with the named hook enabled.
Method Detail
__construct() ¶ public
__construct(arrayCake\Core\PluginInterface> $plugins = [])
Constructor
Parameters
-
arrayCake\Core\PluginInterface>
$plugins optional The map of plugins to add to the collection.
add() ¶ public
add(Cake\Core\PluginInterface $plugin): $this
Add a plugin to the collection
Plugins will be keyed by their names.
Parameters
-
Cake\Core\PluginInterface
$plugin The plugin to load.
Returns
$this
count() ¶ public
count(): int
Implementation of Countable.
Get the number of plugins in the collection.
Returns
int
create() ¶ public
create(string $name, array<string, mixed> $config = []): Cake\Core\PluginInterface
Create a plugin instance from a name/classname and configuration.
Parameters
-
string
$name The plugin name or classname
-
array<string, mixed>
$config optional Configuration options for the plugin.
Returns
Cake\Core\PluginInterface
Throws
Cake\Core\Exception\MissingPluginException
When plugin instance could not be created.
current() ¶ public
current(): Cake\Core\PluginInterface
Part of Iterator Interface
Returns
Cake\Core\PluginInterface
findPath() ¶ public
findPath(string $name): string
Locate a plugin path by looking at configuration data.
This will use the plugins
Configure key, and fallback to enumerating App::path('plugins')
This method is not part of the official public API as plugins with no plugin class are being phased out.
Parameters
-
string
$name The plugin name to locate a path for.
Returns
string
Throws
Cake\Core\Exception\MissingPluginException
when a plugin path cannot be resolved.
get() ¶ public
get(string $name): Cake\Core\PluginInterface
Get the a plugin by name.
If a plugin isn't already loaded it will be autoloaded on first access and that plugins loaded this way may miss some hook methods.
Parameters
-
string
$name The plugin to get.
Returns
Cake\Core\PluginInterface
Throws
Cake\Core\Exception\MissingPluginException
when unknown plugins are fetched.
has() ¶ public
has(string $name): bool
Check whether the named plugin exists in the collection.
Parameters
-
string
$name The named plugin.
Returns
bool
loadConfig() ¶ protected
loadConfig(): void
Load the path information stored in vendor/cakephp-plugins.php
This file is generated by the cakephp/plugin-installer package and used
to locate plugins on the filesystem as applications can use extra.plugin-paths
in their composer.json file to move plugin outside of vendor/
Returns
void
remove() ¶ public
remove(string $name): $this
Remove a plugin from the collection if it exists.
Parameters
-
string
$name The named plugin.
Returns
$this
with() ¶ public
with(string $hook): GeneratorCake\Core\PluginInterface>
Filter the plugins to those with the named hook enabled.
Parameters
-
string
$hook The hook to filter plugins by
Returns
GeneratorCake\Core\PluginInterface>
Throws
InvalidArgumentException
on invalid hooks