Input.php 5.0 KB

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