You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
5.3 KiB

3 years ago
  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. protected $stream;
  28. protected $options = [];
  29. protected $arguments = [];
  30. protected $interactive = true;
  31. public function __construct(InputDefinition $definition = null)
  32. {
  33. if (null === $definition) {
  34. $this->definition = new InputDefinition();
  35. } else {
  36. $this->bind($definition);
  37. $this->validate();
  38. }
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function bind(InputDefinition $definition)
  44. {
  45. $this->arguments = [];
  46. $this->options = [];
  47. $this->definition = $definition;
  48. $this->parse();
  49. }
  50. /**
  51. * Processes command line arguments.
  52. */
  53. abstract protected function parse();
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function validate()
  58. {
  59. $definition = $this->definition;
  60. $givenArguments = $this->arguments;
  61. $missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) {
  62. return !\array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired();
  63. });
  64. if (\count($missingArguments) > 0) {
  65. throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
  66. }
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function isInteractive()
  72. {
  73. return $this->interactive;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function setInteractive(bool $interactive)
  79. {
  80. $this->interactive = $interactive;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function getArguments()
  86. {
  87. return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getArgument(string $name)
  93. {
  94. if (!$this->definition->hasArgument($name)) {
  95. throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  96. }
  97. return $this->arguments[$name] ?? $this->definition->getArgument($name)->getDefault();
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function setArgument(string $name, $value)
  103. {
  104. if (!$this->definition->hasArgument($name)) {
  105. throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  106. }
  107. $this->arguments[$name] = $value;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function hasArgument($name)
  113. {
  114. return $this->definition->hasArgument($name);
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function getOptions()
  120. {
  121. return array_merge($this->definition->getOptionDefaults(), $this->options);
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function getOption(string $name)
  127. {
  128. if ($this->definition->hasNegation($name)) {
  129. if (null === $value = $this->getOption($this->definition->negationToName($name))) {
  130. return $value;
  131. }
  132. return !$value;
  133. }
  134. if (!$this->definition->hasOption($name)) {
  135. throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
  136. }
  137. return \array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function setOption(string $name, $value)
  143. {
  144. if ($this->definition->hasNegation($name)) {
  145. $this->options[$this->definition->negationToName($name)] = !$value;
  146. return;
  147. } elseif (!$this->definition->hasOption($name)) {
  148. throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
  149. }
  150. $this->options[$name] = $value;
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function hasOption(string $name)
  156. {
  157. return $this->definition->hasOption($name) || $this->definition->hasNegation($name);
  158. }
  159. /**
  160. * Escapes a token through escapeshellarg if it contains unsafe chars.
  161. *
  162. * @return string
  163. */
  164. public function escapeToken(string $token)
  165. {
  166. return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. public function setStream($stream)
  172. {
  173. $this->stream = $stream;
  174. }
  175. /**
  176. * {@inheritdoc}
  177. */
  178. public function getStream()
  179. {
  180. return $this->stream;
  181. }
  182. }