1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the webmozart/console package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[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 Webmozart\Console\Adapter; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Webmozart\Console\Api\Args\Args; |
17
|
|
|
use Webmozart\Console\Api\Args\RawArgs; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Adapts an {@link Args} instance to Symfony's {@link InputInterface} API. |
21
|
|
|
* |
22
|
|
|
* @since 1.0 |
23
|
|
|
* |
24
|
|
|
* @author Bernhard Schussek <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ArgsInput implements InputInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var RawArgs |
30
|
|
|
*/ |
31
|
|
|
private $rawArgs; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Args |
35
|
|
|
*/ |
36
|
|
|
private $args; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
private $interactive = true; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Creates the adapter. |
45
|
|
|
* |
46
|
|
|
* @param RawArgs $rawArgs The unparsed console arguments. |
47
|
|
|
* @param Args $args The parsed console arguments. |
|
|
|
|
48
|
|
|
*/ |
49
|
16 |
|
public function __construct(RawArgs $rawArgs, Args $args = null) |
50
|
|
|
{ |
51
|
16 |
|
$this->rawArgs = $rawArgs; |
52
|
16 |
|
$this->args = $args; |
53
|
16 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return RawArgs |
57
|
|
|
*/ |
58
|
2 |
|
public function getRawArgs() |
59
|
|
|
{ |
60
|
2 |
|
return $this->rawArgs; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return Args |
65
|
|
|
*/ |
66
|
2 |
|
public function getArgs() |
67
|
|
|
{ |
68
|
2 |
|
return $this->args; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
2 |
|
public function getFirstArgument() |
75
|
|
|
{ |
76
|
2 |
|
$tokens = $this->rawArgs->getTokens(); |
77
|
|
|
|
78
|
2 |
|
return count($tokens) > 0 ? reset($tokens) : null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
2 |
|
public function hasParameterOption($values, $onlyParams = false) |
85
|
|
|
{ |
86
|
2 |
|
$tokens = $this->rawArgs->getTokens(); |
87
|
|
|
|
88
|
2 |
|
foreach ((array) $values as $value) { |
89
|
2 |
|
foreach ($tokens as $token) { |
90
|
|
|
// end of options (--) signal reached, stop now |
91
|
2 |
|
if ($onlyParams && $token === '--') { |
92
|
2 |
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
if ($token === $value || 0 === strpos($token, $value.'=')) { |
96
|
2 |
|
return true; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
1 |
|
public function getParameterOption($values, $default = false, $onlyParams = false) |
110
|
|
|
{ |
111
|
1 |
|
$tokens = $this->rawArgs->getTokens(); |
112
|
|
|
|
113
|
1 |
|
foreach ((array) $values as $value) { |
114
|
1 |
|
for (reset($tokens); null !== key($tokens); next($tokens)) { |
115
|
1 |
|
$token = current($tokens); |
116
|
|
|
|
117
|
1 |
|
if ($onlyParams && ($token === '--')) { |
118
|
|
|
// end of options (--) signal reached, stop now |
119
|
1 |
|
return $default; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// Long/short option with value in the next argument |
123
|
1 |
|
if ($token === $value) { |
124
|
1 |
|
$next = next($tokens); |
125
|
|
|
|
126
|
1 |
|
return ($next && ($next !== '--')) ? $next : null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Long option with = |
130
|
1 |
|
if (0 === strpos($token, $value.'=')) { |
131
|
1 |
|
return substr($token, strlen($value) + 1); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Short option |
135
|
1 |
|
if (strlen($token) > 2 && '-' === $token[0] && '-' !== $token[1] && 0 === strpos($token, $value)) { |
136
|
1 |
|
return substr($token, 2); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $default; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function bind(InputDefinition $definition) |
148
|
|
|
{ |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
|
|
public function validate() |
155
|
|
|
{ |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
1 |
|
public function getArguments() |
162
|
|
|
{ |
163
|
1 |
|
return $this->args ? $this->args->getArguments() : array(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
2 |
|
public function getArgument($name) |
170
|
|
|
{ |
171
|
2 |
|
return $this->args ? $this->args->getArgument($name) : null; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritdoc} |
176
|
|
|
*/ |
177
|
1 |
|
public function setArgument($name, $value) |
178
|
|
|
{ |
179
|
1 |
|
if ($this->args) { |
180
|
1 |
|
$this->args->setArgument($name, $value); |
181
|
|
|
} |
182
|
1 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* {@inheritdoc} |
186
|
|
|
*/ |
187
|
1 |
|
public function hasArgument($name) |
188
|
|
|
{ |
189
|
1 |
|
return $this->args ? $this->args->isArgumentDefined($name) : false; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* {@inheritdoc} |
194
|
|
|
*/ |
195
|
1 |
|
public function getOptions() |
196
|
|
|
{ |
197
|
1 |
|
return $this->args ? $this->args->getOptions() : array(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritdoc} |
202
|
|
|
*/ |
203
|
2 |
|
public function getOption($name) |
204
|
|
|
{ |
205
|
2 |
|
return $this->args ? $this->args->getOption($name) : null; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@inheritdoc} |
210
|
|
|
*/ |
211
|
1 |
|
public function setOption($name, $value) |
212
|
|
|
{ |
213
|
1 |
|
if ($this->args) { |
214
|
1 |
|
$this->args->setOption($name, $value); |
215
|
|
|
} |
216
|
1 |
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* {@inheritdoc} |
220
|
|
|
*/ |
221
|
1 |
|
public function hasOption($name) |
222
|
|
|
{ |
223
|
1 |
|
return $this->args ? $this->args->isOptionDefined($name) : false; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* {@inheritdoc} |
228
|
|
|
*/ |
229
|
1 |
|
public function isInteractive() |
230
|
|
|
{ |
231
|
1 |
|
return $this->interactive; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* {@inheritdoc} |
236
|
|
|
*/ |
237
|
1 |
|
public function setInteractive($interactive) |
238
|
|
|
{ |
239
|
1 |
|
$this->interactive = $interactive; |
240
|
1 |
|
} |
241
|
|
|
} |
242
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.