DialogHelper.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Helper;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. /**
  13. * The Dialog class provides helpers to interact with the user.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class DialogHelper extends Helper
  18. {
  19. private $inputStream;
  20. /**
  21. * Asks a question to the user.
  22. *
  23. * @param OutputInterface $output An Output instance
  24. * @param string|array $question The question to ask
  25. * @param string $default The default answer if none is given by the user
  26. *
  27. * @return string The user answer
  28. *
  29. * @throws \RuntimeException If there is no data to read in the input stream
  30. */
  31. public function ask(OutputInterface $output, $question, $default = null)
  32. {
  33. $output->write($question);
  34. $ret = fgets($this->inputStream ?: STDIN, 4096);
  35. if (false === $ret) {
  36. throw new \RuntimeException('Aborted');
  37. }
  38. $ret = trim($ret);
  39. return strlen($ret) > 0 ? $ret : $default;
  40. }
  41. /**
  42. * Asks a confirmation to the user.
  43. *
  44. * The question will be asked until the user answers by nothing, yes, or no.
  45. *
  46. * @param OutputInterface $output An Output instance
  47. * @param string|array $question The question to ask
  48. * @param Boolean $default The default answer if the user enters nothing
  49. *
  50. * @return Boolean true if the user has confirmed, false otherwise
  51. */
  52. public function askConfirmation(OutputInterface $output, $question, $default = true)
  53. {
  54. $answer = 'z';
  55. while ($answer && !in_array(strtolower($answer[0]), array('y', 'n'))) {
  56. $answer = $this->ask($output, $question);
  57. }
  58. if (false === $default) {
  59. return $answer && 'y' == strtolower($answer[0]);
  60. }
  61. return !$answer || 'y' == strtolower($answer[0]);
  62. }
  63. /**
  64. * Asks for a value and validates the response.
  65. *
  66. * The validator receives the data to validate. It must return the
  67. * validated data when the data is valid and throw an exception
  68. * otherwise.
  69. *
  70. * @param OutputInterface $output An Output instance
  71. * @param string|array $question The question to ask
  72. * @param callback $validator A PHP callback
  73. * @param integer $attempts Max number of times to ask before giving up (false by default, which means infinite)
  74. * @param string $default The default answer if none is given by the user
  75. *
  76. * @return mixed
  77. *
  78. * @throws \Exception When any of the validators return an error
  79. */
  80. public function askAndValidate(OutputInterface $output, $question, $validator, $attempts = false, $default = null)
  81. {
  82. $error = null;
  83. while (false === $attempts || $attempts--) {
  84. if (null !== $error) {
  85. $output->writeln($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
  86. }
  87. $value = $this->ask($output, $question, $default);
  88. try {
  89. return call_user_func($validator, $value);
  90. } catch (\Exception $error) {
  91. }
  92. }
  93. throw $error;
  94. }
  95. /**
  96. * Sets the input stream to read from when interacting with the user.
  97. *
  98. * This is mainly useful for testing purpose.
  99. *
  100. * @param resource $stream The input stream
  101. */
  102. public function setInputStream($stream)
  103. {
  104. $this->inputStream = $stream;
  105. }
  106. /**
  107. * Returns the helper's input stream
  108. *
  109. * @return string
  110. */
  111. public function getInputStream()
  112. {
  113. return $this->inputStream;
  114. }
  115. /**
  116. * Returns the helper's canonical name.
  117. */
  118. public function getName()
  119. {
  120. return 'dialog';
  121. }
  122. }