diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:30:08 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-13 11:30:08 +0000 |
commit | 4ce65d59ca91871cfd126497158200a818720bce (patch) | |
tree | e277def01fc7eba7dbc21c4a4ae5576e8aa2cf1f /vendor/ipl/stdlib/src/Plugins.php | |
parent | Initial commit. (diff) | |
download | icinga-php-library-4ce65d59ca91871cfd126497158200a818720bce.tar.xz icinga-php-library-4ce65d59ca91871cfd126497158200a818720bce.zip |
Adding upstream version 0.13.1.upstream/0.13.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/ipl/stdlib/src/Plugins.php')
-rw-r--r-- | vendor/ipl/stdlib/src/Plugins.php | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/vendor/ipl/stdlib/src/Plugins.php b/vendor/ipl/stdlib/src/Plugins.php new file mode 100644 index 0000000..a5dbb77 --- /dev/null +++ b/vendor/ipl/stdlib/src/Plugins.php @@ -0,0 +1,95 @@ +<?php + +namespace ipl\Stdlib; + +use ipl\Stdlib\Contract\PluginLoader; +use ipl\Stdlib\Loader\AutoloadingPluginLoader; + +trait Plugins +{ + /** @var array Registered plugin loaders by type */ + protected $pluginLoaders = []; + + /** + * Factory for plugin loaders + * + * @param PluginLoader|string $loaderOrNamespace + * @param string $postfix + * + * @return PluginLoader + */ + public static function wantPluginLoader($loaderOrNamespace, $postfix = '') + { + if ($loaderOrNamespace instanceof PluginLoader) { + $loader = $loaderOrNamespace; + } else { + $loader = new AutoloadingPluginLoader($loaderOrNamespace, $postfix); + } + + return $loader; + } + + /** + * Get whether a plugin loader for the given type exists + * + * @param string $type + * + * @return bool + */ + public function hasPluginLoader($type) + { + return isset($this->pluginLoaders[$type]); + } + + /** + * Add a plugin loader for the given type + * + * @param string $type + * @param PluginLoader|string $loaderOrNamespace + * @param string $postfix + * + * @return $this + */ + public function addPluginLoader($type, $loaderOrNamespace, $postfix = '') + { + $loader = static::wantPluginLoader($loaderOrNamespace, $postfix); + + if (! isset($this->pluginLoaders[$type])) { + $this->pluginLoaders[$type] = []; + } + + array_unshift($this->pluginLoaders[$type], $loader); + + return $this; + } + + /** + * Load the class file of the given plugin + * + * @param string $type + * @param string $name + * + * @return string|false + */ + public function loadPlugin($type, $name) + { + if ($this->hasPluginLoader($type)) { + /** @var PluginLoader $loader */ + foreach ($this->pluginLoaders[$type] as $loader) { + $class = $loader->load($name); + if ($class) { + return $class; + } + } + } + + return false; + } + + protected function addDefaultPluginLoader($type, $loaderOrNamespace, $postfix) + { + $this->pluginLoaders[$type][] = static::wantPluginLoader($loaderOrNamespace, $postfix); + + return $this; + } +} |