<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\DI\Scope;

use PpPhp\Core\Platform\DI\Contracts\ScopeInterface;
use PpPhp\Core\Platform\DI\Contracts\ScopeManagerInterface;

final class ScopeManager implements ScopeManagerInterface
{
    private array $scopes = [];
    private string $current = 'default';

    public function current(): ScopeInterface { return $this->get($this->current); }
    public function get(string $id): ScopeInterface
    {
        if (!isset($this->scopes[$id])) { $this->scopes[$id] = new Scope($id); }
        return $this->scopes[$id];
    }
    public function use(string $id): ScopeInterface { $this->current = $id; return $this->get($id); }
    public function clear(?string $id = null): void
    {
        if ($id !== null) { $this->get($id)->clear(); return; }
        foreach ($this->scopes as $scope) { $scope->clear(); }
    }
}
