11
11
12
12
namespace Symfony \Component \Console \Helper ;
13
13
14
- use Symfony \Component \Console \Helper \ Helper ;
14
+ use Symfony \Component \Console \Input \ InputInterface ;
15
15
use Symfony \Component \Console \Output \OutputInterface ;
16
16
use Symfony \Component \Console \Formatter \OutputFormatterStyle ;
17
- use Symfony \Component \Console \Dialog \Question ;
18
- use Symfony \Component \Console \Dialog \ChoiceQuestion ;
17
+ use Symfony \Component \Console \Question \Question ;
18
+ use Symfony \Component \Console \Question \ChoiceQuestion ;
19
19
20
20
/**
21
- * The Question class provides helpers to interact with the user.
21
+ * The QuestionHelper class provides helpers to interact with the user.
22
22
*
23
23
* @author Fabien Potencier <fabien@symfony.com>
24
24
*/
@@ -36,22 +36,27 @@ public function __construct()
36
36
/**
37
37
* Asks a question to the user.
38
38
*
39
- * @param OutputInterface $output An Output instance
39
+ * @param InputInterface $input An InputInterface instance
40
+ * @param OutputInterface $output An OutputInterface instance
40
41
* @param Question $question The question to ask
41
42
*
42
43
* @return string The user answer
43
44
*
44
45
* @throws \RuntimeException If there is no data to read in the input stream
45
46
*/
46
- public function ask (OutputInterface $ output , Question $ question )
47
+ public function ask (InputInterface $ input , OutputInterface $ output , Question $ question )
47
48
{
48
- $ that = $ this ;
49
+ if (!$ input ->isInteractive ()) {
50
+ return $ question ->getDefault ();
51
+ }
49
52
50
53
if (!$ question ->getValidator ()) {
51
- return $ that ->doAsk ($ output , $ question );
54
+ return $ this ->doAsk ($ output , $ question );
52
55
}
53
56
54
- $ interviewer = function () use ($ output , $ question , $ that ) {
57
+ $ that = $ this ;
58
+
59
+ $ interviewer = function () use ($ output , $ question , $ that ) {
55
60
return $ that ->doAsk ($ output , $ question );
56
61
};
57
62
@@ -64,23 +69,48 @@ public function ask(OutputInterface $output, Question $question)
64
69
* This is mainly useful for testing purpose.
65
70
*
66
71
* @param resource $stream The input stream
72
+ *
73
+ * @throws \InvalidArgumentException In case the stream is not a resource
67
74
*/
68
75
public function setInputStream ($ stream )
69
76
{
77
+ if (!is_resource ($ stream )) {
78
+ throw new \InvalidArgumentException ('Input stream must be a valid resource. ' );
79
+ }
80
+
70
81
$ this ->inputStream = $ stream ;
71
82
}
72
83
73
84
/**
74
85
* Returns the helper's input stream
75
86
*
76
- * @return
F438
string
87
+ * @return resource
77
88
*/
78
89
public function getInputStream ()
79
90
{
80
91
return $ this ->inputStream ;
81
92
}
82
93
83
- private function doAsk ($ output , $ question )
94
+ /**
95
+ * {@inheritdoc}
96
+ */
97
+ public function getName ()
98
+ {
99
+ return 'question ' ;
100
+ }
101
+
102
+ /**
103
+ * Asks the question to the user.
104
+ *
105
+ * @param OutputInterface $output
106
+ * @param Question $question
107
+ *
108
+ * @return bool|mixed|null|string
109
+ *
110
+ * @throws \Exception
111
+ * @throws \RuntimeException
112
+ */
113
+ private function doAsk (OutputInterface $ output , Question $ question )
84
114
{
85
115
$ message = $ question ->getQuestion ();
86
116
if ($ question instanceof ChoiceQuestion) {
@@ -98,12 +128,12 @@ private function doAsk($output, $question)
98
128
99
129
$ output ->write ($ message );
100
130
101
- $ autocomplete = $ question ->getAutocompleter ();
131
+ $ autocomplete = $ question ->getAutocompleterValues ();
102
132
if (null === $ autocomplete || !$ this ->hasSttyAvailable ()) {
103
133
$ ret = false ;
104
134
if ($ question ->isHidden ()) {
105
135
try {
106
- $ ret = trim ($ this ->askHiddenResponse ($ output, $ question ));
136
+ $ ret = trim ($ this ->getHiddenResponse ($ output ));
107
137
} catch (\RuntimeException $ e ) {
108
138
if (!$ question ->isHiddenFallback ()) {
109
139
throw $ e ;
@@ -119,7 +149,7 @@ private function doAsk($output, $question)
119
149
$ ret = trim ($ ret );
120
150
}
121
151
} else {
122
- $ ret = $ this ->autocomplete ($ output , $ question );
152
+ $ ret = trim ( $ this ->autocomplete ($ output , $ question) );
123
153
}
124
154
125
155
$ ret = strlen ($ ret ) > 0 ? $ ret : $ question ->getDefault ();
@@ -131,9 +161,17 @@ private function doAsk($output, $question)
131
161
return $ ret ;
132
162
}
133
163
164
+ /**
165
+ * Autocompletes a question.
166
+ *
167
+ * @param OutputInterface $output
168
+ * @param Question $question
169
+ *
170
+ * @return string
171
+ */
134
172
private function autocomplete (OutputInterface $ output , Question $ question )
135
173
{
136
- $ autocomplete = $ question ->getAutocompleter ();
174
+ $ autocomplete = $ question ->getAutocompleterValues ();
137
175
$ ret = '' ;
138
176
139
177
$ i = 0 ;
@@ -241,16 +279,15 @@ private function autocomplete(OutputInterface $output, Question $question)
241
279
}
242
280
243
281
/**
244
- * Asks a question to the user, the response is hidden
282
+ * Gets a hidden response from user.
245
283
*
246
284
* @param OutputInterface $output An Output instance
247
- * @param string|array $question The question
248
285
*
249
286
* @return string The answer
250
287
*
251
- * @throws \RuntimeException In case the fallback is deactivated and the response can not be hidden
288
+ * @throws \RuntimeException In case the fallback is deactivated and the response cannot be hidden
252
289
*/
253
- private function askHiddenResponse (OutputInterface $ output, Question $ question )
290
+ private function
F438
getHiddenResponse (OutputInterface $ output )
254
291
{
255
292
if (defined ('PHP_WINDOWS_VERSION_BUILD ' )) {
256
293
$ exe = __DIR__ .'/../Resources/bin/hiddeninput.exe ' ;
@@ -298,7 +335,7 @@ private function askHiddenResponse(OutputInterface $output, Question $question)
298
335
return $ value ;
299
336
}
300
337
301
- throw new \RuntimeException ('Unable to hide the response ' );
338
+ throw new \RuntimeException ('Unable to hide the response. ' );
302
339
}
303
340
304
341
/**
@@ -315,8 +352,8 @@ private function askHiddenResponse(OutputInterface $output, Question $question)
315
352
private function validateAttempts ($ interviewer , OutputInterface $ output , Question $ question )
316
353
{
317
354
$ error = null ;
318
- $ attempts = $ question ->getMaxAttemps ();
319
- while (false === $ attempts || $ attempts --) {
355
+ $ attempts = $ question ->getMaxAttempts ();
356
+ while (null === $ attempts || $ attempts --) {
320
357
if (null !== $ error ) {
321
358
$ output ->writeln ($ this ->getHelperSet ()->get ('formatter ' )->formatBlock ($ error ->getMessage (), 'error ' ));
322
359
}
@@ -331,7 +368,7 @@ private function validateAttempts($interviewer, OutputInterface $output, Questio
331
368
}
332
369
333
370
/**
334
- * Return a valid unix shell
371
+ * Returns a valid unix shell.
335
372
*
336
373
* @return string|Boolean The valid shell name, false in case no valid shell is found
337
374
*/
@@ -357,6 +394,11 @@ private function getShell()
357
394
return self ::$ shell ;
358
395
}
359
396
397
+ /**
398
+ * Returns whether Stty is available or not.
399
+ *
400
+ * @return Boolean
401
+ */
360
402
private function hasSttyAvailable ()
361
403
{
362
404
if (null !== self ::$ stty ) {
@@ -367,12 +409,4 @@ private function hasSttyAvailable()
367
409
368
410
return self ::$ stty = $ exitcode === 0 ;
369
411
}
370
-
371
- /**
372
- * {@inheritDoc}
373
- */
374
- public function getName ()
375
- {
376
- return 'question ' ;
377
- }
378
412
}
0 commit comments