Application console

This commit is contained in:
Simon Vieille 2015-02-17 01:18:05 +01:00
parent af647f7549
commit c24c3f6e47
2 changed files with 71 additions and 0 deletions

16
app/console Executable file
View file

@ -0,0 +1,16 @@
#!/usr/bin/env php
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Deblan\Console\Application;
$app = new Application();
$app->chdir(__DIR__.'/../');
$app->addCommandsPath('src/Deblan/PowerDNS/Command/', 'PowerDNS\\Command');
$app->addCommandsPath('vendor/propel/propel/src/Propel/Generator/Command/', 'Propel\\Generator\\Command');
$app->loadCommands();
$app->run();;

View file

@ -0,0 +1,55 @@
<?php
namespace Deblan\Console;
use ReflectionException;
use ReflectionClass;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Finder\Finder;
class Application extends BaseApplication
{
protected $commandsPaths = array();
public function addCommandsPath($path, $namespace)
{
$this->commandsPaths[$path] = trim($namespace, '\\');
return $this;
}
public function chdir($directory)
{
chdir($directory);
return $this;
}
public function loadCommands()
{
foreach ($this->commandsPaths as $path => $namespace) {
$finder = new Finder();
$finder->name('*Command.php')->in($path);
foreach ($finder as $file) {
$className = $namespace.'\\'.str_replace('.php', '', $file->getFilename());
try {
$reflexion = new ReflectionClass($className);
if ($reflexion->isInstantiable()) {
$command = new $className();
$this->addCommands(array(
$command,
));
}
} catch (ReflectionException $e) {
}
}
}
return $this;
}
}