|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Symfony package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
|
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(array('name' => 'foo', '--bar' => 'foobar')); |
|
23
|
|
|
* |
|
24
|
|
|
* @author Fabien Potencier <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class ArrayInput extends Input |
|
27
|
|
|
{ |
|
28
|
|
|
private $parameters; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $parameters An array of parameters |
|
34
|
|
|
* @param InputDefinition $definition A InputDefinition instance |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(array $parameters, InputDefinition $definition = null) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->parameters = $parameters; |
|
39
|
|
|
|
|
40
|
|
|
parent::__construct($definition); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Returns the first argument from the raw parameters (not parsed). |
|
45
|
|
|
* |
|
46
|
|
|
* @return string The value of the first argument or null otherwise |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getFirstArgument() |
|
49
|
|
|
{ |
|
50
|
|
|
foreach ($this->parameters as $key => $value) { |
|
51
|
|
|
if ($key && '-' === $key[0]) { |
|
52
|
|
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $value; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns true if the raw parameters (not parsed) contain a value. |
|
61
|
|
|
* |
|
62
|
|
|
* This method is to be used to introspect the input parameters |
|
63
|
|
|
* before they have been validated. It must be used carefully. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string|array $values The values to look for in the raw parameters (can be an array) |
|
66
|
|
|
* @param bool $onlyParams Only check real parameters, skip those following an end of options (--) signal |
|
67
|
|
|
* |
|
68
|
|
|
* @return bool true if the value is contained in the raw parameters |
|
69
|
|
|
*/ |
|
70
|
|
|
public function hasParameterOption($values, $onlyParams = false) |
|
71
|
|
|
{ |
|
72
|
|
|
$values = (array) $values; |
|
73
|
|
|
|
|
74
|
|
|
foreach ($this->parameters as $k => $v) { |
|
75
|
|
|
if (!is_int($k)) { |
|
76
|
|
|
$v = $k; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if ($onlyParams && $v === '--') { |
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (in_array($v, $values)) { |
|
84
|
|
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns the value of a raw option (not parsed). |
|
93
|
|
|
* |
|
94
|
|
|
* This method is to be used to introspect the input parameters |
|
95
|
|
|
* before they have been validated. It must be used carefully. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string|array $values The value(s) to look for in the raw parameters (can be an array) |
|
98
|
|
|
* @param mixed $default The default value to return if no result is found |
|
99
|
|
|
* @param bool $onlyParams Only check real parameters, skip those following an end of options (--) signal |
|
100
|
|
|
* |
|
101
|
|
|
* @return mixed The option value |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getParameterOption($values, $default = false, $onlyParams = false) |
|
104
|
|
|
{ |
|
105
|
|
|
$values = (array) $values; |
|
106
|
|
|
|
|
107
|
|
|
foreach ($this->parameters as $k => $v) { |
|
108
|
|
|
if ($onlyParams && ($k === '--' || (is_int($k) && $v === '--'))) { |
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (is_int($k)) { |
|
113
|
|
|
if (in_array($v, $values)) { |
|
114
|
|
|
return true; |
|
115
|
|
|
} |
|
116
|
|
|
} elseif (in_array($k, $values)) { |
|
117
|
|
|
return $v; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $default; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Returns a stringified representation of the args passed to the command. |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
public function __toString() |
|
130
|
|
|
{ |
|
131
|
|
|
$params = array(); |
|
132
|
|
|
foreach ($this->parameters as $param => $val) { |
|
133
|
|
|
if ($param && '-' === $param[0]) { |
|
134
|
|
|
$params[] = $param.('' != $val ? '='.$this->escapeToken($val) : ''); |
|
135
|
|
|
} else { |
|
136
|
|
|
$params[] = $this->escapeToken($val); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return implode(' ', $params); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Processes command line arguments. |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function parse() |
|
147
|
|
|
{ |
|
148
|
|
|
foreach ($this->parameters as $key => $value) { |
|
149
|
|
|
if ($key === '--') { |
|
150
|
|
|
return; |
|
151
|
|
|
} |
|
152
|
|
|
if (0 === strpos($key, '--')) { |
|
153
|
|
|
$this->addLongOption(substr($key, 2), $value); |
|
154
|
|
|
} elseif ('-' === $key[0]) { |
|
155
|
|
|
$this->addShortOption(substr($key, 1), $value); |
|
156
|
|
|
} else { |
|
157
|
|
|
$this->addArgument($key, $value); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Adds a short option value. |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $shortcut The short option key |
|
166
|
|
|
* @param mixed $value The value for the option |
|
167
|
|
|
* |
|
168
|
|
|
* @throws InvalidOptionException When option given doesn't exist |
|
169
|
|
|
*/ |
|
170
|
|
View Code Duplication |
private function addShortOption($shortcut, $value) |
|
171
|
|
|
{ |
|
172
|
|
|
if (!$this->definition->hasShortcut($shortcut)) { |
|
173
|
|
|
throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut)); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Adds a long option value. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $name The long option key |
|
183
|
|
|
* @param mixed $value The value for the option |
|
184
|
|
|
* |
|
185
|
|
|
* @throws InvalidOptionException When option given doesn't exist |
|
186
|
|
|
* @throws InvalidOptionException When a required value is missing |
|
187
|
|
|
*/ |
|
188
|
|
|
private function addLongOption($name, $value) |
|
189
|
|
|
{ |
|
190
|
|
|
if (!$this->definition->hasOption($name)) { |
|
191
|
|
|
throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name)); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$option = $this->definition->getOption($name); |
|
195
|
|
|
|
|
196
|
|
|
if (null === $value) { |
|
197
|
|
|
if ($option->isValueRequired()) { |
|
198
|
|
|
throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name)); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
$value = $option->isValueOptional() ? $option->getDefault() : true; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
$this->options[$name] = $value; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Adds an argument value. |
|
209
|
|
|
* |
|
210
|
|
|
* @param string $name The argument name |
|
211
|
|
|
* @param mixed $value The value for the argument |
|
212
|
|
|
* |
|
213
|
|
|
* @throws InvalidArgumentException When argument given doesn't exist |
|
214
|
|
|
*/ |
|
215
|
|
View Code Duplication |
private function addArgument($name, $value) |
|
216
|
|
|
{ |
|
217
|
|
|
if (!$this->definition->hasArgument($name)) { |
|
218
|
|
|
throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
$this->arguments[$name] = $value; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|