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

use Edyan\Neuralyzer\ContainerFactory;
use Edyan\Neuralyzer\Console\Application;
use Edyan\Neuralyzer\Service\ServiceInterface;

if (PHP_SAPI !== 'cli') {
    echo 'Error: neuralyzer should be invoked via the CLI version of PHP, not the '.PHP_SAPI.' SAPI.'.PHP_EOL;
    exit(1);
}

$here = __DIR__ . DIRECTORY_SEPARATOR;
$autoloaderPath = 'phar://neuralyzer.phar/vendor/autoload.php';
if (!strpos(basename(__FILE__), 'phar')) {
    if (file_exists($here . 'vendor/autoload.php')) {
        $autoloaderPath = $here . 'vendor/autoload.php';
    } elseif (file_exists($here . '../../../autoload.php')) {
        $autoloaderPath = $here . '../../../autoload.php';
    } elseif (file_exists($here . '../vendor/autoload.php')) {
        $autoloaderPath = $here . '../vendor/autoload.php';
    }
}
require $autoloaderPath;

// Build a container and load services from it
$container = ContainerFactory::createContainer();

// Add commands automatically
$app = new Application('Neuralyzer CLI', Application::VERSION);
foreach ($container->getParameter('console.command.ids') as $command) {
    $app->add($container->get($command));
}
$app->run();
