Input.php 6.0 KB

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