| 148 |
lars |
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 array $parameters;
|
|
|
29 |
|
|
|
30 |
public function __construct(array $parameters, InputDefinition $definition = null)
|
|
|
31 |
{
|
|
|
32 |
$this->parameters = $parameters;
|
|
|
33 |
|
|
|
34 |
parent::__construct($definition);
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
public function getFirstArgument(): ?string
|
|
|
38 |
{
|
|
|
39 |
foreach ($this->parameters as $param => $value) {
|
|
|
40 |
if ($param && \is_string($param) && '-' === $param[0]) {
|
|
|
41 |
continue;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
return $value;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
return null;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
public function hasParameterOption(string|array $values, bool $onlyParams = false): bool
|
|
|
51 |
{
|
|
|
52 |
$values = (array) $values;
|
|
|
53 |
|
|
|
54 |
foreach ($this->parameters as $k => $v) {
|
|
|
55 |
if (!\is_int($k)) {
|
|
|
56 |
$v = $k;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
if ($onlyParams && '--' === $v) {
|
|
|
60 |
return false;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
if (\in_array($v, $values)) {
|
|
|
64 |
return true;
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
return false;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
public function getParameterOption(string|array $values, string|bool|int|float|array|null $default = false, bool $onlyParams = false): mixed
|
|
|
72 |
{
|
|
|
73 |
$values = (array) $values;
|
|
|
74 |
|
|
|
75 |
foreach ($this->parameters as $k => $v) {
|
|
|
76 |
if ($onlyParams && ('--' === $k || (\is_int($k) && '--' === $v))) {
|
|
|
77 |
return $default;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
if (\is_int($k)) {
|
|
|
81 |
if (\in_array($v, $values)) {
|
|
|
82 |
return true;
|
|
|
83 |
}
|
|
|
84 |
} elseif (\in_array($k, $values)) {
|
|
|
85 |
return $v;
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
return $default;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Returns a stringified representation of the args passed to the command.
|
|
|
94 |
*/
|
|
|
95 |
public function __toString(): string
|
|
|
96 |
{
|
|
|
97 |
$params = [];
|
|
|
98 |
foreach ($this->parameters as $param => $val) {
|
|
|
99 |
if ($param && \is_string($param) && '-' === $param[0]) {
|
|
|
100 |
$glue = ('-' === $param[1]) ? '=' : ' ';
|
|
|
101 |
if (\is_array($val)) {
|
|
|
102 |
foreach ($val as $v) {
|
|
|
103 |
$params[] = $param.('' != $v ? $glue.$this->escapeToken($v) : '');
|
|
|
104 |
}
|
|
|
105 |
} else {
|
|
|
106 |
$params[] = $param.('' != $val ? $glue.$this->escapeToken($val) : '');
|
|
|
107 |
}
|
|
|
108 |
} else {
|
|
|
109 |
$params[] = \is_array($val) ? implode(' ', array_map($this->escapeToken(...), $val)) : $this->escapeToken($val);
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
return implode(' ', $params);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
protected function parse()
|
|
|
117 |
{
|
|
|
118 |
foreach ($this->parameters as $key => $value) {
|
|
|
119 |
if ('--' === $key) {
|
|
|
120 |
return;
|
|
|
121 |
}
|
|
|
122 |
if (str_starts_with($key, '--')) {
|
|
|
123 |
$this->addLongOption(substr($key, 2), $value);
|
|
|
124 |
} elseif (str_starts_with($key, '-')) {
|
|
|
125 |
$this->addShortOption(substr($key, 1), $value);
|
|
|
126 |
} else {
|
|
|
127 |
$this->addArgument($key, $value);
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Adds a short option value.
|
|
|
134 |
*
|
|
|
135 |
* @throws InvalidOptionException When option given doesn't exist
|
|
|
136 |
*/
|
|
|
137 |
private function addShortOption(string $shortcut, mixed $value)
|
|
|
138 |
{
|
|
|
139 |
if (!$this->definition->hasShortcut($shortcut)) {
|
|
|
140 |
throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut));
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
$this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Adds a long option value.
|
|
|
148 |
*
|
|
|
149 |
* @throws InvalidOptionException When option given doesn't exist
|
|
|
150 |
* @throws InvalidOptionException When a required value is missing
|
|
|
151 |
*/
|
|
|
152 |
private function addLongOption(string $name, mixed $value)
|
|
|
153 |
{
|
|
|
154 |
if (!$this->definition->hasOption($name)) {
|
|
|
155 |
if (!$this->definition->hasNegation($name)) {
|
|
|
156 |
throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name));
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
$optionName = $this->definition->negationToName($name);
|
|
|
160 |
$this->options[$optionName] = false;
|
|
|
161 |
|
|
|
162 |
return;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
$option = $this->definition->getOption($name);
|
|
|
166 |
|
|
|
167 |
if (null === $value) {
|
|
|
168 |
if ($option->isValueRequired()) {
|
|
|
169 |
throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name));
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
if (!$option->isValueOptional()) {
|
|
|
173 |
$value = true;
|
|
|
174 |
}
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
$this->options[$name] = $value;
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
/**
|
|
|
181 |
* Adds an argument value.
|
|
|
182 |
*
|
|
|
183 |
* @throws InvalidArgumentException When argument given doesn't exist
|
|
|
184 |
*/
|
|
|
185 |
private function addArgument(string|int $name, mixed $value)
|
|
|
186 |
{
|
|
|
187 |
if (!$this->definition->hasArgument($name)) {
|
|
|
188 |
throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
$this->arguments[$name] = $value;
|
|
|
192 |
}
|
|
|
193 |
}
|