#!/usr/bin/env php
<?php
declare(strict_types=1);

use Loxya\Config\Config;
use Loxya\Console\App;
use Symfony\Component\Console\Input\ArgvInput;

if (!in_array(PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
    echo 'Caution: The console must only be used with the CLI version of PHP.' . PHP_EOL;
}

set_time_limit(0);

require  __DIR__ . '/../vendors/autoload.php';
require  __DIR__ . '/../src/App/Config/constants.php';
require  __DIR__ . '/../src/App/Config/functions.php';

$input = new ArgvInput();
if (null !== $env = $input->getParameterOption(['--env', '-e'], null, true)) {
    putenv('APP_ENV=' . $_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $env);
}

// - Chargement de l'environnement.
$envFiles = ['.env'];
if (Config::getEnv(true) === 'test') {
    $envFiles[] = '.env.test';
}
$dotenv = Dotenv\Dotenv::createImmutable(ROOT_FOLDER, $envFiles, false);
$dotenv->safeLoad();

// - Let's go !
(new App)->run($input);
