/** * @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; }
/** * @Command start-http */ public function startHttpServer(Option $entryPoint, Option $host, Option $port, Output $output, ErrorOutput $errorOutput) { $host->setDefaultValue('localhost'); $port->setDefaultValue(8000); $entryPoint->setDefaultValue('public/index.php'); $listen = "{$host->getValue()}:{$port->getValue()}"; $output->writeln("Running http server on {$listen}"); $process = new Process("php -S {$listen} {$entryPoint->getValue()}"); $process->setTimeout(null); $process->run(function ($type, $buffer) use($output, $errorOutput) { if (Process::ERR === $type) { $errorOutput->write($buffer); } else { $output->write($buffer); } }); }
/** * @Command request */ public function request(Parameter $method, Parameter $uri, Option $data, Option $baseUri, Output $output, ErrorOutput $errorOutput) { $baseUri->setDefaultValue('http://localhost:8000'); $args = ['-siL', '-X', $method->getValue(), '-H', 'Content-Type: application/json', '-d', $data->getValue(), "{$baseUri->getValue()}{$uri->getValue()}"]; $command = "curl"; foreach ($args as $arg) { $command .= ' ' . escapeshellarg($arg); } $process = new Process($command); $process->run(function ($type, $buffer) use($output, $errorOutput) { if (Process::ERR === $type) { $errorOutput->write($buffer); } else { $output->write($buffer); } }); $output->writeln(''); }