コード例 #1
0
ファイル: Command.php プロジェクト: codespot/sprinter
 /**
  * @param array $cliArguments
  * @return array
  */
 protected function _parseArguments(array $cliArguments)
 {
     $commandArguments = new CommandArguments($cliArguments);
     $options = $commandArguments->getOptions();
     $arguments = $commandArguments->getArguments();
     $parameters = [];
     foreach ($this->_getReflection()->getParameters() as $parameter) {
         $name = $parameter->getName();
         if ($class = $parameter->getClass()) {
             if ($class->implementsInterface('\\Sprinter\\Argument\\ParameterInterface')) {
                 if (!count($arguments)) {
                     throw new \InvalidArgumentException('Not enough arguments');
                 }
                 $parameters[] = new Parameter(array_shift($arguments));
                 continue;
             }
             if ($class->implementsInterface('\\Sprinter\\Argument\\OptionInterface')) {
                 $option = new Option();
                 if (isset($options[$name])) {
                     $option->setValue($options[$name]);
                     unset($options[$name]);
                 }
                 $parameters[] = $option;
                 continue;
             }
             if ($class->implementsInterface('\\Sprinter\\Stream\\InputInterface')) {
                 /** @var Input $input */
                 $input = $class->newInstance();
                 $input->setOutput(new ErrorOutput());
                 $parameters[] = $input;
                 continue;
             }
             if ($class->implementsInterface('\\Sprinter\\Stream\\OutputInterface')) {
                 $parameters[] = $class->newInstance();
                 continue;
             }
         }
         throw new \InvalidArgumentException('Invalid Command argument, needs to be Parameter, Option, or Input/Output stream');
     }
     if (count($arguments)) {
         throw new \InvalidArgumentException('Too many arguments passed');
     }
     if (count($options)) {
         $invalidOptions = array_keys($options);
         $invalidOption = array_shift($invalidOptions);
         throw new \InvalidArgumentException("Invalid option : `--{$invalidOption}`");
     }
     return $parameters;
 }