4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Helper;
14 use Symfony\Component\Console\Exception\InvalidArgumentException;
15 use Symfony\Component\Console\Exception\RuntimeException;
16 use Symfony\Component\Console\Formatter\OutputFormatter;
17 use Symfony\Component\Console\Formatter\OutputFormatterStyle;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Input\StreamableInputInterface;
20 use Symfony\Component\Console\Output\ConsoleOutputInterface;
21 use Symfony\Component\Console\Output\OutputInterface;
22 use Symfony\Component\Console\Question\Question;
23 use Symfony\Component\Console\Question\ChoiceQuestion;
26 * The QuestionHelper class provides helpers to interact with the user.
28 * @author Fabien Potencier <fabien@symfony.com>
30 class QuestionHelper extends Helper
33 private static $shell;
37 * Asks a question to the user.
39 * @return mixed The user answer
41 * @throws RuntimeException If there is no data to read in the input stream
43 public function ask(InputInterface $input, OutputInterface $output, Question $question)
45 if ($output instanceof ConsoleOutputInterface) {
46 $output = $output->getErrorOutput();
49 if (!$input->isInteractive()) {
50 if ($question instanceof ChoiceQuestion) {
51 $choices = $question->getChoices();
53 return $choices[$question->getDefault()];
56 return $question->getDefault();
59 if ($input instanceof StreamableInputInterface && $stream = $input->getStream()) {
60 $this->inputStream = $stream;
63 if (!$question->getValidator()) {
64 return $this->doAsk($output, $question);
67 $interviewer = function () use ($output, $question) {
68 return $this->doAsk($output, $question);
71 return $this->validateAttempts($interviewer, $output, $question);
75 * Sets the input stream to read from when interacting with the user.
77 * This is mainly useful for testing purpose.
79 * @deprecated since version 3.2, to be removed in 4.0. Use
80 * StreamableInputInterface::setStream() instead.
82 * @param resource $stream The input stream
84 * @throws InvalidArgumentException In case the stream is not a resource
86 public function setInputStream($stream)
88 @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.2 and will be removed in 4.0. Use %s::setStream() instead.', __METHOD__, StreamableInputInterface::class), E_USER_DEPRECATED);
90 if (!is_resource($stream)) {
91 throw new InvalidArgumentException('Input stream must be a valid resource.');
94 $this->inputStream = $stream;
98 * Returns the helper's input stream.
100 * @deprecated since version 3.2, to be removed in 4.0. Use
101 * StreamableInputInterface::getStream() instead.
105 public function getInputStream()
107 if (0 === func_num_args() || func_get_arg(0)) {
108 @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.2 and will be removed in 4.0. Use %s::getStream() instead.', __METHOD__, StreamableInputInterface::class), E_USER_DEPRECATED);
111 return $this->inputStream;
117 public function getName()
123 * Prevents usage of stty.
125 public static function disableStty()
131 * Asks the question to the user.
133 * @return bool|mixed|null|string
135 * @throws RuntimeException In case the fallback is deactivated and the response cannot be hidden
137 private function doAsk(OutputInterface $output, Question $question)
139 $this->writePrompt($output, $question);
141 $inputStream = $this->inputStream ?: STDIN;
142 $autocomplete = $question->getAutocompleterValues();
144 if (null === $autocomplete || !$this->hasSttyAvailable()) {
146 if ($question->isHidden()) {
148 $ret = trim($this->getHiddenResponse($output, $inputStream));
149 } catch (RuntimeException $e) {
150 if (!$question->isHiddenFallback()) {
156 if (false === $ret) {
157 $ret = fgets($inputStream, 4096);
158 if (false === $ret) {
159 throw new RuntimeException('Aborted');
164 $ret = trim($this->autocomplete($output, $question, $inputStream, is_array($autocomplete) ? $autocomplete : iterator_to_array($autocomplete, false)));
167 $ret = strlen($ret) > 0 ? $ret : $question->getDefault();
169 if ($normalizer = $question->getNormalizer()) {
170 return $normalizer($ret);
177 * Outputs the question prompt.
179 protected function writePrompt(OutputInterface $output, Question $question)
181 $message = $question->getQuestion();
183 if ($question instanceof ChoiceQuestion) {
184 $maxWidth = max(array_map(array($this, 'strlen'), array_keys($question->getChoices())));
186 $messages = (array) $question->getQuestion();
187 foreach ($question->getChoices() as $key => $value) {
188 $width = $maxWidth - $this->strlen($key);
189 $messages[] = ' [<info>'.$key.str_repeat(' ', $width).'</info>] '.$value;
192 $output->writeln($messages);
194 $message = $question->getPrompt();
197 $output->write($message);
201 * Outputs an error message.
203 protected function writeError(OutputInterface $output, \Exception $error)
205 if (null !== $this->getHelperSet() && $this->getHelperSet()->has('formatter')) {
206 $message = $this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error');
208 $message = '<error>'.$error->getMessage().'</error>';
211 $output->writeln($message);
215 * Autocompletes a question.
217 * @param OutputInterface $output
218 * @param Question $question
219 * @param resource $inputStream
220 * @param array $autocomplete
224 private function autocomplete(OutputInterface $output, Question $question, $inputStream, array $autocomplete)
230 $matches = $autocomplete;
231 $numMatches = count($matches);
233 $sttyMode = shell_exec('stty -g');
235 // Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
236 shell_exec('stty -icanon -echo');
238 // Add highlighted text style
239 $output->getFormatter()->setStyle('hl', new OutputFormatterStyle('black', 'white'));
242 while (!feof($inputStream)) {
243 $c = fread($inputStream, 1);
245 // Backspace Character
247 if (0 === $numMatches && 0 !== $i) {
249 // Move cursor backwards
250 $output->write("\033[1D");
255 $matches = $autocomplete;
256 $numMatches = count($matches);
261 // Pop the last character off the end of our string
262 $ret = substr($ret, 0, $i);
263 } elseif ("\033" === $c) {
264 // Did we read an escape sequence?
265 $c .= fread($inputStream, 2);
267 // A = Up Arrow. B = Down Arrow
268 if (isset($c[2]) && ('A' === $c[2] || 'B' === $c[2])) {
269 if ('A' === $c[2] && -1 === $ofs) {
273 if (0 === $numMatches) {
277 $ofs += ('A' === $c[2]) ? -1 : 1;
278 $ofs = ($numMatches + $ofs) % $numMatches;
280 } elseif (ord($c) < 32) {
281 if ("\t" === $c || "\n" === $c) {
282 if ($numMatches > 0 && -1 !== $ofs) {
283 $ret = $matches[$ofs];
284 // Echo out remaining chars for current match
285 $output->write(substr($ret, $i));
306 foreach ($autocomplete as $value) {
307 // If typed characters match the beginning chunk of value (e.g. [AcmeDe]moBundle)
308 if (0 === strpos($value, $ret)) {
309 $matches[$numMatches++] = $value;
314 // Erase characters from cursor to end of line
315 $output->write("\033[K");
317 if ($numMatches > 0 && -1 !== $ofs) {
318 // Save cursor position
319 $output->write("\0337");
320 // Write highlighted text
321 $output->write('<hl>'.OutputFormatter::escapeTrailingBackslash(substr($matches[$ofs], $i)).'</hl>');
322 // Restore cursor position
323 $output->write("\0338");
327 // Reset stty so it behaves normally again
328 shell_exec(sprintf('stty %s', $sttyMode));
334 * Gets a hidden response from user.
336 * @param OutputInterface $output An Output instance
337 * @param resource $inputStream The handler resource
339 * @return string The answer
341 * @throws RuntimeException In case the fallback is deactivated and the response cannot be hidden
343 private function getHiddenResponse(OutputInterface $output, $inputStream)
345 if ('\\' === DIRECTORY_SEPARATOR) {
346 $exe = __DIR__.'/../Resources/bin/hiddeninput.exe';
348 // handle code running from a phar
349 if ('phar:' === substr(__FILE__, 0, 5)) {
350 $tmpExe = sys_get_temp_dir().'/hiddeninput.exe';
355 $value = rtrim(shell_exec($exe));
356 $output->writeln('');
358 if (isset($tmpExe)) {
365 if ($this->hasSttyAvailable()) {
366 $sttyMode = shell_exec('stty -g');
368 shell_exec('stty -echo');
369 $value = fgets($inputStream, 4096);
370 shell_exec(sprintf('stty %s', $sttyMode));
372 if (false === $value) {
373 throw new RuntimeException('Aborted');
376 $value = trim($value);
377 $output->writeln('');
382 if (false !== $shell = $this->getShell()) {
383 $readCmd = 'csh' === $shell ? 'set mypassword = $<' : 'read -r mypassword';
384 $command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd);
385 $value = rtrim(shell_exec($command));
386 $output->writeln('');
391 throw new RuntimeException('Unable to hide the response.');
395 * Validates an attempt.
397 * @param callable $interviewer A callable that will ask for a question and return the result
398 * @param OutputInterface $output An Output instance
399 * @param Question $question A Question instance
401 * @return mixed The validated response
403 * @throws \Exception In case the max number of attempts has been reached and no valid response has been given
405 private function validateAttempts(callable $interviewer, OutputInterface $output, Question $question)
408 $attempts = $question->getMaxAttempts();
409 while (null === $attempts || $attempts--) {
410 if (null !== $error) {
411 $this->writeError($output, $error);
415 return call_user_func($question->getValidator(), $interviewer());
416 } catch (RuntimeException $e) {
418 } catch (\Exception $error) {
426 * Returns a valid unix shell.
428 * @return string|bool The valid shell name, false in case no valid shell is found
430 private function getShell()
432 if (null !== self::$shell) {
436 self::$shell = false;
438 if (file_exists('/usr/bin/env')) {
439 // handle other OSs with bash/zsh/ksh/csh if available to hide the answer
440 $test = "/usr/bin/env %s -c 'echo OK' 2> /dev/null";
441 foreach (array('bash', 'zsh', 'ksh', 'csh') as $sh) {
442 if ('OK' === rtrim(shell_exec(sprintf($test, $sh)))) {
453 * Returns whether Stty is available or not.
457 private function hasSttyAvailable()
459 if (null !== self::$stty) {
463 exec('stty 2>&1', $output, $exitcode);
465 return self::$stty = 0 === $exitcode;