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

use Feeder\Console\Application;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator as LazyProxyRuntimeInstantiator;
use Symfony\Component\Dotenv\Dotenv;

ini_set('memory_limit', '1024M');

$rootDir = __DIR__ . '/../../../..';

include($rootDir.'/vendor/autoload.php');

$dotenv = new Dotenv();
$dotenv->load($rootDir . '/.env');

$container = new ContainerBuilder();
$container->setProxyInstantiator(new LazyProxyRuntimeInstantiator());

$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../config'));
if (interface_exists('\Geocoder\Geocoder')) {
    $loader->load('services/geo.yml');
}
$container->compile(true);

$application = new Application();
$application->setContainer($container);

$finder = new \Symfony\Component\Finder\Finder();
$finder = $finder->files()->in($rootDir.'/src/Command/');

foreach ($finder as $file) {
    require_once $file;

    $declaredClasses = get_declared_classes();
    if (!is_array($declaredClasses)) {
        continue;
    }
    $lastClass = array_pop($declaredClasses);
    if (!is_string($lastClass)) {
        continue;
    }

    $reflection = new \ReflectionClass($lastClass);

    if (!$reflection->isAbstract() && !$reflection->getConstructor()->getNumberOfRequiredParameters()) {
        $application->add($reflection->newInstance());
    }
}

$application->run();
