11
11
12
12
namespace Symfony \Component \Console \Helper ;
13
13
14
+ use Symfony \Component \Console \Exception \MissingInputException ;
14
15
use Symfony \Component \Console \Exception \RuntimeException ;
15
16
use Symfony \Component \Console \Formatter \OutputFormatter ;
16
17
use Symfony \Component \Console \Formatter \OutputFormatterStyle ;
@@ -48,44 +49,32 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
48
49
}
49
50
50
51
if (!$ input ->isInteractive ()) {
51
- $ default = $ question ->getDefault ();
52
-
53
- if (null === $ default ) {
54
- return $ default ;
55
- }
56
-
57
- if ($ validator = $ question ->getValidator ()) {
58
- return \call_user_func ($ question ->getValidator (), $ default );
59
- } elseif ($ question instanceof ChoiceQuestion) {
60
- $ choices = $ question ->getChoices ();
61
-
62
- if (!$ question ->isMultiselect ()) {
63
- return isset ($ choices [$ default ]) ? $ choices [$ default ] : $ default ;
64
- }
65
-
66
- $ default = explode (', ' , $ default );
67
- foreach ($ default as $ k => $ v ) {
68
- $ v = $ question ->isTrimmable () ? trim ($ v ) : $ v ;
69
- $ default [$ k ] = isset ($ choices [$ v ]) ? $ choices [$ v ] : $ v ;
70
- }
71
- }
72
-
73
- return $ default ;
52
+ return $ this ->getDefaultAnswer ($ question );
74
53
}
75
54
76
55
if ($ input instanceof StreamableInputInterface && $ stream = $ input ->getStream ()) {
77
56
$ this ->inputStream = $ stream ;
78
57
}
79
58
80
- if (!$ question ->getValidator ()) {
81
- return $ this ->doAsk ($ output , $ question );
82
- }
59
+ try {
60
+ if (!$ question ->getValidator ()) {
61
+ return $ this ->doAsk ($ output , $ question );
62
+ }
83
63
84
- $ interviewer = function () use ($ output , $ question ) {
85
- return $ this ->doAsk ($ output , $ question );
86
- };
64
+ $ interviewer = function () use ($ output , $ question ) {
65
+ return $ this ->doAsk ($ output , $ question );
66
+ };
87
67
88
- return $ this ->validateAttempts ($ interviewer , $ output , $ question );
68
+ return $ this ->validateAttempts ($ interviewer , $ output , $ question );
69
+ } catch (MissingInputException $ exception ) {
70
+ $ input ->setInteractive (false );
71
+
72
+ if (null === $ fallbackOutput = $ this ->getDefaultAnswer ($ question )) {
73
+ throw $ exception ;
74
+ }
75
+
76
+ return $ fallbackOutput ;
77
+ }
89
78
}
90
79
91
80
/**
@@ -134,7 +123,7 @@ private function doAsk(OutputInterface $output, Question $question)
134
123
if (false === $ ret ) {
135
124
$ ret = fgets ($ inputStream , 4096 );
136
125
if (false === $ ret ) {
137
- throw new RuntimeException ('Aborted. ' );
126
+ throw new MissingInputException ('Aborted. ' );
138
127
}
139
128
if ($ question ->isTrimmable ()) {
140
129
$ ret = trim ($ ret );
@@ -158,6 +147,36 @@ private function doAsk(OutputInterface $output, Question $question)
158
147
return $ ret ;
159
148
}
160
149
150
+ /**
151
+ * @return mixed
152
+ */
153
+ private function getDefaultAnswer (Question $ question )
154
+ {
155
+ $ default = $ question ->getDefault ();
156
+
157
+ if (null === $ default ) {
158
+ return $ default ;
159
+ }
160
+
161
+ if ($ validator = $ question ->getValidator ()) {
162
+ return \call_user_func ($ question ->getValidator (), $ default );
163
+ } elseif ($ question instanceof ChoiceQuestion) {
164
+ $ choices = $ question ->getChoices ();
165
+
166
+ if (!$ question ->isMultiselect ()) {
167
+ return isset ($ choices [$ default ]) ? $ choices [$ default ] : $ default ;
168
+ }
169
+
170
+ $ default = explode (', ' , $ default );
171
+ foreach ($ default as $ k => $ v ) {
172
+ $ v = $ question ->isTrimmable () ? trim ($ v ) : $ v ;
173
+ $ default [$ k ] = isset ($ choices [$ v ]) ? $ choices [$ v ] : $ v ;
174
+ }
175
+ }
176
+
177
+ return $ default ;
178
+ }
179
+
161
180
/**
162
181
* Outputs the question prompt.
163
182
*/
@@ -240,7 +259,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
240
259
// as opposed to fgets(), fread() returns an empty string when the stream content is empty, not false.
241
260
if (false === $ c || ('' === $ ret && '' === $ c && null === $ question ->getDefault ())) {
242
261
shell_exec (sprintf ('stty %s ' , $ sttyMode ));
243
- throw new RuntimeException ('Aborted. ' );
262
+ throw new MissingInputException ('Aborted. ' );
244
263
} elseif ("\177" === $ c ) { // Backspace Character
245
264
if (0 === $ numMatches && 0 !== $ i ) {
246
265
--$ i ;
@@ -406,7 +425,7 @@ private function getHiddenResponse(OutputInterface $output, $inputStream, bool $
406
425
shell_exec (sprintf ('stty %s ' , $ sttyMode ));
407
426
408
427
if (false === $ value ) {
409
- throw new RuntimeException ('Aborted. ' );
428
+ throw new MissingInputException ('Aborted. ' );
410
429
}
411
430
if ($ trimmable ) {
412
431
$ value = trim ($ value );
0 commit comments