Input.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Input;
  11. use Symfony\Component\Console\Exception\InvalidArgumentException;
  12. use Symfony\Component\Console\Exception\RuntimeException;
  13. /**
  14. * Input is the base class for all concrete Input classes.
  15. *
  16. * Three concrete classes are provided by default:
  17. *
  18. * * `ArgvInput`: The input comes from the CLI arguments (argv)
  19. * * `StringInput`: The input is provided as a string
  20. * * `ArrayInput`: The input is provided as an array
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. */
  24. abstract class Input implements InputInterface, StreamableInputInterface
  25. {
  26. protected $definition;
  27. /** @var resource */
  28. protected $stream;
  29. protected $options = [];
  30. protected $arguments = [];
  31. protected $interactive = true;
  32. public function __construct(?InputDefinition $definition = null)
  33. {
  34. if (null === $definition) {
  35. $this->definition = new InputDefinition();
  36. } else {
  37. $this->bind($definition);
  38. $this->validate();
  39. }
  40. }
  41. /**
  42. * @return void
  43. */
  44. public function bind(InputDefinition $definition)
  45. {
  46. $this->arguments = [];
  47. $this->options = [];
  48. $this->definition = $definition;
  49. $this->parse();
  50. }
  51. /**
  52. * Processes command line arguments.
  53. *
  54. * @return void
  55. */
  56. abstract protected function parse();
  57. /**
  58. * @return void
  59. */
  60. public function validate()
  61. {
  62. $definition = $this->definition;
  63. $givenArguments = $this->arguments;
  64. $missingArguments = array_filter(array_keys($definition->getArguments()), fn ($argument) => !\array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired());
  65. if (\count($missingArguments) > 0) {
  66. throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
  67. }
  68. }
  69. public function isInteractive(): bool
  70. {
  71. return $this->interactive;
  72. }
  73. /**
  74. * @return void
  75. */
  76. public function setInteractive(bool $interactive)
  77. {
  78. $this->interactive = $interactive;
  79. }
  80. public function getArguments(): array
  81. {
  82. return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
  83. }
  84. public function getArgument(string $name): mixed
  85. {
  86. if (!$this->definition->hasArgument($name)) {
  87. throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  88. }
  89. return $this->arguments[$name] ?? $this->definition->getArgument($name)->getDefault();
  90. }
  91. /**
  92. * @return void
  93. */
  94. public function setArgument(string $name, mixed $value)
  95. {
  96. if (!$this->definition->hasArgument($name)) {
  97. throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  98. }
  99. $this->arguments[$name] = $value;
  100. }
  101. public function hasArgument(string $name): bool
  102. {
  103. return $this->definition->hasArgument($name);
  104. }
  105. public function getOptions(): array
  106. {
  107. return array_merge($this->definition->getOptionDefaults(), $this->options);
  108. }
  109. public function getOption(string $name): mixed
  110. {
  111. if ($this->definition->hasNegation($name)) {
  112. if (null === $value = $this->getOption($this->definition->negationToName($name))) {
  113. return $value;
  114. }
  115. return !$value;
  116. }
  117. if (!$this->definition->hasOption($name)) {
  118. throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
  119. }
  120. return \array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
  121. }
  122. /**
  123. * @return void
  124. */
  125. public function setOption(string $name, mixed $value)
  126. {
  127. if ($this->definition->hasNegation($name)) {
  128. $this->options[$this->definition->negationToName($name)] = !$value;
  129. return;
  130. } elseif (!$this->definition->hasOption($name)) {
  131. throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
  132. }
  133. $this->options[$name] = $value;
  134. }
  135. public function hasOption(string $name): bool
  136. {
  137. return $this->definition->hasOption($name) || $this->definition->hasNegation($name);
  138. }
  139. /**
  140. * Escapes a token through escapeshellarg if it contains unsafe chars.
  141. */
  142. public function escapeToken(string $token): string
  143. {
  144. return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
  145. }
  146. /**
  147. * @param resource $stream
  148. *
  149. * @return void
  150. */
  151. public function setStream($stream)
  152. {
  153. $this->stream = $stream;
  154. }
  155. /**
  156. * @return resource
  157. */
  158. public function getStream()
  159. {
  160. return $this->stream;
  161. }
  162. }