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