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\Output\OutputInterface;
17 * The Dialog class provides helpers to interact with the user.
19 * @author Fabien Potencier <fabien@symfony.com>
21 class DialogHelper extends Helper
26 * Asks a question to the user.
28 * @param OutputInterface $output An Output instance
29 * @param string|array $question The question to ask
30 * @param string $default The default answer if none is given by the user
32 * @return string The user answer
34 * @throws \RuntimeException If there is no data to read in the input stream
36 public function ask(OutputInterface $output, $question, $default = null)
38 $output->write($question);
40 $ret = fgets($this->inputStream ?: STDIN, 4096);
42 throw new \RuntimeException('Aborted');
46 return strlen($ret) > 0 ? $ret : $default;
50 * Asks a confirmation to the user.
52 * The question will be asked until the user answers by nothing, yes, or no.
54 * @param OutputInterface $output An Output instance
55 * @param string|array $question The question to ask
56 * @param Boolean $default The default answer if the user enters nothing
58 * @return Boolean true if the user has confirmed, false otherwise
60 public function askConfirmation(OutputInterface $output, $question, $default = true)
63 while ($answer && !in_array(strtolower($answer[0]), array('y', 'n'))) {
64 $answer = $this->ask($output, $question);
67 if (false === $default) {
68 return $answer && 'y' == strtolower($answer[0]);
71 return !$answer || 'y' == strtolower($answer[0]);
75 * Asks for a value and validates the response.
77 * The validator receives the data to validate. It must return the
78 * validated data when the data is valid and throw an exception
81 * @param OutputInterface $output An Output instance
82 * @param string|array $question The question to ask
83 * @param callback $validator A PHP callback
84 * @param integer $attempts Max number of times to ask before giving up (false by default, which means infinite)
85 * @param string $default The default answer if none is given by the user
89 * @throws \Exception When any of the validators return an error
91 public function askAndValidate(OutputInterface $output, $question, $validator, $attempts = false, $default = null)
94 while (false === $attempts || $attempts--) {
95 if (null !== $error) {
96 $output->writeln($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
99 $value = $this->ask($output, $question, $default);
102 return call_user_func($validator, $value);
103 } catch (\Exception $error) {
111 * Sets the input stream to read from when interacting with the user.
113 * This is mainly useful for testing purpose.
115 * @param resource $stream The input stream
117 public function setInputStream($stream)
119 $this->inputStream = $stream;
123 * Returns the helper's input stream
127 public function getInputStream()
129 return $this->inputStream;
133 * Returns the helper's canonical name.
135 * @return string The helper name
137 public function getName()