1111
1212namespace Symfony \Component \Console \Helper ;
1313
14- use Symfony \Component \Console \Helper \ Helper ;
14+ use Symfony \Component \Console \Input \ InputInterface ;
1515use Symfony \Component \Console \Output \OutputInterface ;
1616use 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 ;
1919
2020/**
21- * The Question class provides helpers to interact with the user.
21+ * The QuestionHelper class provides helpers to interact with the user.
2222 *
2323 * @author Fabien Potencier <[email protected] > 2424 */
@@ -36,22 +36,27 @@ public function __construct()
3636 /**
3737 * Asks a question to the user.
3838 *
39- * @param OutputInterface $output An Output instance
39+ * @param InputInterface $input An InputInterface instance
40+ * @param OutputInterface $output An OutputInterface instance
4041 * @param Question $question The question to ask
4142 *
4243 * @return string The user answer
4344 *
4445 * @throws \RuntimeException If there is no data to read in the input stream
4546 */
46- public function ask (OutputInterface $ output , Question $ question )
47+ public function ask (InputInterface $ input , OutputInterface $ output , Question $ question )
4748 {
48- $ that = $ this ;
49+ if (!$ input ->isInteractive ()) {
50+ return $ question ->getDefault ();
51+ }
4952
5053 if (!$ question ->getValidator ()) {
51- return $ that ->doAsk ($ output , $ question );
54+ return $ this ->doAsk ($ output , $ question );
5255 }
5356
54- $ interviewer = function () use ($ output , $ question , $ that ) {
57+ $ that = $ this ;
58+
59+ $ interviewer = function () use ($ output , $ question , $ that ) {
5560 return $ that ->doAsk ($ output , $ question );
5661 };
5762
@@ -64,23 +69,48 @@ public function ask(OutputInterface $output, Question $question)
6469 * This is mainly useful for testing purpose.
6570 *
6671 * @param resource $stream The input stream
72+ *
73+ * @throws \InvalidArgumentException In case the stream is not a resource
6774 */
6875 public function setInputStream ($ stream )
6976 {
77+ if (!is_resource ($ stream )) {
78+ throw new \InvalidArgumentException ('Input stream must be a valid resource. ' );
79+ }
80+
7081 $ this ->inputStream = $ stream ;
7182 }
7283
7384 /**
7485 * Returns the helper's input stream
7586 *
76- * @return string
87+ * @return resource
7788 */
7889 public function getInputStream ()
7990 {
8091 return $ this ->inputStream ;
8192 }
8293
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 )
84114 {
85115 $ message = $ question ->getQuestion ();
86116 if ($ question instanceof ChoiceQuestion) {
@@ -98,12 +128,12 @@ private function doAsk($output, $question)
98128
99129 $ output ->write ($ message );
100130
101- $ autocomplete = $ question ->getAutocompleter ();
131+ $ autocomplete = $ question ->getAutocompleterValues ();
102132 if (null === $ autocomplete || !$ this ->hasSttyAvailable ()) {
103133 $ ret = false ;
104134 if ($ question ->isHidden ()) {
105135 try {
106- $ ret = trim ($ this ->askHiddenResponse ($ output, $ question ));
136+ $ ret = trim ($ this ->getHiddenResponse ($ output ));
107137 } catch (\RuntimeException $ e ) {
108138 if (!$ question ->isHiddenFallback ()) {
109139 throw $ e ;
@@ -119,7 +149,7 @@ private function doAsk($output, $question)
119149 $ ret = trim ($ ret );
120150 }
121151 } else {
122- $ ret = $ this ->autocomplete ($ output , $ question );
152+ $ ret = trim ( $ this ->autocomplete ($ output , $ question) );
123153 }
124154
125155 $ ret = strlen ($ ret ) > 0 ? $ ret : $ question ->getDefault ();
@@ -131,9 +161,17 @@ private function doAsk($output, $question)
131161 return $ ret ;
132162 }
133163
164+ /**
165+ * Autocompletes a question.
166+ *
167+ * @param OutputInterface $output
168+ * @param Question $question
169+ *
170+ * @return string
171+ */
134172 private function autocomplete (OutputInterface $ output , Question $ question )
135173 {
136- $ autocomplete = $ question ->getAutocompleter ();
174+ $ autocomplete = $ question ->getAutocompleterValues ();
137175 $ ret = '' ;
138176
139177 $ i = 0 ;
@@ -241,16 +279,15 @@ private function autocomplete(OutputInterface $output, Question $question)
241279 }
242280
243281 /**
244- * Asks a question to the user, the response is hidden
282+ * Gets a hidden response from user.
245283 *
246284 * @param OutputInterface $output An Output instance
247- * @param string|array $question The question
248285 *
249286 * @return string The answer
250287 *
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
252289 */
253- private function askHiddenResponse (OutputInterface $ output, Question $ question )
290+ private function getHiddenResponse (OutputInterface $ output )
254291 {
255292 if (defined ('PHP_WINDOWS_VERSION_BUILD ' )) {
256293 $ exe = __DIR__ .'/../Resources/bin/hiddeninput.exe ' ;
@@ -298,7 +335,7 @@ private function askHiddenResponse(OutputInterface $output, Question $question)
298335 return $ value ;
299336 }
300337
301- throw new \RuntimeException ('Unable to hide the response ' );
338+ throw new \RuntimeException ('Unable to hide the response. ' );
302339 }
303340
304341 /**
@@ -315,8 +352,8 @@ private function askHiddenResponse(OutputInterface $output, Question $question)
315352 private function validateAttempts ($ interviewer , OutputInterface $ output , Question $ question )
316353 {
317354 $ error = null ;
318- $ attempts = $ question ->getMaxAttemps ();
319- while (false === $ attempts || $ attempts --) {
355+ $ attempts = $ question ->getMaxAttempts ();
356+ while (null === $ attempts || $ attempts --) {
320357 if (null !== $ error ) {
321358 $ output ->writeln ($ this ->getHelperSet ()->get ('formatter ' )->formatBlock ($ error ->getMessage (), 'error ' ));
322359 }
@@ -331,7 +368,7 @@ private function validateAttempts($interviewer, OutputInterface $output, Questio
331368 }
332369
333370 /**
334- * Return a valid unix shell
371+ * Returns a valid unix shell.
335372 *
336373 * @return string|Boolean The valid shell name, false in case no valid shell is found
337374 */
@@ -357,6 +394,11 @@ private function getShell()
357394 return self ::$ shell ;
358395 }
359396
397+ /**
398+ * Returns whether Stty is available or not.
399+ *
400+ * @return Boolean
401+ */
360402 private function hasSttyAvailable ()
361403 {
362404 if (null !== self ::$ stty ) {
@@ -367,12 +409,4 @@ private function hasSttyAvailable()
367409
368410 return self ::$ stty = $ exitcode === 0 ;
369411 }
370-
371- /**
372- * {@inheritDoc}
373- */
374- public function getName ()
375- {
376- return 'question ' ;
377- }
378412}
0 commit comments