Input.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace Symfony\Component\Console\Input;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  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.potencier@symfony-project.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. * @throws \RuntimeException When not enough arguments are given
  60. */
  61. public function validate()
  62. {
  63. if (count($this->arguments) < $this->definition->getArgumentRequiredCount()) {
  64. throw new \RuntimeException('Not enough arguments.');
  65. }
  66. }
  67. public function isInteractive()
  68. {
  69. return $this->interactive;
  70. }
  71. public function setInteractive($interactive)
  72. {
  73. $this->interactive = (Boolean) $interactive;
  74. }
  75. /**
  76. * Returns the argument values.
  77. *
  78. * @return array An array of argument values
  79. */
  80. public function getArguments()
  81. {
  82. return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
  83. }
  84. /**
  85. * Returns the argument value for a given argument name.
  86. *
  87. * @param string $name The argument name
  88. *
  89. * @return mixed The argument value
  90. *
  91. * @throws \InvalidArgumentException When argument given doesn't exist
  92. */
  93. public function getArgument($name)
  94. {
  95. if (!$this->definition->hasArgument($name)) {
  96. throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  97. }
  98. return isset($this->arguments[$name]) ? $this->arguments[$name] : $this->definition->getArgument($name)->getDefault();
  99. }
  100. /**
  101. * Sets an argument value by name.
  102. *
  103. * @param string $name The argument name
  104. * @param string $value The argument value
  105. *
  106. * @throws \InvalidArgumentException When argument given doesn't exist
  107. */
  108. public function setArgument($name, $value)
  109. {
  110. if (!$this->definition->hasArgument($name)) {
  111. throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  112. }
  113. $this->arguments[$name] = $value;
  114. }
  115. /**
  116. * Returns true if an InputArgument object exists by name or position.
  117. *
  118. * @param string|integer $name The InputArgument name or position
  119. *
  120. * @return Boolean true if the InputArgument object exists, false otherwise
  121. */
  122. public function hasArgument($name)
  123. {
  124. return $this->definition->hasArgument($name);
  125. }
  126. /**
  127. * Returns the options values.
  128. *
  129. * @return array An array of option values
  130. */
  131. public function getOptions()
  132. {
  133. return array_merge($this->definition->getOptionDefaults(), $this->options);
  134. }
  135. /**
  136. * Returns the option value for a given option name.
  137. *
  138. * @param string $name The option name
  139. *
  140. * @return mixed The option value
  141. *
  142. * @throws \InvalidArgumentException When option given doesn't exist
  143. */
  144. public function getOption($name)
  145. {
  146. if (!$this->definition->hasOption($name)) {
  147. throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
  148. }
  149. return isset($this->options[$name]) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
  150. }
  151. /**
  152. * Sets an option value by name.
  153. *
  154. * @param string $name The option name
  155. * @param string $value The option value
  156. *
  157. * @throws \InvalidArgumentException When option given doesn't exist
  158. */
  159. public function setOption($name, $value)
  160. {
  161. if (!$this->definition->hasOption($name)) {
  162. throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
  163. }
  164. $this->options[$name] = $value;
  165. }
  166. /**
  167. * Returns true if an InputOption object exists by name.
  168. *
  169. * @param string $name The InputOption name
  170. *
  171. * @return Boolean true if the InputOption object exists, false otherwise
  172. */
  173. public function hasOption($name)
  174. {
  175. return $this->definition->hasOption($name);
  176. }
  177. }