<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\DI\Resolver;

use PpPhp\Core\Platform\DI\Exceptions\CircularDependencyException;

final class ResolutionStack
{
    private array $stack = [];

    public function push(string $id): void
    {
        if (in_array($id, $this->stack, true)) {
            $path = implode(' -> ', [...$this->stack, $id]);
            throw new CircularDependencyException('Circular dependency detected: ' . $path);
        }

        $this->stack[] = $id;
    }

    public function pop(): void
    {
        array_pop($this->stack);
    }

    public function all(): array
    {
        return $this->stack;
    }
}
