| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Console\Input; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Exception\InvalidArgumentException; |
| 15 |
use Symfony\Component\Console\Exception\InvalidOptionException; |
| 16 |
|
| 17 |
/** |
| 18 |
* ArrayInput represents an input provided as an array. |
| 19 |
* |
| 20 |
* Usage: |
| 21 |
* |
| 22 |
* $input = new ArrayInput(['command' => 'foo:bar', 'foo' => 'bar', '--bar' => 'foobar']); |
| 23 |
* |
| 24 |
* @author Fabien Potencier <fabien@symfony.com> |
| 25 |
*/ |
| 26 |
class ArrayInput extends Input |
| 27 |
{ |
| 28 |
private $parameters; |
| 29 |
|
| 30 |
public function __construct(array $parameters, InputDefinition $definition = null) |
| 31 |
{ |
| 32 |
$this->parameters = $parameters; |
| 33 |
|
| 34 |
parent::__construct($definition); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* {@inheritdoc} |
| 39 |
*/ |
| 40 |
public function getFirstArgument() |
| 41 |
{ |
| 42 |
foreach ($this->parameters as $param => $value) { |
| 43 |
if ($param && \is_string($param) && '-' === $param[0]) { |
| 44 |
continue; |
| 45 |
} |
| 46 |
|
| 47 |
return $value; |
| 48 |
} |
| 49 |
|
| 50 |
return null; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* {@inheritdoc} |
| 55 |
*/ |
| 56 |
public function hasParameterOption($values, $onlyParams = false) |
| 57 |
{ |
| 58 |
$values = (array) $values; |
| 59 |
|
| 60 |
foreach ($this->parameters as $k => $v) { |
| 61 |
if (!\is_int($k)) { |
| 62 |
$v = $k; |
| 63 |
} |
| 64 |
|
| 65 |
if ($onlyParams && '--' === $v) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
if (\in_array($v, $values)) { |
| 70 |
return true; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* {@inheritdoc} |
| 79 |
*/ |
| 80 |
public function getParameterOption($values, $default = false, $onlyParams = false) |
| 81 |
{ |
| 82 |
$values = (array) $values; |
| 83 |
|
| 84 |
foreach ($this->parameters as $k => $v) { |
| 85 |
if ($onlyParams && ('--' === $k || (\is_int($k) && '--' === $v))) { |
| 86 |
return $default; |
| 87 |
} |
| 88 |
|
| 89 |
if (\is_int($k)) { |
| 90 |
if (\in_array($v, $values)) { |
| 91 |
return true; |
| 92 |
} |
| 93 |
} elseif (\in_array($k, $values)) { |
| 94 |
return $v; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
return $default; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Returns a stringified representation of the args passed to the command. |
| 103 |
* |
| 104 |
* @return string |
| 105 |
*/ |
| 106 |
public function __toString() |
| 107 |
{ |
| 108 |
$params = []; |
| 109 |
foreach ($this->parameters as $param => $val) { |
| 110 |
if ($param && \is_string($param) && '-' === $param[0]) { |
| 111 |
if (\is_array($val)) { |
| 112 |
foreach ($val as $v) { |
| 113 |
$params[] = $param.('' != $v ? '='.$this->escapeToken($v) : ''); |
| 114 |
} |
| 115 |
} else { |
| 116 |
$params[] = $param.('' != $val ? '='.$this->escapeToken($val) : ''); |
| 117 |
} |
| 118 |
} else { |
| 119 |
$params[] = \is_array($val) ? implode(' ', array_map([$this, 'escapeToken'], $val)) : $this->escapeToken($val); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return implode(' ', $params); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* {@inheritdoc} |
| 128 |
*/ |
| 129 |
protected function parse() |
| 130 |
{ |
| 131 |
foreach ($this->parameters as $key => $value) { |
| 132 |
if ('--' === $key) { |
| 133 |
return; |
| 134 |
} |
| 135 |
if (0 === strpos($key, '--')) { |
| 136 |
$this->addLongOption(substr($key, 2), $value); |
| 137 |
} elseif (0 === strpos($key, '-')) { |
| 138 |
$this->addShortOption(substr($key, 1), $value); |
| 139 |
} else { |
| 140 |
$this->addArgument($key, $value); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Adds a short option value. |
| 147 |
* |
| 148 |
* @param string $shortcut The short option key |
| 149 |
* @param mixed $value The value for the option |
| 150 |
* |
| 151 |
* @throws InvalidOptionException When option given doesn't exist |
| 152 |
*/ |
| 153 |
private function addShortOption($shortcut, $value) |
| 154 |
{ |
| 155 |
if (!$this->definition->hasShortcut($shortcut)) { |
| 156 |
throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut)); |
| 157 |
} |
| 158 |
|
| 159 |
$this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Adds a long option value. |
| 164 |
* |
| 165 |
* @param string $name The long option key |
| 166 |
* @param mixed $value The value for the option |
| 167 |
* |
| 168 |
* @throws InvalidOptionException When option given doesn't exist |
| 169 |
* @throws InvalidOptionException When a required value is missing |
| 170 |
*/ |
| 171 |
private function addLongOption($name, $value) |
| 172 |
{ |
| 173 |
if (!$this->definition->hasOption($name)) { |
| 174 |
throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name)); |
| 175 |
} |
| 176 |
|
| 177 |
$option = $this->definition->getOption($name); |
| 178 |
|
| 179 |
if (null === $value) { |
| 180 |
if ($option->isValueRequired()) { |
| 181 |
throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name)); |
| 182 |
} |
| 183 |
|
| 184 |
if (!$option->isValueOptional()) { |
| 185 |
$value = true; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
$this->options[$name] = $value; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Adds an argument value. |
| 194 |
* |
| 195 |
* @param string $name The argument name |
| 196 |
* @param mixed $value The value for the argument |
| 197 |
* |
| 198 |
* @throws InvalidArgumentException When argument given doesn't exist |
| 199 |
*/ |
| 200 |
private function addArgument($name, $value) |
| 201 |
{ |
| 202 |
if (!$this->definition->hasArgument($name)) { |
| 203 |
throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
| 204 |
} |
| 205 |
|
| 206 |
$this->arguments[$name] = $value; |
| 207 |
} |
| 208 |
} |
| 209 |
|