Input.php 4.6 KB

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