[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/console/Helper/ -> SymfonyQuestionHelper.php (source)

   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\Exception\LogicException;
  15  use Symfony\Component\Console\Formatter\OutputFormatter;
  16  use Symfony\Component\Console\Input\InputInterface;
  17  use Symfony\Component\Console\Output\OutputInterface;
  18  use Symfony\Component\Console\Question\ChoiceQuestion;
  19  use Symfony\Component\Console\Question\ConfirmationQuestion;
  20  use Symfony\Component\Console\Question\Question;
  21  use Symfony\Component\Console\Style\SymfonyStyle;
  22  
  23  /**
  24   * Symfony Style Guide compliant question helper.
  25   *
  26   * @author Kevin Bond <kevinbond@gmail.com>
  27   */
  28  class SymfonyQuestionHelper extends QuestionHelper
  29  {
  30      /**
  31       * {@inheritdoc}
  32       */
  33      public function ask(InputInterface $input, OutputInterface $output, Question $question)
  34      {
  35          $validator = $question->getValidator();
  36          $question->setValidator(function ($value) use ($validator) {
  37              if (null !== $validator) {
  38                  $value = $validator($value);
  39              } else {
  40                  // make required
  41                  if (!\is_array($value) && !\is_bool($value) && 0 === \strlen($value)) {
  42                      throw new LogicException('A value is required.');
  43                  }
  44              }
  45  
  46              return $value;
  47          });
  48  
  49          return parent::ask($input, $output, $question);
  50      }
  51  
  52      /**
  53       * {@inheritdoc}
  54       */
  55      protected function writePrompt(OutputInterface $output, Question $question)
  56      {
  57          $text = OutputFormatter::escapeTrailingBackslash($question->getQuestion());
  58          $default = $question->getDefault();
  59  
  60          switch (true) {
  61              case null === $default:
  62                  $text = sprintf(' <info>%s</info>:', $text);
  63  
  64                  break;
  65  
  66              case $question instanceof ConfirmationQuestion:
  67                  $text = sprintf(' <info>%s (yes/no)</info> [<comment>%s</comment>]:', $text, $default ? 'yes' : 'no');
  68  
  69                  break;
  70  
  71              case $question instanceof ChoiceQuestion && $question->isMultiselect():
  72                  $choices = $question->getChoices();
  73                  $default = explode(',', $default);
  74  
  75                  foreach ($default as $key => $value) {
  76                      $default[$key] = $choices[trim($value)];
  77                  }
  78  
  79                  $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(implode(', ', $default)));
  80  
  81                  break;
  82  
  83              case $question instanceof ChoiceQuestion:
  84                  $choices = $question->getChoices();
  85                  $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(isset($choices[$default]) ? $choices[$default] : $default));
  86  
  87                  break;
  88  
  89              default:
  90                  $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($default));
  91          }
  92  
  93          $output->writeln($text);
  94  
  95          if ($question instanceof ChoiceQuestion) {
  96              $width = max(array_map('strlen', array_keys($question->getChoices())));
  97  
  98              foreach ($question->getChoices() as $key => $value) {
  99                  $output->writeln(sprintf("  [<comment>%-$width}s</comment>] %s", $key, $value));
 100              }
 101          }
 102  
 103          $output->write(' > ');
 104      }
 105  
 106      /**
 107       * {@inheritdoc}
 108       */
 109      protected function writeError(OutputInterface $output, \Exception $error)
 110      {
 111          if ($output instanceof SymfonyStyle) {
 112              $output->newLine();
 113              $output->error($error->getMessage());
 114  
 115              return;
 116          }
 117  
 118          parent::writeError($output, $error);
 119      }
 120  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1