菜谱项目

Input.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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, StreamableInputInterface
  25. {
  26. protected $definition;
  27. protected $stream;
  28. protected $options = array();
  29. protected $arguments = array();
  30. protected $interactive = true;
  31. public function __construct(InputDefinition $definition = null)
  32. {
  33. if (null === $definition) {
  34. $this->definition = new InputDefinition();
  35. } else {
  36. $this->bind($definition);
  37. $this->validate();
  38. }
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function bind(InputDefinition $definition)
  44. {
  45. $this->arguments = array();
  46. $this->options = array();
  47. $this->definition = $definition;
  48. $this->parse();
  49. }
  50. /**
  51. * Processes command line arguments.
  52. */
  53. abstract protected function parse();
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function validate()
  58. {
  59. $definition = $this->definition;
  60. $givenArguments = $this->arguments;
  61. $missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) {
  62. return !array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired();
  63. });
  64. if (count($missingArguments) > 0) {
  65. throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
  66. }
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function isInteractive()
  72. {
  73. return $this->interactive;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function setInteractive($interactive)
  79. {
  80. $this->interactive = (bool) $interactive;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function getArguments()
  86. {
  87. return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getArgument($name)
  93. {
  94. if (!$this->definition->hasArgument($name)) {
  95. throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  96. }
  97. return isset($this->arguments[$name]) ? $this->arguments[$name] : $this->definition->getArgument($name)->getDefault();
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function setArgument($name, $value)
  103. {
  104. if (!$this->definition->hasArgument($name)) {
  105. throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  106. }
  107. $this->arguments[$name] = $value;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function hasArgument($name)
  113. {
  114. return $this->definition->hasArgument($name);
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function getOptions()
  120. {
  121. return array_merge($this->definition->getOptionDefaults(), $this->options);
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function getOption($name)
  127. {
  128. if (!$this->definition->hasOption($name)) {
  129. throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
  130. }
  131. return array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function setOption($name, $value)
  137. {
  138. if (!$this->definition->hasOption($name)) {
  139. throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
  140. }
  141. $this->options[$name] = $value;
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function hasOption($name)
  147. {
  148. return $this->definition->hasOption($name);
  149. }
  150. /**
  151. * Escapes a token through escapeshellarg if it contains unsafe chars.
  152. *
  153. * @param string $token
  154. *
  155. * @return string
  156. */
  157. public function escapeToken($token)
  158. {
  159. return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function setStream($stream)
  165. {
  166. $this->stream = $stream;
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. public function getStream()
  172. {
  173. return $this->stream;
  174. }
  175. }