30 lines
936 B
PHP
30 lines
936 B
PHP
<?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, $apiProvider = null): ApiPluginInterface
|
|
{
|
|
if (!isset(static::$plugins[$name])) {
|
|
throw new InvalidArgumentException("Plugin {$name} not registered.");
|
|
}
|
|
$className = static::$plugins[$name];
|
|
return new $className($apiProvider);
|
|
}
|
|
} |