DialogHelper.php 2.9 KB

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