ArrayInput.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. * ArrayInput represents an input provided as an array.
  13. *
  14. * Usage:
  15. *
  16. * $input = new ArrayInput(array('name' => 'foo', '--bar' => 'foobar'));
  17. *
  18. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  19. */
  20. class ArrayInput extends Input
  21. {
  22. protected $parameters;
  23. /**
  24. * Constructor.
  25. *
  26. * @param array $param An array of parameters
  27. * @param InputDefinition $definition A InputDefinition instance
  28. */
  29. public function __construct(array $parameters, InputDefinition $definition = null)
  30. {
  31. $this->parameters = $parameters;
  32. parent::__construct($definition);
  33. }
  34. /**
  35. * Returns the first argument from the raw parameters (not parsed).
  36. *
  37. * @return string The value of the first argument or null otherwise
  38. */
  39. public function getFirstArgument()
  40. {
  41. foreach ($this->parameters as $key => $value) {
  42. if ($key && '-' === $key[0]) {
  43. continue;
  44. }
  45. return $value;
  46. }
  47. }
  48. /**
  49. * Returns true if the raw parameters (not parsed) contains a value.
  50. *
  51. * This method is to be used to introspect the input parameters
  52. * before it has been validated. It must be used carefully.
  53. *
  54. * @param string|array $value The values to look for in the raw parameters (can be an array)
  55. *
  56. * @return Boolean true if the value is contained in the raw parameters
  57. */
  58. public function hasParameterOption($values)
  59. {
  60. if (!is_array($values)) {
  61. $values = array($values);
  62. }
  63. foreach ($this->parameters as $k => $v) {
  64. if (!is_int($k)) {
  65. $v = $k;
  66. }
  67. if (in_array($v, $values)) {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. /**
  74. * Processes command line arguments.
  75. */
  76. protected function parse()
  77. {
  78. foreach ($this->parameters as $key => $value) {
  79. if ('--' === substr($key, 0, 2)) {
  80. $this->addLongOption(substr($key, 2), $value);
  81. } elseif ('-' === $key[0]) {
  82. $this->addShortOption(substr($key, 1), $value);
  83. } else {
  84. $this->addArgument($key, $value);
  85. }
  86. }
  87. }
  88. /**
  89. * Adds a short option value.
  90. *
  91. * @param string $shortcut The short option key
  92. * @param mixed $value The value for the option
  93. *
  94. * @throws \RuntimeException When option given doesn't exist
  95. */
  96. protected function addShortOption($shortcut, $value)
  97. {
  98. if (!$this->definition->hasShortcut($shortcut)) {
  99. throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
  100. }
  101. $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
  102. }
  103. /**
  104. * Adds a long option value.
  105. *
  106. * @param string $name The long option key
  107. * @param mixed $value The value for the option
  108. *
  109. * @throws \InvalidArgumentException When option given doesn't exist
  110. * @throws \InvalidArgumentException When a required value is missing
  111. */
  112. protected function addLongOption($name, $value)
  113. {
  114. if (!$this->definition->hasOption($name)) {
  115. throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
  116. }
  117. $option = $this->definition->getOption($name);
  118. if (null === $value) {
  119. if ($option->isValueRequired()) {
  120. throw new \InvalidArgumentException(sprintf('The "--%s" option requires a value.', $name));
  121. }
  122. $value = $option->isValueOptional() ? $option->getDefault() : true;
  123. }
  124. $this->options[$name] = $value;
  125. }
  126. /**
  127. * Adds an argument value.
  128. *
  129. * @param string $name The argument name
  130. * @param mixed $value The value for the argument
  131. *
  132. * @throws \InvalidArgumentException When argument given doesn't exist
  133. */
  134. protected function addArgument($name, $value)
  135. {
  136. if (!$this->definition->hasArgument($name)) {
  137. throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  138. }
  139. $this->arguments[$name] = $value;
  140. }
  141. }