initial import
This commit is contained in:
29
app/Api/Plugins/PluginLoader.php
Normal file
29
app/Api/Plugins/PluginLoader.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Api\Plugins;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class PluginLoader
|
||||
{
|
||||
protected static $plugins = [];
|
||||
|
||||
public static function registerPlugin(string $name, string $className)
|
||||
{
|
||||
if (!class_exists($className)) {
|
||||
throw new InvalidArgumentException("Plugin class {$className} not found.");
|
||||
}
|
||||
if (!in_array(ApiPluginInterface::class, class_implements($className))) {
|
||||
throw new InvalidArgumentException("Plugin class {$className} must implement ApiPluginInterface.");
|
||||
}
|
||||
static::$plugins[$name] = $className;
|
||||
}
|
||||
|
||||
public static function getPlugin(string $name): ApiPluginInterface
|
||||
{
|
||||
if (!isset(static::$plugins[$name])) {
|
||||
throw new InvalidArgumentException("Plugin {$name} not registered.");
|
||||
}
|
||||
return new static::$plugins[$name]();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user