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