Bugfixes and improvements to shell and context

This commit is contained in:
Chris 2016-11-02 13:53:56 +01:00
parent a2c1148c52
commit fe27eeb4a3
2 changed files with 29 additions and 1 deletions

View File

@ -12,6 +12,9 @@ class Context
protected $data = []; protected $data = [];
protected $parent;
protected $shell;
public function __construct($name=null, array $data=[]) public function __construct($name=null, array $data=[])
{ {
@ -20,6 +23,28 @@ class Context
$this->configure(); $this->configure();
} }
public function setParent(Context $parent=null)
{
$this->parent = $parent;
}
public function getParent()
{
return $this->parent;
}
public function getRoot()
{
if (!$this->parent) {
return $this;
}
$node = $this;
while ($parent = $node->getParent()) {
$node = $parent;
}
return $parent;
}
protected function configure() protected function configure()
{ {
// Override this to do setup stuff // Override this to do setup stuff
@ -52,6 +77,7 @@ class Context
{ {
$this->commands[$command] = $handler; $this->commands[$command] = $handler;
$this->commandInfo[$command] = $info; $this->commandInfo[$command] = $info;
ksort($this->commands);
} }
public function setCommandHelp($command, $help) public function setCommandHelp($command, $help)

View File

@ -35,8 +35,10 @@ class Shell
public function pushContext(Context $context) public function pushContext(Context $context)
{ {
if ($this->context) { if ($this->context) {
$context->setParent($this->context);
array_unshift($this->contextStack, $this->context); array_unshift($this->contextStack, $this->context);
} }
$context->setShell($this);
$this->context = $context; $this->context = $context;
$this->dispatchEvent("context.update", [ "context"=>$this->context ]); $this->dispatchEvent("context.update", [ "context"=>$this->context ]);
} }
@ -173,7 +175,7 @@ class Shell
* Execute a command with arguments. * Execute a command with arguments.
* *
* @param string $command The command name to execute * @param string $command The command name to execute
* @param string.. $args Arguments * @param string $args Arguments
* @return mixed * @return mixed
* @throws Exception\BadCommandExcception * @throws Exception\BadCommandExcception
*/ */