Fixed issue with global commands in parent contexts
This commit is contained in:
parent
46806e3d62
commit
455574b6a5
@ -20,6 +20,29 @@ class MyContext extends Context
|
|||||||
* @command testme
|
* @command testme
|
||||||
* @args
|
* @args
|
||||||
* @help Useful test!
|
* @help Useful test!
|
||||||
|
* @global
|
||||||
|
*/
|
||||||
|
public function test()
|
||||||
|
{
|
||||||
|
echo "Test\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @command context
|
||||||
|
* @help Create a new context
|
||||||
|
*/
|
||||||
|
public function context()
|
||||||
|
{
|
||||||
|
return new OtherContext("newcontext");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class OtherContext extends Context
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @command other
|
||||||
|
* @args
|
||||||
|
* @help Other test
|
||||||
*/
|
*/
|
||||||
public function test()
|
public function test()
|
||||||
{
|
{
|
||||||
|
@ -71,7 +71,7 @@ class Context
|
|||||||
}, explode("\n", $docblock));
|
}, explode("\n", $docblock));
|
||||||
$info = [];
|
$info = [];
|
||||||
foreach ($lines as $line) {
|
foreach ($lines as $line) {
|
||||||
if (preg_match("/^@(command|help|args) (.+?)$/", $line, $match)) {
|
if (preg_match("/^@(command|help|args|global)\\s*(.*)$/", $line, $match)) {
|
||||||
list($void,$key,$value) = $match;
|
list($void,$key,$value) = $match;
|
||||||
$info[$key] = $value;
|
$info[$key] = $value;
|
||||||
}
|
}
|
||||||
@ -132,6 +132,12 @@ class Context
|
|||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function isCommandGlobal($command)
|
||||||
|
{
|
||||||
|
$info = $this->commandInfo[$command];
|
||||||
|
return array_key_exists('global', $info);
|
||||||
|
}
|
||||||
|
|
||||||
public function getName()
|
public function getName()
|
||||||
{
|
{
|
||||||
return $this->name;
|
return $this->name;
|
||||||
|
@ -176,12 +176,18 @@ class Shell
|
|||||||
private function findCommand($command)
|
private function findCommand($command)
|
||||||
{
|
{
|
||||||
// Go over current context and walk through stack until finding command
|
// Go over current context and walk through stack until finding command
|
||||||
foreach(array_merge([ $this->context ] , $this->contextStack) as $context) {
|
if ($this->context->hasCommand($command)) {
|
||||||
if ($context->hasCommand($command)) {
|
$handler = $this->context->getCommand($command);
|
||||||
$handler = $context->getCommand($command);
|
} else {
|
||||||
break;
|
foreach($this->contextStack as $context) {
|
||||||
|
if ($context->hasCommand($command) && $context->isCommandGlobal($command)) {
|
||||||
|
$handler = $context->getCommand($command);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// No handler...
|
||||||
if (empty($handler)) {
|
if (empty($handler)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -236,13 +242,34 @@ class Shell
|
|||||||
break;
|
break;
|
||||||
case 'help':
|
case 'help':
|
||||||
$help = $this->context->getCommandHelp();
|
$help = $this->context->getCommandHelp();
|
||||||
printf("Commands in current context:\n\n");
|
$ghelp = [];
|
||||||
foreach ($help as $command=>$info) {
|
foreach ($this->contextStack as $context) {
|
||||||
printf(" %-20s %s\n", $command, $info);
|
$commands = $context->getCommandHelp();
|
||||||
|
foreach ($commands as $command=>$info) {
|
||||||
|
if (strpos(" ",$command)!==false) {
|
||||||
|
list ($cmd,$arg)=explode(" ",$command,2);
|
||||||
|
} else {
|
||||||
|
$cmd = $command;
|
||||||
|
}
|
||||||
|
if ($context->isCommandGlobal($cmd)) {
|
||||||
|
$ghelp[$command] = $info;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
printf("\nGlobal commands:\n\n");
|
ksort($ghelp);
|
||||||
printf(" %-20s %s\n", "exit", "Leave the shell");
|
printf("Commands in current context:\n");
|
||||||
printf(" %-20s %s\n", "..", "Discard the current context and go to parent");
|
foreach ($help as $command=>$info) {
|
||||||
|
printf(" %-20s %s\n", $command, $info);
|
||||||
|
}
|
||||||
|
if (count($ghelp)) {
|
||||||
|
printf("\nImported from parent contexts:\n");
|
||||||
|
foreach ($ghelp as $command=>$info) {
|
||||||
|
printf(" %-20s %s\n", $command, $info);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("\nGlobal commands:\n");
|
||||||
|
printf(" %-20s %s\n", "exit", "Leave the shell");
|
||||||
|
printf(" %-20s %s\n", "..", "Discard the current context and go to parent");
|
||||||
break;
|
break;
|
||||||
case 'exit':
|
case 'exit':
|
||||||
$this->stop();
|
$this->stop();
|
||||||
|
Loading…
Reference in New Issue
Block a user