Browse Source

Add docs and simplify getShell().

Mark Story 8 years ago
parent
commit
c871aa3cc2
1 changed files with 10 additions and 11 deletions
  1. 10 11
      src/Console/CommandRunner.php

+ 10 - 11
src/Console/CommandRunner.php

@@ -66,15 +66,12 @@ class CommandRunner
                 "Unknown root command{$command}. Was expecting `{$this->root}`."
             );
         }
-        $io = $io ?: new ConsoleIo();
-
         // Remove the root executable segment
         array_shift($argv);
 
-        $shell = $this->getShell($io, $commands, $argv);
+        $io = $io ?: new ConsoleIo();
+        $shell = $this->getShell($io, $commands, array_shift($argv));
 
-        // Remove the command name segment
-        array_shift($argv);
         try {
             $shell->initialize();
             $result = $shell->runCommand($argv, true);
@@ -93,20 +90,22 @@ class CommandRunner
     }
 
     /**
-     * Get the shell instance for the argv list.
+     * Get the shell instance for a given command name
      *
+     * @param \Cake\Console\ConsoleIo $io The io wrapper for the created shell class.
+     * @param \Cake\Console\CommandCollection $commands The command collection to find the shell in.
+     * @param string $name The command name to find
      * @return \Cake\Console\Shell
      */
-    protected function getShell(ConsoleIo $io, CommandCollection $commands, array $argv)
+    protected function getShell(ConsoleIo $io, CommandCollection $commands, $name)
     {
-        $command = array_shift($argv);
-        if (!$commands->has($command)) {
+        if (!$commands->has($name)) {
             throw new RuntimeException(
-                "Unknown command `{$this->root} {$command}`." .
+                "Unknown command `{$this->root} {$name}`." .
                 " Run `{$this->root} --help` to get the list of valid commands."
             );
         }
-        $classOrInstance = $commands->get($command);
+        $classOrInstance = $commands->get($name);
         if (is_string($classOrInstance)) {
             return new $classOrInstance($io);
         }