diff --git a/CHANGELOG.md b/CHANGELOG.md index 7003239b3..5781f9313 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ 1. [](#new) * Added `Medium::attribute()` to pass arbitrary attributes [#3065](https://github.com/getgrav/grav/pull/3065) + * Added `Plugins::getPlugins()` and `Plugins::getPlugin($name)` to make it easier to access plugin instances [#2277](https://github.com/getgrav/grav/pull/2277) 1. [](#improved) * Added abstract `FlexObject`, `FlexCollection` and `FlexIndex` classes to `\Grav\Common\Flex` namespace (extend those instead of Framework or Generic classes) * Updated bundled `composer.phar` binary to latest version `2.0.9` diff --git a/system/src/Grav/Common/Plugins.php b/system/src/Grav/Common/Plugins.php index 5c80dfb9c..f1d7b8e66 100644 --- a/system/src/Grav/Common/Plugins.php +++ b/system/src/Grav/Common/Plugins.php @@ -170,6 +170,33 @@ class Plugins extends Iterator return $array; } + /** + * @return Plugin[] Index of all plugins by plugin name. + */ + public static function getPlugins(): array + { + $grav = Grav::instance(); + $plugins = $grav['plugins']; + + $list = []; + foreach ($plugins as $instance) { + $list[$instance->name] = $instance; + } + + return $list; + } + + /** + * @param string $name Plugin name + * @return Plugin|null Plugin object or null if plugin cannot be found. + */ + public static function getPlugin(string $name) + { + $list = static::getPlugins(); + + return $list[$name] ?? null; + } + /** * Return list of all plugin data with their blueprints. *