Input.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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
  25. {
  26. /**
  27. * @var InputDefinition
  28. */
  29. protected $definition;
  30. protected $options = array();
  31. protected $arguments = array();
  32. protected $interactive = true;
  33. /**
  34. * Constructor.
  35. *
  36. * @param InputDefinition $definition A InputDefinition instance
  37. */
  38. public function __construct(InputDefinition $definition = null)
  39. {
  40. if (null === $definition) {
  41. $this->definition = new InputDefinition();
  42. } else {
  43. $this->bind($definition);
  44. $this->validate();
  45. }
  46. }
  47. /**
  48. * Binds the current Input instance with the given arguments and options.
  49. *
  50. * @param InputDefinition $definition A InputDefinition instance
  51. */
  52. public function bind(InputDefinition $definition)
  53. {
  54. $this->arguments = array();
  55. $this->options = array();
  56. $this->definition = $definition;
  57. $this->parse();
  58. }
  59. /**
  60. * Processes command line arguments.
  61. */
  62. abstract protected function parse();
  63. /**
  64. * Validates the input.
  65. *
  66. * @throws RuntimeException When not enough arguments are given
  67. */
  68. public function validate()
  69. {
  70. $definition = $this->definition;
  71. $givenArguments = $this->arguments;
  72. $missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) {
  73. return !array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired();
  74. });
  75. if (count($missingArguments) > 0) {
  76. throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
  77. }
  78. }
  79. /**
  80. * Checks if the input is interactive.
  81. *
  82. * @return bool Returns true if the input is interactive
  83. */
  84. public function isInteractive()
  85. {
  86. return $this->interactive;
  87. }
  88. /**
  89. * Sets the input interactivity.
  90. *
  91. * @param bool $interactive If the input should be interactive
  92. */
  93. public function setInteractive($interactive)
  94. {
  95. $this->interactive = (bool) $interactive;
  96. }
  97. /**
  98. * Returns the argument values.
  99. *
  100. * @return array An array of argument values
  101. */
  102. public function getArguments()
  103. {
  104. return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
  105. }
  106. /**
  107. * Returns the argument value for a given argument name.
  108. *
  109. * @param string $name The argument name
  110. *
  111. * @return mixed The argument value
  112. *
  113. * @throws InvalidArgumentException When argument given doesn't exist
  114. */
  115. public function getArgument($name)
  116. {
  117. if (!$this->definition->hasArgument($name)) {
  118. throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  119. }
  120. return isset($this->arguments[$name]) ? $this->arguments[$name] : $this->definition->getArgument($name)->getDefault();
  121. }
  122. /**
  123. * Sets an argument value by name.
  124. *
  125. * @param string $name The argument name
  126. * @param string $value The argument value
  127. *
  128. * @throws InvalidArgumentException When argument given doesn't exist
  129. */
  130. public function setArgument($name, $value)
  131. {
  132. if (!$this->definition->hasArgument($name)) {
  133. throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  134. }
  135. $this->arguments[$name] = $value;
  136. }
  137. /**
  138. * Returns true if an InputArgument object exists by name or position.
  139. *
  140. * @param string|int $name The InputArgument name or position
  141. *
  142. * @return bool true if the InputArgument object exists, false otherwise
  143. */
  144. public function hasArgument($name)
  145. {
  146. return $this->definition->hasArgument($name);
  147. }
  148. /**
  149. * Returns the options values.
  150. *
  151. * @return array An array of option values
  152. */
  153. public function getOptions()
  154. {
  155. return array_merge($this->definition->getOptionDefaults(), $this->options);
  156. }
  157. /**
  158. * Returns the option value for a given option name.
  159. *
  160. * @param string $name The option name
  161. *
  162. * @return mixed The option value
  163. *
  164. * @throws InvalidArgumentException When option given doesn't exist
  165. */
  166. public function getOption($name)
  167. {
  168. if (!$this->definition->hasOption($name)) {
  169. throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
  170. }
  171. return isset($this->options[$name]) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
  172. }
  173. /**
  174. * Sets an option value by name.
  175. *
  176. * @param string $name The option name
  177. * @param string|bool $value The option value
  178. *
  179. * @throws InvalidArgumentException When option given doesn't exist
  180. */
  181. public function setOption($name, $value)
  182. {
  183. if (!$this->definition->hasOption($name)) {
  184. throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
  185. }
  186. $this->options[$name] = $value;
  187. }
  188. /**
  189. * Returns true if an InputOption object exists by name.
  190. *
  191. * @param string $name The InputOption name
  192. *
  193. * @return bool true if the InputOption object exists, false otherwise
  194. */
  195. public function hasOption($name)
  196. {
  197. return $this->definition->hasOption($name);
  198. }
  199. /**
  200. * Escapes a token through escapeshellarg if it contains unsafe chars.
  201. *
  202. * @param string $token
  203. *
  204. * @return string
  205. */
  206. public function escapeToken($token)
  207. {
  208. return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
  209. }
  210. }