Drupal investigation

Input.php 4.7KB

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