1111
1212namespace Symfony \Component \Console \Helper ;
1313
14+ use Symfony \Component \Console \Exception \MissingInputException ;
1415use Symfony \Component \Console \Exception \RuntimeException ;
1516use Symfony \Component \Console \Formatter \OutputFormatter ;
1617use Symfony \Component \Console \Formatter \OutputFormatterStyle ;
@@ -48,44 +49,32 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
4849 }
4950
5051if (!$ 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 ($ questioninstanceof 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 ($ defaultas $ 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 );
7453 }
7554
7655if ($ inputinstanceof StreamableInputInterface &&$ stream =$ input ->getStream ()) {
7756$ this ->inputStream =$ stream ;
7857 }
7958
80- if (!$ question ->getValidator ()) {
81- return $ this ->doAsk ($ output ,$ question );
82- }
59+ try {
60+ if (!$ question ->getValidator ()) {
61+ return $ this ->doAsk ($ output ,$ question );
62+ }
8363
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+ };
8767
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+ }
8978 }
9079
9180/**
@@ -134,7 +123,7 @@ private function doAsk(OutputInterface $output, Question $question)
134123if (false ===$ ret ) {
135124$ ret =fgets ($ inputStream ,4096 );
136125if (false ===$ ret ) {
137- throw new RuntimeException ('Aborted. ' );
126+ throw new MissingInputException ('Aborted. ' );
138127 }
139128if ($ question ->isTrimmable ()) {
140129$ ret =trim ($ ret );
@@ -158,6 +147,36 @@ private function doAsk(OutputInterface $output, Question $question)
158147return $ ret ;
159148 }
160149
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 ($ questioninstanceof 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 ($ defaultas $ 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+
161180/**
162181 * Outputs the question prompt.
163182 */
@@ -240,7 +259,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
240259// as opposed to fgets(), fread() returns an empty string when the stream content is empty, not false.
241260if (false ===$ c || ('' ===$ ret &&'' ===$ c &&null ===$ question ->getDefault ())) {
242261shell_exec (sprintf ('stty %s ' ,$ sttyMode ));
243- throw new RuntimeException ('Aborted. ' );
262+ throw new MissingInputException ('Aborted. ' );
244263 }elseif ("\177" ===$ c ) {// Backspace Character
245264if (0 ===$ numMatches &&0 !==$ i ) {
246265 --$ i ;
@@ -406,7 +425,7 @@ private function getHiddenResponse(OutputInterface $output, $inputStream, bool $
406425shell_exec (sprintf ('stty %s ' ,$ sttyMode ));
407426
408427if (false ===$ value ) {
409- throw new RuntimeException ('Aborted. ' );
428+ throw new MissingInputException ('Aborted. ' );
410429 }
411430if ($ trimmable ) {
412431$ value =trim ($ value );