#!/usr/bin/env php
<?php
// @link https://www.tomasvotruba.cz/blog/2018/08/02/5-gotchas-of-the-bin-file-in-php-cli-applications/

declare(strict_types=1);

gc_disable(); // performance boost

$autoloader = 'vendor/autoload.php';

if (Phar::running()) {
    $phar = new Phar($_SERVER['argv'][0]);
    $possibleAutoloadPaths = [
        'phar://' . $phar->getAlias(),
    ];

} else {
    $possibleAutoloadPaths = [
        // local dev repository
        dirname(__DIR__,),
        // dependency
        dirname(__DIR__, 4),
    ];
}

$isAutoloadFound = false;
foreach ($possibleAutoloadPaths as $possibleAutoloadPath) {
    if (file_exists($possibleAutoloadPath . DIRECTORY_SEPARATOR . $autoloader)) {
        require_once $possibleAutoloadPath . DIRECTORY_SEPARATOR . $autoloader;
        $isAutoloadFound = true;
        break;
    }
}

if ($isAutoloadFound === false) {
    throw new RuntimeException(
        sprintf(
            'Unable to find "%s" in "%s" paths.',
            $autoloader,
            implode('", "', $possibleAutoloadPaths)
        )
    );
}

if (PHP_SAPI !== 'cli') {
    return;
}

use Bartlett\UmlWriter\Console\Application;
use Bartlett\UmlWriter\Service\ContainerService;

$application = new Application(new ContainerService());
$application->run();
