Implemented contexts, optimizations

This commit is contained in:
2016-11-01 15:12:11 +01:00
parent 27af65af5e
commit de7f12b7d5
9 changed files with 441 additions and 95 deletions

View File

@ -4,58 +4,234 @@ namespace NoccyLabs\Shell;
use NoccyLabs\Shell\LineRead;
abstract class Shell
class Shell
{
protected $prompt;
const EV_PROMPT = "prompt";
const EV_COMMAND = "command";
protected $promptStyle;
protected $commandStyle;
protected $lineReader = null;
public function __construct(array $config=[])
protected $context = null;
protected $contextStack = [];
protected $listeners = [];
public function __construct()
{
$this->configure($config);
$this->configure();
}
abstract protected function configure(array $config);
public function addCommand($command, callable $handler=null)
protected function configure()
{
if (!$handler) {
if (!($command instanceof Command)) {
throw new \RuntimeException("Handler is not callable nor a Command");
}
$command->setShell($this);
$this->commands[$command->getName()] = $command;
}
/**
* Push a new primary context, saving the previous contexts on a stack.
*
* @param Context $context
*/
public function pushContext(Context $context)
{
if ($this->context) {
array_unshift($this->contextStack, $this->context);
}
$this->context = $context;
$this->dispatchEvent("context.update", [ "context"=>$this->context ]);
}
/**
* Pop the current context.
*
* @return Context
*/
public function popContext()
{
$previous = $this->context;
if (count($this->contextStack)>0) {
$this->context = array_shift($this->contextStack);
} else {
$this->commands[$command] = $handler;
$this->context = null;
}
$this->dispatchEvent("context.update", [ "context"=>$this->context ]);
return $previous;
}
public function getContextPath($separator=":")
{
$stack = [ $this->context->getName() ];
foreach ($this->contextStack as $context) {
$stack[] = $context->getName();
}
$stack = array_reverse($stack);
return join($separator,$stack);
}
public function createContext()
{
$context = new Context();
$this->pushContext($context);
return $context;
}
/**
* Add a handler to be called when an event fires.
*
* @param string $event
* @param callable $handler
*/
public function addListener($event, callable $handler)
{
if (!array_key_exists($event,$this->listeners)) {
$this->listeners[$event] = [];
}
$this->listeners[$event][] = $handler;
}
/**
* Invoke event handlers for event.
*
* @param string $event
* @param callable $handler
*/
protected function dispatchEvent($event, array $data=[])
{
if (!array_key_exists($event,$this->listeners)) {
return;
}
foreach ($this->listeners[$event] as $handler) {
call_user_func($handler, (object)$data, $this);
}
}
public function execute($command)
/**
* Add a callback to be called at a regular interval during the update phase.
* Adding timers with a low interval (less than 200 i.e. 0.2s) may have an
* impact on performance.
*
* @param int $interval Interval in ms
* @param callable $handler The handler
* @param array $userdata Data to be passed to the handler
* @return Timer
*/
public function addTimer($interval, callable $handler, array $userdata=[])
{
if (is_array($command)) {
foreach ($command as $cmd) {
$this->execute($cmd);
}
/**
* Remove a created timer.
*
* @param Timer $timer
*/
public function removeTimer(Timer $timer)
{
}
public function setPrompt($text)
{
if (!$this->lineReader) {
return;
}
$this->lineReader->setPromptText($text);
}
/**
* Find a command and return a closure.
*
* @return callable The command
*/
private function findCommand($command)
{
// Go over current context and walk through stack until finding command
foreach(array_merge([ $this->context ] , $this->contextStack) as $context) {
if ($context->hasCommand($command)) {
$handler = $context->getCommand($command);
break;
}
}
if (empty($handler)) {
return false;
}
// Return closure
return function (...$args) use ($handler) {
return call_user_func($handler, ...$args);
};
}
/**
* Execute a command with arguments.
*
* @param string $command The command name to execute
* @param string.. $args Arguments
* @return mixed
* @throws Exception\BadCommandExcception
*/
public function executeCommand($command, ...$args)
{
if ($this->executeBuiltin($command, ...$args)) {
return;
}
$buffer = str_getcsv($command, " ", "\"", "\\");
if (count($buffer)>0) {
$this->onCommand($buffer);
// Call the handler if the command was found
if (($target = $this->findCommand($command))) {
$ret = $target(...$args);
if ($ret instanceof Context) {
$this->pushContext($ret);
}
return;
}
}
protected function onCommand($buffer)
{
$this->executeBuffer($buffer);
// Throw error if the command could not be found
throw new Exception\BadCommandException("Command {$command} not found");
}
protected function executeBuffer(array $buffer)
public function executeBuiltin($command, ...$args)
{
$commandName = array_shift($buffer);
switch ($command) {
case '.':
printf("context<%s>:\n", $this->context->getName());
echo " ".join("\n ",explode("\n",json_encode($this->context->getData(),JSON_PRETTY_PRINT)))."\n";
break;
case '..':
if (count($this->contextStack)>0)
$this->popContext();
break;
case 'help':
echo
"Built in commands:\n".
" . Show current context\n".
" .. Go to parent context\n".
" exit Exit the shell\n";
break;
case 'exit':
$this->stop();
break;
default:
return false;
}
return true;
}
/**
* Parse a string and execute the resulting command.
*
* @param string $command The string to parse
* @return mixed
*/
public function executeBuffer(string $command)
{
$args = str_getcsv($command, " ", "\"", "\\");
$command = array_shift($args);
try {
$this->executeCommand($command, ...$args);
} catch (Exception\ShellException $e) {
echo "\e[31;91;1m{$e->getMessage()}\e[0m\n";
}
/*
if (array_key_exists($commandName, $this->commands)) {
$command = $this->commands[$commandName];
if ($command instanceof Command) {
@ -66,61 +242,52 @@ abstract class Shell
return;
}
$this->writeln("Bad command: ".$commandName);
}
public function writeln($output)
{
echo "\r\e[K\e[0m".$output."\n";
}
protected function onUpdate()
{
}
public function setPrompt($prompt)
{
$this->prompt = $prompt;
}
public function setPromptStyle($style)
{
$this->promptStyle = $style;
}
public function setCommandStyle($style)
{
$this->commandStyle = $style;
*/
}
public function run()
{
$lineRead = new LineRead();
$lineRead->setCommandStyle($this->commandStyle);
$this->lineReader = new LineRead();
$this->lineReader->setPromptText("shell>");
$this->lineReader->setPromptStyle(new Style(Style::BR_GREEN));
$this->lineReader->setCommandStyle(new Style(Style::GREEN));
$this->running = true;
do {
$lineRead->setPrompt($this->prompt, $this->promptStyle);
$buffer = $lineRead->update();
$this->dispatchEvent("prompt", [ "context"=>$this->context ]);
while ($this->running) {
// Update the input stuff, sleep if nothing to do.
if (!($buffer = $this->lineReader->update())) {
usleep(10000);
continue;
}
// we get a ^C on ^C, so deal with the ^C.
if ($buffer == "\x03") {
$this->stop();
continue;
}
// Execute the buffer
ob_start();
$this->onUpdate();
if ($buffer !== null) {
$this->execute($buffer);
}
$buf = ob_get_contents();
$this->executeBuffer($buffer);
$output = ob_get_contents();
ob_end_clean();
if ($buf) {
$lineRead->erase();
echo str_replace("\n", "\r\n", rtrim($buf)."\n");
$lineRead->redraw();
if (trim($output)) {
$this->lineReader->erase();
echo rtrim($output)."\n";
$this->lineReader->redraw();
}
usleep(10000);
} while ($this->running);
if (!$this->context) {
$this->stop();
}
$this->dispatchEvent("prompt", [ "context"=>$this->context ]);
}
$this->lineReader = null;
}