From fe27eeb4a3ab17ca1d32ddf42104c802cb985b59 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Wed, 2 Nov 2016 13:53:56 +0100 Subject: [PATCH] Bugfixes and improvements to shell and context --- lib/Context.php | 26 ++++++++++++++++++++++++++ lib/Shell.php | 4 +++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/Context.php b/lib/Context.php index 876422f..f6ca350 100644 --- a/lib/Context.php +++ b/lib/Context.php @@ -12,6 +12,9 @@ class Context protected $data = []; + protected $parent; + + protected $shell; public function __construct($name=null, array $data=[]) { @@ -20,6 +23,28 @@ class Context $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() { // Override this to do setup stuff @@ -52,6 +77,7 @@ class Context { $this->commands[$command] = $handler; $this->commandInfo[$command] = $info; + ksort($this->commands); } public function setCommandHelp($command, $help) diff --git a/lib/Shell.php b/lib/Shell.php index db5270e..3b9c075 100644 --- a/lib/Shell.php +++ b/lib/Shell.php @@ -35,8 +35,10 @@ class Shell public function pushContext(Context $context) { if ($this->context) { + $context->setParent($this->context); array_unshift($this->contextStack, $this->context); } + $context->setShell($this); $this->context = $context; $this->dispatchEvent("context.update", [ "context"=>$this->context ]); } @@ -173,7 +175,7 @@ class Shell * Execute a command with arguments. * * @param string $command The command name to execute - * @param string.. $args Arguments + * @param string $args Arguments * @return mixed * @throws Exception\BadCommandExcception */