#!/usr/bin/env php
<?php

declare(strict_types=1);

const BASE_DIR = __DIR__.'/../../../../';

require BASE_DIR.'/vendor/autoload.php';

use apexl\Io\Client;
use apexl\Io\collections\CommandCollection;
use apexl\Io\includes\Hook;
use apexl\Io\includes\System;
use Symfony\Component\Console\Application as ConsoleApplication;

$ioApp = Client::getInstance();

//Set the configuration directory to use and load the primary config file
$ioConfigDir = BASE_DIR.'/config';
$ioApp->setConfigDir($ioConfigDir);
$ioApp->setBasePath(BASE_DIR.'/web');
$ioConfig = file_exists($ioConfigDir.'/config.local.json') ? 'config.local.json' : 'config.json';

//start the app (bootstrap), provide the primary config file.
try {
    $ioApp->setConfigFile($ioConfig);
    $ioApp->bootstrap();

    $console = new ConsoleApplication();

    /** @var CommandCollection $commands */
    $commands = Hook::processHook('consoleCommands', new CommandCollection());
    $commands->map(System::getRegisteredService(...))->each($console->add(...));

    $console->run();
} catch (Exception $e) {
    echo $e->getMessage();
}
