<?php

declare(strict_types=1);

namespace apexl\Io;

use DI\Container;


/**
 * Get the value of an environment variable.
 *
 * @param string $key The name of the environment variable.
 * @param mixed $default The default value to return if the variable is not set.
 * @return mixed The value of the environment variable or the default value.
 */
function env(string $key, mixed $default = null): mixed
{
    return $_ENV[$key] ?? $default;
}


function container(): Container
{
    global $container;

    return $container;
}

/**
 * @template T of object
 * @param class-string<T> $shouldBeInstanceOf
 * @phpstan-assert class-string<T> $candidate
 */
function assertInstanceOf(string|object $candidate, string $shouldBeInstanceOf, string $message = ''): void
{
    if (is_object($candidate)) {
        $candidate = $candidate::class;
    }
    assert(
        $candidate === $shouldBeInstanceOf || is_subclass_of($candidate, $shouldBeInstanceOf),
        $message ?: sprintf(
            '%s must implement / extend %s.',
            $candidate,
            $shouldBeInstanceOf
        ),
    );
}
