Input.php 5.0 KB

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