Initial Version
This commit is contained in:
89
core/lib/Resource/ProviderManager.php
Normal file
89
core/lib/Resource/ProviderManager.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Resource;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use KTXF\Resource\Provider\ProviderInterface;
|
||||
|
||||
/**
|
||||
* Provider Registry
|
||||
*
|
||||
* Manages registration and resolution of authentication providers.
|
||||
*/
|
||||
class ProviderManager
|
||||
{
|
||||
private array $registeredProviders = [];
|
||||
private array $resolvedProviders = [];
|
||||
|
||||
public function __construct(
|
||||
private readonly ContainerInterface $container
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Register an authentication provider (called from module boot)
|
||||
*
|
||||
* @param string $type Provider type (e.g., 'authentication', 'storage', 'notification')
|
||||
* @param string $identifier Provider ID (e.g., 'default', 'oidc', 'totp')
|
||||
* @param string $class Fully qualified class name
|
||||
*/
|
||||
public function register(string $type, string $identifier, string $class): void
|
||||
{
|
||||
$this->registeredProviders[$type][$identifier] = $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister a provider
|
||||
*/
|
||||
public function unregister(string $type, string $identifier): void
|
||||
{
|
||||
unset($this->registeredProviders[$type][$identifier]);
|
||||
unset($this->resolvedProviders[$type][$identifier]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a provider by ID
|
||||
*/
|
||||
public function resolve(string $type, string $identifier): ?ProviderInterface
|
||||
{
|
||||
if (isset($this->resolvedProviders[$type][$identifier])) {
|
||||
return $this->resolvedProviders[$type][$identifier];
|
||||
}
|
||||
|
||||
if (!isset($this->registeredProviders[$type][$identifier])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$provider = $this->container->get($this->registeredProviders[$type][$identifier]);
|
||||
$this->resolvedProviders[$type][$identifier] = $provider;
|
||||
return $provider;
|
||||
} catch (\Exception $e) {
|
||||
error_log("Failed to resolve provider {$identifier}: " . $e->getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve multiple providers
|
||||
*
|
||||
* @param array|null $filter Optional list of provider IDs to return
|
||||
* @return array<string, ProviderInterface>
|
||||
*/
|
||||
public function providers(string $type, ?array $filter = null): array
|
||||
{
|
||||
$requestedProviders = $filter ?? array_keys($this->registeredProviders[$type] ?? []);
|
||||
$result = [];
|
||||
|
||||
foreach ($requestedProviders as $identifier) {
|
||||
$provider = $this->resolve($type, $identifier);
|
||||
if ($provider !== null) {
|
||||
$result[$identifier] = $provider;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user