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.

Namespace: Cake\Core

Properties summary

  • $loopDepth protected
    int

    Loop depth

  • $names protected
    string[]

    Names of plugins

  • $plugins protected
    \Cake\Core\PluginInterface[]

    Plugin list

  • $positions protected
    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(array $plugins)

Constructor

Parameters

\Cake\Core\PluginInterface[] $plugins optional

The map of plugins to add to the collection.

add() public

add(\Cake\Core\PluginInterface $plugin)

Add a plugin to the collection

Plugins will be keyed by their names.

Parameters

\Cake\Core\PluginInterface $plugin

The plugin to load.

Returns

$this

clear() public

clear()

Remove all plugins from the collection

Returns

$this

count() public

count()

Implementation of Countable.

Get the number of plugins in the collection.

Returns

int

create() public

create(string $name, array $config)

Create a plugin instance from a name/classname and configuration.

Parameters

string $name

The plugin name or classname

array $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()

Part of Iterator Interface

Returns

\Cake\Core\PluginInterface

findPath() public

findPath(string $name)

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. Will return '' when a plugin cannot be found.

Returns

string

Throws

Cake\Core\Exception\MissingPluginException
when a plugin path cannot be resolved.

get() public

get(string $name)

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

The plugin.

Throws

Cake\Core\Exception\MissingPluginException
when unknown plugins are fetched.

has() public

has(string $name)

Check whether the named plugin exists in the collection.

Parameters

string $name

The named plugin.

Returns

bool

key() public

key()

Part of Iterator Interface

Returns

string

loadConfig() protected

loadConfig()

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/

next() public

next()

Part of Iterator Interface

remove() public

remove(string $name)

Remove a plugin from the collection if it exists.

Parameters

string $name

The named plugin.

Returns

$this

rewind() public

rewind()

Part of Iterator Interface

valid() public

valid()

Part of Iterator Interface

Returns

bool

with() public

with(string $hook)

Filter the plugins to those with the named hook enabled.

Parameters

string $hook

The hook to filter plugins by

Returns

\Generator&\Cake\Core\PluginInterface[]

A generator containing matching plugins.

Throws

InvalidArgumentException
on invalid hooks

Property Detail

$loopDepth protected

Loop depth

Type

int

$names protected

Names of plugins

Type

string[]

$plugins protected

Plugin list

Type

\Cake\Core\PluginInterface[]

$positions protected

Iterator position stack.

Type

int[]

© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.0/class-Cake.Core.PluginCollection.html