@@ -28,11 +28,6 @@ class QuestionHelper extends Helper
28
28
private static $ shell ;
29
29
private static $ stty ;
30
30
31
- public function __construct ()
32
- {
33
- $ this ->inputStream = STDIN ;
34
- }
35
-
36
31
/**
37
32
* Asks a question to the user.
38
33
*
@@ -114,6 +109,8 @@ public function getName()
114
109
*/
115
110
public function doAsk (OutputInterface $ output , Question $ question )
116
111
{
112
+ $ inputStream = $ this ->inputStream ?: STDIN ;
113
+
117
114
$ message = $ question ->getQuestion ();
118
115
if ($ question instanceof ChoiceQuestion) {
119
116
$ width = max (array_map ('strlen ' , array_keys ($ question ->getChoices ())));
@@ -135,7 +132,7 @@ public function doAsk(OutputInterface $output, Question $question)
135
132
$ ret = false ;
136
133
if ($ question ->isHidden ()) {
137
134
try {
138
- $ ret = trim ($ this ->getHiddenResponse ($ output ));
135
+ $ ret = trim ($ this ->getHiddenResponse ($ output, $ inputStream ));
139
136
} catch (\RuntimeException $ e ) {
140
137
if (!$ question ->isHiddenFallback ()) {
141
138
throw $ e ;
@@ -144,14 +141,14 @@ public function doAsk(OutputInterface $output, Question $question)
144
141
}
145
142
146
143
if (false === $ ret ) {
147
- $ ret = fgets ($ this -> inputStream , 4096 );
144
+ $ ret = fgets ($ inputStream , 4096 );
148
145
if (false === $ ret ) {
149
146
throw new \RuntimeException ('Aborted ' );
150
147
}
151
148
$ ret = trim ($ ret );
152
149
}
153
150
} else {
154
- $ ret = trim ($ this ->autocomplete ($ output , $ question ));
151
+ $ ret = trim ($ this ->autocomplete ($ output , $ question, $ inputStream ));
155
152
}
156
153
157
154
$ ret = strlen ($ ret ) > 0 ? $ ret : $ question ->getDefault ();
@@ -171,7 +168,7 @@ public function doAsk(OutputInterface $output, Question $question)
171
168
*
172
169
* @return string
173
170
*/
174
- private function autocomplete (OutputInterface $ output , Question $ question )
171
+ private function autocomplete (OutputInterface $ output , Question $ question, $ inputStream )
175
172
{
176
173
$ autocomplete = $ question ->getAutocompleterValues ();
177
174
$ ret = '' ;
@@ -190,8 +187,8 @@ private function autocomplete(OutputInterface $output, Question $question)
190
187
$ output ->getFormatter ()->setStyle ('hl ' , new OutputFormatterStyle ('black ' , 'white ' ));
191
188
192
189
// Read a keypress
193
- while (!feof ($ this -> inputStream )) {
194
- $ c = fread ($ this -> inputStream , 1 );
190
+ while (!feof ($ inputStream )) {
191
+ $ c = fread ($ inputStream , 1 );
195
192
196
193
// Backspace Character
197
194
if ("\177" === $ c ) {
@@ -212,7 +209,7 @@ private function autocomplete(OutputInterface $output, Question $question)
212
209
// Pop the last character off the end of our string
213
210
$ ret = substr ($ ret , 0 , $ i );
214
211
} elseif ("\033" === $ c ) { // Did we read an escape sequence?
215
- $ c .= fread ($ this -> inputStream , 2 );
212
+ $ c .= fread ($ inputStream , 2 );
216
213
217
214
// A = Up Arrow. B = Down Arrow
218
215
if ('A ' === $ c [2 ] || 'B ' === $ c [2 ]) {
@@ -289,7 +286,7 @@ private function autocomplete(OutputInterface $output, Question $question)
289
286
*
290
287
* @throws \RuntimeException In case the fallback is deactivated and the response cannot be hidden
291
288
*/
292
- private function getHiddenResponse (OutputInterface $ output )
289
+ private function getHiddenResponse (OutputInterface $ output, $ inputStream )
293
290
{
294
291
if (defined ('PHP_WINDOWS_VERSION_BUILD ' )) {
295
292
$ exe = __DIR__ .'/../Resources/bin/hiddeninput.exe ' ;
@@ -315,7 +312,7 @@ private function getHiddenResponse(OutputInterface $output)
315
312
$ sttyMode = shell_exec ('stty -g ' );
316
313
317
314
shell_exec ('stty -echo ' );
318
- $ value = fgets ($ this -> inputStream , 4096 );
315
+ $ value = fgets ($ inputStream , 4096 );
319
316
shell_exec (sprintf ('stty %s ' , $ sttyMode ));
320
317
321
318
if (false === $ value ) {
0 commit comments