DialogHelper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Symfony\Component\Console\Helper;
  3. use Symfony\Component\Console\Output\OutputInterface;
  4. /*
  5. * This file is part of the Symfony framework.
  6. *
  7. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. /**
  13. * The Dialog class provides helpers to interact with the user.
  14. *
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. */
  17. class DialogHelper extends Helper
  18. {
  19. /**
  20. * Asks a question to the user.
  21. *
  22. * @param OutputInterface $output
  23. * @param string|array $question The question to ask
  24. * @param string $default The default answer if none is given by the user
  25. *
  26. * @return string The user answer
  27. */
  28. public function ask(OutputInterface $output, $question, $default = null)
  29. {
  30. // @codeCoverageIgnoreStart
  31. $output->writeln($question);
  32. $ret = trim(fgets(STDIN));
  33. return $ret ? $ret : $default;
  34. // @codeCoverageIgnoreEnd
  35. }
  36. /**
  37. * Asks a confirmation to the user.
  38. *
  39. * The question will be asked until the user answer by nothing, yes, or no.
  40. *
  41. * @param OutputInterface $output
  42. * @param string|array $question The question to ask
  43. * @param Boolean $default The default answer if the user enters nothing
  44. *
  45. * @return Boolean true if the user has confirmed, false otherwise
  46. */
  47. public function askConfirmation(OutputInterface $output, $question, $default = true)
  48. {
  49. // @codeCoverageIgnoreStart
  50. $answer = 'z';
  51. while ($answer && !in_array(strtolower($answer[0]), array('y', 'n'))) {
  52. $answer = $this->ask($output, $question);
  53. }
  54. if (false === $default) {
  55. return $answer && 'y' == strtolower($answer[0]);
  56. } else {
  57. return !$answer || 'y' == strtolower($answer[0]);
  58. }
  59. // @codeCoverageIgnoreEnd
  60. }
  61. /**
  62. * Asks for a value and validates the response.
  63. *
  64. * @param OutputInterface $output
  65. * @param string|array $question
  66. * @param Closure $validator
  67. * @param integer $attempts Max number of times to ask before giving up (false by default, which means infinite)
  68. *
  69. * @return mixed
  70. *
  71. * @throws \Exception When any of the validator returns an error
  72. */
  73. public function askAndValidate(OutputInterface $output, $question, \Closure $validator, $attempts = false)
  74. {
  75. // @codeCoverageIgnoreStart
  76. $error = null;
  77. while (false === $attempts || $attempts--) {
  78. if (null !== $error) {
  79. $output->writeln($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
  80. }
  81. $value = $this->ask($output, $question, null);
  82. try {
  83. return $validator($value);
  84. } catch (\Exception $error) {
  85. }
  86. }
  87. throw $error;
  88. // @codeCoverageIgnoreEnd
  89. }
  90. /**
  91. * Returns the helper's canonical name
  92. */
  93. public function getName()
  94. {
  95. return 'dialog';
  96. }
  97. }