57 lines
1.5 KiB
PHP
57 lines
1.5 KiB
PHP
|
|
<?php
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Phred\Support;
|
||
|
|
|
||
|
|
use DI\Container;
|
||
|
|
use DI\ContainerBuilder;
|
||
|
|
use Phred\Support\Contracts\ConfigInterface;
|
||
|
|
use Phred\Support\Contracts\ServiceProviderInterface;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Loads and executes service providers in deterministic order.
|
||
|
|
* Order: core → app → modules
|
||
|
|
*/
|
||
|
|
final class ProviderRepository
|
||
|
|
{
|
||
|
|
/** @var list<ServiceProviderInterface> */
|
||
|
|
private array $providers = [];
|
||
|
|
|
||
|
|
public function __construct(private readonly ConfigInterface $config)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
public function load(): void
|
||
|
|
{
|
||
|
|
$this->providers = [];
|
||
|
|
$core = (array) Config::get('providers.core', []);
|
||
|
|
$app = (array) Config::get('providers.app', []);
|
||
|
|
$modules = (array) Config::get('providers.modules', []);
|
||
|
|
|
||
|
|
foreach ([$core, $app, $modules] as $group) {
|
||
|
|
foreach ($group as $class) {
|
||
|
|
if (is_string($class) && class_exists($class)) {
|
||
|
|
$instance = new $class();
|
||
|
|
if ($instance instanceof ServiceProviderInterface) {
|
||
|
|
$this->providers[] = $instance;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function registerAll(ContainerBuilder $builder): void
|
||
|
|
{
|
||
|
|
foreach ($this->providers as $provider) {
|
||
|
|
$provider->register($builder, $this->config);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function bootAll(Container $container): void
|
||
|
|
{
|
||
|
|
foreach ($this->providers as $provider) {
|
||
|
|
$provider->boot($container);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|