t411-console/src/Console/Application.php

45 lines
1,008 B
PHP
Raw Normal View History

2015-02-10 01:17:29 +01:00
<?php
namespace Console;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Finder\Finder;
class Application extends BaseApplication
{
protected $commandsPaths = array(
'src/Console/Command' => 'Console\\Command',
);
2015-02-15 15:23:29 +01:00
public function addCommandsPath($path, $namespace)
2015-02-10 01:17:29 +01:00
{
2015-02-15 15:23:29 +01:00
$this->commandsPaths[$path] = trim($namespace, '\\');
2015-02-10 01:17:29 +01:00
2015-02-15 15:23:29 +01:00
return $this;
2015-02-10 01:17:29 +01:00
}
2015-02-15 15:23:29 +01:00
public function chdir($directory)
2015-02-10 01:17:29 +01:00
{
2015-02-15 15:23:29 +01:00
chdir($directory);
2015-02-10 01:17:29 +01:00
return $this;
}
2015-02-15 15:23:29 +01:00
public function loadCommands()
2015-02-10 01:17:29 +01:00
{
foreach ($this->commandsPaths as $path => $namespace) {
2015-02-23 10:01:45 +01:00
$finder = new Finder();
2015-02-16 23:56:53 +01:00
$finder->name('*Command.php')->in($path);
2015-02-10 01:17:29 +01:00
foreach ($finder as $file) {
$className = $namespace.'\\'.str_replace('.php', '', $file->getFilename());
$this->addCommands(array(
new $className(),
));
}
}
2015-02-15 15:23:29 +01:00
return $this;
2015-02-10 01:17:29 +01:00
}
}