0 follower

Final Class Yiisoft\Test\Support\Container\SimpleContainer

InheritanceYiisoft\Test\Support\Container\SimpleContainer
ImplementsPsr\Container\ContainerInterface

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( array $definitions = [], Closure|null $factory null, Closure|null $hasCallback null )
$definitions array
$factory Closure|null

Should be closure that works like ContainerInterface::get(string $id): mixed

$hasCallback Closure|null

Should be closure that works like ContainerInterface::has(string $id): bool

                public function __construct(
    private array $definitions = [],
    ?Closure $factory = null,
    ?Closure $hasCallback = null
) {
    $this->factory = $factory ??
        /** @return mixed */
        static function (string $id) {
            throw new NotFoundException($id);
        };
    $this->hasCallback = $hasCallback ??
        function (string $id): bool {
            try {
                $this->get($id);
                return true;
            } catch (NotFoundException) {
                return false;
            }
        };
}

            
get() public method

public mixed get ( mixed $id )
$id mixed

                public function get($id)
{
    if (!array_key_exists($id, $this->definitions)) {
        $this->definitions[$id] = ($this->factory)($id);
    }
    return $this->definitions[$id];
}

            
has() public method

public boolean has ( mixed $id )
$id mixed

                public function has($id): bool
{
    if (array_key_exists($id, $this->definitions)) {
        return true;
    }
    return ($this->hasCallback)($id);
}