<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\DI;

use PpPhp\Core\Platform\DI\Contracts\ServiceProviderInterface;

final class ContainerBuilder
{
    private Container $container;
    private array $providers = [];

    public function __construct(?Container $container = null)
    {
        $this->container = $container ?? new Container();
    }

    public function container(): Container { return $this->container; }
    public function addProvider(ServiceProviderInterface $provider): self { $this->providers[] = $provider; return $this; }

    public function build(): Container
    {
        foreach ($this->providers as $provider) { $provider->register($this->container); }
        foreach ($this->providers as $provider) { $provider->boot($this->container); }
        return $this->container;
    }
}
