Fixed issue with global commands in parent contexts
This commit is contained in:
		| @@ -20,6 +20,29 @@ class MyContext extends Context | ||||
|      * @command testme | ||||
|      * @args | ||||
|      * @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() | ||||
|     { | ||||
|   | ||||
| @@ -71,7 +71,7 @@ class Context | ||||
|             }, explode("\n", $docblock)); | ||||
|             $info = []; | ||||
|             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; | ||||
|                     $info[$key] = $value;  | ||||
|                 } | ||||
| @@ -132,6 +132,12 @@ class Context | ||||
|         return $ret; | ||||
|     } | ||||
|  | ||||
|     public function isCommandGlobal($command) | ||||
|     { | ||||
|         $info = $this->commandInfo[$command]; | ||||
|         return array_key_exists('global', $info); | ||||
|     } | ||||
|  | ||||
|     public function getName() | ||||
|     { | ||||
|         return $this->name; | ||||
|   | ||||
| @@ -176,12 +176,18 @@ class Shell | ||||
|     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 ($this->context->hasCommand($command)) { | ||||
|             $handler = $this->context->getCommand($command); | ||||
|         } else { | ||||
|             foreach($this->contextStack as $context) { | ||||
|                 if ($context->hasCommand($command) && $context->isCommandGlobal($command)) { | ||||
|                     $handler = $context->getCommand($command); | ||||
|                     break; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         // No handler... | ||||
|         if (empty($handler)) { | ||||
|             return false; | ||||
|         } | ||||
| @@ -236,13 +242,34 @@ class Shell | ||||
|                 break; | ||||
|             case 'help': | ||||
|                 $help = $this->context->getCommandHelp(); | ||||
|                 printf("Commands in current context:\n\n"); | ||||
|                 foreach ($help as $command=>$info) { | ||||
|                     printf("  %-20s %s\n", $command, $info); | ||||
|                 $ghelp = []; | ||||
|                 foreach ($this->contextStack as $context) { | ||||
|                     $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"); | ||||
|                 printf("  %-20s %s\n", "exit", "Leave the shell"); | ||||
|                 printf("  %-20s %s\n", "..", "Discard the current context and go to parent"); | ||||
|                 ksort($ghelp); | ||||
|                 printf("Commands in current context:\n"); | ||||
|                 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; | ||||
|             case 'exit': | ||||
|                 $this->stop(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user