Пример #1
0
 /**
  *
  */
 public function run()
 {
     ksort($this->commands);
     $options = $this->input->getApplicationOptions();
     // --version
     if (isset($options['version'])) {
         $this->output = $this->commandVersion();
         return;
     }
     try {
         $this->output = $this->callCommand($this->input->getCommand(), $this->input->getCommandArguments());
     } catch (InvalidCommandException $e) {
         switch ($e->getCode()) {
             case InvalidCommandException::UNDEFINED:
                 $this->output = sprintf("%s: '%s' is not a %s command. See '%s help'", $this->name, $this->input->getCommand(), $this->name, $this->name);
                 break;
             case InvalidCommandException::UNSPECIFIED:
                 // Client has defined a default command
                 if (!is_null($this->defaultCommand)) {
                     $this->output = call_user_func($this->defaultCommand->getClosure());
                     $this->stop();
                 } else {
                     $this->output = $this->getUsage();
                 }
                 break;
         }
         $this->exit = 1;
     } catch (\BadFunctionCallException $e) {
         $command = $this->getCommand($this->input->getCommand());
         $usage = $command->getUsage();
         $arguments = $this->input->getCommandArguments();
         // Attempt calling subcommand first
         if (count($arguments) > 0) {
             // Subcommand is registered
             if (is_null($command->getSubcommand($arguments[0])) == false) {
                 $usage = $command->getName();
                 $command = $command->getSubcommand($arguments[0]);
                 $usage .= ' ' . $command->getUsage();
                 array_shift($arguments);
             }
         }
         $this->output = sprintf('usage: %s %s', $this->name, $usage) . PHP_EOL;
         $this->exit = 1;
     } catch (CommandInvokedException $e) {
         $this->output = sprintf('%s: %s', $this->name, $e->getMessage()) . PHP_EOL;
         $this->exit = $e->getCode();
     } catch (IncorrectUsageException $e) {
         $this->output = sprintf('usage: %s %s', $this->name, $e->getMessage()) . PHP_EOL;
         $this->exit = 1;
     }
 }
Пример #2
0
 /**
  * @param	Command	$command
  */
 public function registerSubcommand(Command $command)
 {
     $this->subcommands[$command->getName()] = $command;
 }