Input.php 6.0 KB

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