Improvements
This commit is contained in:
@ -13,26 +13,50 @@ abstract class Shell
|
||||
|
||||
abstract protected function configure(array $config);
|
||||
|
||||
public function addCommand($command, callable $handler)
|
||||
public function addCommand($command, callable $handler=null)
|
||||
{
|
||||
$this->commands[$command] = $handler;
|
||||
if (!$handler) {
|
||||
if (!($command instanceof Command)) {
|
||||
throw new \RuntimeException("Handler is not callable nor a Command");
|
||||
}
|
||||
$command->setShell($this);
|
||||
$this->commands[$command->getName()] = $command;
|
||||
} else {
|
||||
$this->commands[$command] = $handler;
|
||||
}
|
||||
}
|
||||
|
||||
public function execute($command)
|
||||
{
|
||||
if (is_array($command)) {
|
||||
foreach ($command as $cmd) {
|
||||
$this->execute($cmd);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
$buffer = str_getcsv($command, " ", "\"", "\\");
|
||||
|
||||
if (count($buffer)>0) {
|
||||
$this->executeBuffer($buffer);
|
||||
$this->onCommand($buffer);
|
||||
}
|
||||
}
|
||||
|
||||
protected function onCommand($buffer)
|
||||
{
|
||||
$this->executeBuffer($buffer);
|
||||
}
|
||||
|
||||
protected function executeBuffer(array $buffer)
|
||||
{
|
||||
$commandName = array_shift($buffer);
|
||||
if (array_key_exists($commandName, $this->commands)) {
|
||||
$command = $this->commands[$commandName];
|
||||
call_user_func_array($command,$buffer);
|
||||
if ($command instanceof Command) {
|
||||
$command->run($buffer);
|
||||
} else {
|
||||
call_user_func_array($command,$buffer);
|
||||
}
|
||||
return;
|
||||
}
|
||||
$this->writeln("Bad command: ".$commandName);
|
||||
@ -47,35 +71,32 @@ abstract class Shell
|
||||
{
|
||||
}
|
||||
|
||||
public function run(callable $updateFunc=null)
|
||||
public function run()
|
||||
{
|
||||
$lineRead = new LineRead();
|
||||
|
||||
$this->running = true;
|
||||
|
||||
|
||||
|
||||
|
||||
do {
|
||||
$lineRead->setPrompt($this->getPrompt());
|
||||
$buffer = $lineRead->update();
|
||||
ob_start();
|
||||
if (is_callable($updateFunc)) {
|
||||
call_user_func($updateFunc);
|
||||
if ($buffer == "\x03") {
|
||||
$this->stop();
|
||||
continue;
|
||||
}
|
||||
ob_start();
|
||||
$this->onUpdate();
|
||||
if ($buffer !== null) {
|
||||
$this->execute($buffer);
|
||||
}
|
||||
$buf = ob_get_contents();
|
||||
ob_end_clean();
|
||||
if ($buf) {
|
||||
$lineRead->erase();
|
||||
echo rtrim($buf)."\n";
|
||||
echo str_replace("\n", "\r\n", rtrim($buf)."\n");
|
||||
$lineRead->redraw();
|
||||
}
|
||||
//$buffer = readline($this->getPrompt());
|
||||
if ($buffer === null) {
|
||||
usleep(10000);
|
||||
continue;
|
||||
} else {
|
||||
$this->execute($buffer);
|
||||
}
|
||||
usleep(10000);
|
||||
} while ($this->running);
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user