#!/usr/bin/env php
<?php
namespace asbamboo\frameworkDemo\bin;

use asbamboo\framework\Constant;
use asbamboo\framework\kernel\KernelInterface;
use asbamboo\framework\kernel\Console;
use asbamboo\frameworkDemo\AppKernel;
use asbamboo\event\EventScheduler;
use asbamboo\framework\Event;
use asbamboo\console\ProcessorInterface;

$autoload   = require_once dirname(__DIR__) . '/vendor/asbamboo/autoload/bootstrap.php';
$autoload->addMappingDir('asbamboo\\frameworkDemo\\', dirname(__DIR__));
// $autoload   = require_once dirname(dirname(__DIR__)) . '/test/bootstrap.php';

/**
 * 命令行程序入口
 */
 
EventScheduler::instance()->bind(Event::KERNEL_CONSOLE_PRE_EXEC, function(KernelInterface $Kernel){
    $CommandCollection  = $Kernel->getContainer()->get(ProcessorInterface::class)->commandCollection();
    $command_path		= $Kernel->getProjectDir() . DIRECTORY_SEPARATOR . 'command';
    if(!is_dir($command_path)){
    	return;
    }
    $files  = scandir($command_path);
    foreach($files AS $file){
        $test_class_name    = str_replace('.php', '', $file);
        $command_name       = strtolower(str_replace('Command', '', $test_class_name));
        $test_namespace     = substr(__NAMESPACE__, 0, -3/*bin*/) . 'command\\';
        $test_class_name    = $test_namespace . $test_class_name;
        if(class_exists($test_class_name)){
            $Command        = $Kernel->getContainer()->get($test_class_name);
            if(method_exists($Command, 'getName')){
                $command_name = $Command->getName();
            }
            if(method_exists($Command, 'setContainer')){
                $Command->setContainer($Kernel->getContainer());
            }
            $CommandCollection->add($command_name, $Command);
        }
    }
});

(new Console())->run(new AppKernel($debug = true));