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

use Symfony\Component\Console\Input\ArgvInput;
use Bgaze\Silex\Provider\ConsoleServiceProvider;
use Bgaze\Silex\Console\Command\ClearCacheCommand;

// Constants.
define('CLI', true);
define('ROOT', __DIR__ . '/..');

// Disable time limit.
set_time_limit(0);

// Load dependencies.
require_once ROOT . '/vendor/autoload.php';

// Manage console input.
$input = new ArgvInput();
define('ENV', $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev'));

// Create application.
$app = require ROOT . '/src/app.php';

// Register console provider.
$app->register(new ConsoleServiceProvider(), ['console.name' => 'My Application', 'console.version' => 'n/a']);

// Load commands.
$app['console']->add(new ClearCacheCommand());
if (is_dir(ROOT . '/src/commands')) {
    $app['finder']->in(ROOT . '/src/commands')->files()->name('*.php')->sortByName();
    foreach ($app['finder'] as $file) {
        require_once $file->getRealPath();
    }
}

// Run console.
$app['console']->run();
