Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console\Input; Chris@0: Chris@0: use Symfony\Component\Console\Exception\InvalidArgumentException; Chris@0: use Symfony\Component\Console\Exception\RuntimeException; Chris@0: Chris@0: /** Chris@0: * Input is the base class for all concrete Input classes. Chris@0: * Chris@0: * Three concrete classes are provided by default: Chris@0: * Chris@0: * * `ArgvInput`: The input comes from the CLI arguments (argv) Chris@0: * * `StringInput`: The input is provided as a string Chris@0: * * `ArrayInput`: The input is provided as an array Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: abstract class Input implements InputInterface, StreamableInputInterface Chris@0: { Chris@0: protected $definition; Chris@0: protected $stream; Chris@17: protected $options = []; Chris@17: protected $arguments = []; Chris@0: protected $interactive = true; Chris@0: Chris@0: public function __construct(InputDefinition $definition = null) Chris@0: { Chris@0: if (null === $definition) { Chris@0: $this->definition = new InputDefinition(); Chris@0: } else { Chris@0: $this->bind($definition); Chris@0: $this->validate(); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function bind(InputDefinition $definition) Chris@0: { Chris@17: $this->arguments = []; Chris@17: $this->options = []; Chris@0: $this->definition = $definition; Chris@0: Chris@0: $this->parse(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Processes command line arguments. Chris@0: */ Chris@0: abstract protected function parse(); Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function validate() Chris@0: { Chris@0: $definition = $this->definition; Chris@0: $givenArguments = $this->arguments; Chris@0: Chris@0: $missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) { Chris@18: return !\array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired(); Chris@0: }); Chris@0: Chris@17: if (\count($missingArguments) > 0) { Chris@0: throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments))); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isInteractive() Chris@0: { Chris@0: return $this->interactive; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setInteractive($interactive) Chris@0: { Chris@0: $this->interactive = (bool) $interactive; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getArguments() Chris@0: { Chris@0: return array_merge($this->definition->getArgumentDefaults(), $this->arguments); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getArgument($name) Chris@0: { Chris@0: if (!$this->definition->hasArgument($name)) { Chris@0: throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); Chris@0: } Chris@0: Chris@0: return isset($this->arguments[$name]) ? $this->arguments[$name] : $this->definition->getArgument($name)->getDefault(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setArgument($name, $value) Chris@0: { Chris@0: if (!$this->definition->hasArgument($name)) { Chris@0: throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); Chris@0: } Chris@0: Chris@0: $this->arguments[$name] = $value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function hasArgument($name) Chris@0: { Chris@0: return $this->definition->hasArgument($name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getOptions() Chris@0: { Chris@0: return array_merge($this->definition->getOptionDefaults(), $this->options); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getOption($name) Chris@0: { Chris@0: if (!$this->definition->hasOption($name)) { Chris@0: throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); Chris@0: } Chris@0: Chris@18: return \array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setOption($name, $value) Chris@0: { Chris@0: if (!$this->definition->hasOption($name)) { Chris@0: throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); Chris@0: } Chris@0: Chris@0: $this->options[$name] = $value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function hasOption($name) Chris@0: { Chris@0: return $this->definition->hasOption($name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Escapes a token through escapeshellarg if it contains unsafe chars. Chris@0: * Chris@0: * @param string $token Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function escapeToken($token) Chris@0: { Chris@0: return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setStream($stream) Chris@0: { Chris@0: $this->stream = $stream; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getStream() Chris@0: { Chris@0: return $this->stream; Chris@0: } Chris@0: }