Input.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 InputDefinition $definition;
  27. /** @var resource */
  28. protected $stream;
  29. protected array $options = [];
  30. protected array $arguments = [];
  31. protected bool $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. public function bind(InputDefinition $definition): void
  42. {
  43. $this->arguments = [];
  44. $this->options = [];
  45. $this->definition = $definition;
  46. $this->parse();
  47. }
  48. /**
  49. * Processes command line arguments.
  50. */
  51. abstract protected function parse(): void;
  52. public function validate(): void
  53. {
  54. $definition = $this->definition;
  55. $givenArguments = $this->arguments;
  56. $missingArguments = array_filter(array_keys($definition->getArguments()), fn ($argument) => !\array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired());
  57. if (\count($missingArguments) > 0) {
  58. throw new RuntimeException(\sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
  59. }
  60. }
  61. public function isInteractive(): bool
  62. {
  63. return $this->interactive;
  64. }
  65. public function setInteractive(bool $interactive): void
  66. {
  67. $this->interactive = $interactive;
  68. }
  69. public function getArguments(): array
  70. {
  71. return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
  72. }
  73. public function getArgument(string $name): mixed
  74. {
  75. if (!$this->definition->hasArgument($name)) {
  76. throw new InvalidArgumentException(\sprintf('The "%s" argument does not exist.', $name));
  77. }
  78. return $this->arguments[$name] ?? $this->definition->getArgument($name)->getDefault();
  79. }
  80. public function setArgument(string $name, mixed $value): void
  81. {
  82. if (!$this->definition->hasArgument($name)) {
  83. throw new InvalidArgumentException(\sprintf('The "%s" argument does not exist.', $name));
  84. }
  85. $this->arguments[$name] = $value;
  86. }
  87. public function hasArgument(string $name): bool
  88. {
  89. return $this->definition->hasArgument($name);
  90. }
  91. public function getOptions(): array
  92. {
  93. return array_merge($this->definition->getOptionDefaults(), $this->options);
  94. }
  95. public function getOption(string $name): mixed
  96. {
  97. if ($this->definition->hasNegation($name)) {
  98. if (null === $value = $this->getOption($this->definition->negationToName($name))) {
  99. return $value;
  100. }
  101. return !$value;
  102. }
  103. if (!$this->definition->hasOption($name)) {
  104. throw new InvalidArgumentException(\sprintf('The "%s" option does not exist.', $name));
  105. }
  106. return \array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
  107. }
  108. public function setOption(string $name, mixed $value): void
  109. {
  110. if ($this->definition->hasNegation($name)) {
  111. $this->options[$this->definition->negationToName($name)] = !$value;
  112. return;
  113. } elseif (!$this->definition->hasOption($name)) {
  114. throw new InvalidArgumentException(\sprintf('The "%s" option does not exist.', $name));
  115. }
  116. $this->options[$name] = $value;
  117. }
  118. public function hasOption(string $name): bool
  119. {
  120. return $this->definition->hasOption($name) || $this->definition->hasNegation($name);
  121. }
  122. /**
  123. * Escapes a token through escapeshellarg if it contains unsafe chars.
  124. */
  125. public function escapeToken(string $token): string
  126. {
  127. return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
  128. }
  129. /**
  130. * @param resource $stream
  131. */
  132. public function setStream($stream): void
  133. {
  134. $this->stream = $stream;
  135. }
  136. /**
  137. * @return resource
  138. */
  139. public function getStream()
  140. {
  141. return $this->stream;
  142. }
  143. }