Input.php 5.6 KB

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