[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/console/Style/ -> SymfonyStyle.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\Style;
  13  
  14  use Symfony\Component\Console\Application;
  15  use Symfony\Component\Console\Exception\RuntimeException;
  16  use Symfony\Component\Console\Formatter\OutputFormatter;
  17  use Symfony\Component\Console\Helper\Helper;
  18  use Symfony\Component\Console\Helper\ProgressBar;
  19  use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
  20  use Symfony\Component\Console\Helper\Table;
  21  use Symfony\Component\Console\Input\InputInterface;
  22  use Symfony\Component\Console\Output\BufferedOutput;
  23  use Symfony\Component\Console\Output\OutputInterface;
  24  use Symfony\Component\Console\Question\ChoiceQuestion;
  25  use Symfony\Component\Console\Question\ConfirmationQuestion;
  26  use Symfony\Component\Console\Question\Question;
  27  
  28  /**
  29   * Output decorator helpers for the Symfony Style Guide.
  30   *
  31   * @author Kevin Bond <kevinbond@gmail.com>
  32   */
  33  class SymfonyStyle extends OutputStyle
  34  {
  35      const MAX_LINE_LENGTH = 120;
  36  
  37      private $input;
  38      private $questionHelper;
  39      private $progressBar;
  40      private $lineLength;
  41      private $bufferedOutput;
  42  
  43      public function __construct(InputInterface $input, OutputInterface $output)
  44      {
  45          $this->input = $input;
  46          $this->bufferedOutput = new BufferedOutput($output->getVerbosity(), false, clone $output->getFormatter());
  47          // Windows cmd wraps lines as soon as the terminal width is reached, whether there are following chars or not.
  48          $this->lineLength = min($this->getTerminalWidth() - (int) (\DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
  49  
  50          parent::__construct($output);
  51      }
  52  
  53      /**
  54       * Formats a message as a block of text.
  55       *
  56       * @param string|array $messages The message to write in the block
  57       * @param string|null  $type     The block type (added in [] on first line)
  58       * @param string|null  $style    The style to apply to the whole block
  59       * @param string       $prefix   The prefix for the block
  60       * @param bool         $padding  Whether to add vertical padding
  61       */
  62      public function block($messages, $type = null, $style = null, $prefix = ' ', $padding = false)
  63      {
  64          $messages = \is_array($messages) ? array_values($messages) : array($messages);
  65  
  66          $this->autoPrependBlock();
  67          $this->writeln($this->createBlock($messages, $type, $style, $prefix, $padding, true));
  68          $this->newLine();
  69      }
  70  
  71      /**
  72       * {@inheritdoc}
  73       */
  74      public function title($message)
  75      {
  76          $this->autoPrependBlock();
  77          $this->writeln(array(
  78              sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
  79              sprintf('<comment>%s</>', str_repeat('=', Helper::strlenWithoutDecoration($this->getFormatter(), $message))),
  80          ));
  81          $this->newLine();
  82      }
  83  
  84      /**
  85       * {@inheritdoc}
  86       */
  87      public function section($message)
  88      {
  89          $this->autoPrependBlock();
  90          $this->writeln(array(
  91              sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)),
  92              sprintf('<comment>%s</>', str_repeat('-', Helper::strlenWithoutDecoration($this->getFormatter(), $message))),
  93          ));
  94          $this->newLine();
  95      }
  96  
  97      /**
  98       * {@inheritdoc}
  99       */
 100      public function listing(array $elements)
 101      {
 102          $this->autoPrependText();
 103          $elements = array_map(function ($element) {
 104              return sprintf(' * %s', $element);
 105          }, $elements);
 106  
 107          $this->writeln($elements);
 108          $this->newLine();
 109      }
 110  
 111      /**
 112       * {@inheritdoc}
 113       */
 114      public function text($message)
 115      {
 116          $this->autoPrependText();
 117  
 118          $messages = \is_array($message) ? array_values($message) : array($message);
 119          foreach ($messages as $message) {
 120              $this->writeln(sprintf(' %s', $message));
 121          }
 122      }
 123  
 124      /**
 125       * Formats a command comment.
 126       *
 127       * @param string|array $message
 128       */
 129      public function comment($message)
 130      {
 131          $messages = \is_array($message) ? array_values($message) : array($message);
 132  
 133          $this->autoPrependBlock();
 134          $this->writeln($this->createBlock($messages, null, null, '<fg=default;bg=default> // </>'));
 135          $this->newLine();
 136      }
 137  
 138      /**
 139       * {@inheritdoc}
 140       */
 141      public function success($message)
 142      {
 143          $this->block($message, 'OK', 'fg=black;bg=green', ' ', true);
 144      }
 145  
 146      /**
 147       * {@inheritdoc}
 148       */
 149      public function error($message)
 150      {
 151          $this->block($message, 'ERROR', 'fg=white;bg=red', ' ', true);
 152      }
 153  
 154      /**
 155       * {@inheritdoc}
 156       */
 157      public function warning($message)
 158      {
 159          $this->block($message, 'WARNING', 'fg=white;bg=red', ' ', true);
 160      }
 161  
 162      /**
 163       * {@inheritdoc}
 164       */
 165      public function note($message)
 166      {
 167          $this->block($message, 'NOTE', 'fg=yellow', ' ! ');
 168      }
 169  
 170      /**
 171       * {@inheritdoc}
 172       */
 173      public function caution($message)
 174      {
 175          $this->block($message, 'CAUTION', 'fg=white;bg=red', ' ! ', true);
 176      }
 177  
 178      /**
 179       * {@inheritdoc}
 180       */
 181      public function table(array $headers, array $rows)
 182      {
 183          $style = clone Table::getStyleDefinition('symfony-style-guide');
 184          $style->setCellHeaderFormat('<info>%s</info>');
 185  
 186          $table = new Table($this);
 187          $table->setHeaders($headers);
 188          $table->setRows($rows);
 189          $table->setStyle($style);
 190  
 191          $table->render();
 192          $this->newLine();
 193      }
 194  
 195      /**
 196       * {@inheritdoc}
 197       */
 198      public function ask($question, $default = null, $validator = null)
 199      {
 200          $question = new Question($question, $default);
 201          $question->setValidator($validator);
 202  
 203          return $this->askQuestion($question);
 204      }
 205  
 206      /**
 207       * {@inheritdoc}
 208       */
 209      public function askHidden($question, $validator = null)
 210      {
 211          $question = new Question($question);
 212  
 213          $question->setHidden(true);
 214          $question->setValidator($validator);
 215  
 216          return $this->askQuestion($question);
 217      }
 218  
 219      /**
 220       * {@inheritdoc}
 221       */
 222      public function confirm($question, $default = true)
 223      {
 224          return $this->askQuestion(new ConfirmationQuestion($question, $default));
 225      }
 226  
 227      /**
 228       * {@inheritdoc}
 229       */
 230      public function choice($question, array $choices, $default = null)
 231      {
 232          if (null !== $default) {
 233              $values = array_flip($choices);
 234              $default = $values[$default];
 235          }
 236  
 237          return $this->askQuestion(new ChoiceQuestion($question, $choices, $default));
 238      }
 239  
 240      /**
 241       * {@inheritdoc}
 242       */
 243      public function progressStart($max = 0)
 244      {
 245          $this->progressBar = $this->createProgressBar($max);
 246          $this->progressBar->start();
 247      }
 248  
 249      /**
 250       * {@inheritdoc}
 251       */
 252      public function progressAdvance($step = 1)
 253      {
 254          $this->getProgressBar()->advance($step);
 255      }
 256  
 257      /**
 258       * {@inheritdoc}
 259       */
 260      public function progressFinish()
 261      {
 262          $this->getProgressBar()->finish();
 263          $this->newLine(2);
 264          $this->progressBar = null;
 265      }
 266  
 267      /**
 268       * {@inheritdoc}
 269       */
 270      public function createProgressBar($max = 0)
 271      {
 272          $progressBar = parent::createProgressBar($max);
 273  
 274          if ('\\' !== \DIRECTORY_SEPARATOR || 'Hyper' === getenv('TERM_PROGRAM')) {
 275              $progressBar->setEmptyBarCharacter('░'); // light shade character \u2591
 276              $progressBar->setProgressCharacter('');
 277              $progressBar->setBarCharacter('▓'); // dark shade character \u2593
 278          }
 279  
 280          return $progressBar;
 281      }
 282  
 283      /**
 284       * @return mixed
 285       */
 286      public function askQuestion(Question $question)
 287      {
 288          if ($this->input->isInteractive()) {
 289              $this->autoPrependBlock();
 290          }
 291  
 292          if (!$this->questionHelper) {
 293              $this->questionHelper = new SymfonyQuestionHelper();
 294          }
 295  
 296          $answer = $this->questionHelper->ask($this->input, $this, $question);
 297  
 298          if ($this->input->isInteractive()) {
 299              $this->newLine();
 300              $this->bufferedOutput->write("\n");
 301          }
 302  
 303          return $answer;
 304      }
 305  
 306      /**
 307       * {@inheritdoc}
 308       */
 309      public function writeln($messages, $type = self::OUTPUT_NORMAL)
 310      {
 311          parent::writeln($messages, $type);
 312          $this->bufferedOutput->writeln($this->reduceBuffer($messages), $type);
 313      }
 314  
 315      /**
 316       * {@inheritdoc}
 317       */
 318      public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
 319      {
 320          parent::write($messages, $newline, $type);
 321          $this->bufferedOutput->write($this->reduceBuffer($messages), $newline, $type);
 322      }
 323  
 324      /**
 325       * {@inheritdoc}
 326       */
 327      public function newLine($count = 1)
 328      {
 329          parent::newLine($count);
 330          $this->bufferedOutput->write(str_repeat("\n", $count));
 331      }
 332  
 333      /**
 334       * @return ProgressBar
 335       */
 336      private function getProgressBar()
 337      {
 338          if (!$this->progressBar) {
 339              throw new RuntimeException('The ProgressBar is not started.');
 340          }
 341  
 342          return $this->progressBar;
 343      }
 344  
 345      private function getTerminalWidth()
 346      {
 347          $application = new Application();
 348          $dimensions = $application->getTerminalDimensions();
 349  
 350          return $dimensions[0] ?: self::MAX_LINE_LENGTH;
 351      }
 352  
 353      private function autoPrependBlock()
 354      {
 355          $chars = substr(str_replace(PHP_EOL, "\n", $this->bufferedOutput->fetch()), -2);
 356  
 357          if (!isset($chars[0])) {
 358              return $this->newLine(); //empty history, so we should start with a new line.
 359          }
 360          //Prepend new line for each non LF chars (This means no blank line was output before)
 361          $this->newLine(2 - substr_count($chars, "\n"));
 362      }
 363  
 364      private function autoPrependText()
 365      {
 366          $fetched = $this->bufferedOutput->fetch();
 367          //Prepend new line if last char isn't EOL:
 368          if ("\n" !== substr($fetched, -1)) {
 369              $this->newLine();
 370          }
 371      }
 372  
 373      private function reduceBuffer($messages)
 374      {
 375          // We need to know if the two last chars are PHP_EOL
 376          // Preserve the last 4 chars inserted (PHP_EOL on windows is two chars) in the history buffer
 377          return array_map(function ($value) {
 378              return substr($value, -4);
 379          }, array_merge(array($this->bufferedOutput->fetch()), (array) $messages));
 380      }
 381  
 382      private function createBlock($messages, $type = null, $style = null, $prefix = ' ', $padding = false, $escape = false)
 383      {
 384          $indentLength = 0;
 385          $prefixLength = Helper::strlenWithoutDecoration($this->getFormatter(), $prefix);
 386          $lines = array();
 387  
 388          if (null !== $type) {
 389              $type = sprintf('[%s] ', $type);
 390              $indentLength = \strlen($type);
 391              $lineIndentation = str_repeat(' ', $indentLength);
 392          }
 393  
 394          // wrap and add newlines for each element
 395          foreach ($messages as $key => $message) {
 396              if ($escape) {
 397                  $message = OutputFormatter::escape($message);
 398              }
 399  
 400              $lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - $prefixLength - $indentLength, PHP_EOL, true)));
 401  
 402              if (\count($messages) > 1 && $key < \count($messages) - 1) {
 403                  $lines[] = '';
 404              }
 405          }
 406  
 407          $firstLineIndex = 0;
 408          if ($padding && $this->isDecorated()) {
 409              $firstLineIndex = 1;
 410              array_unshift($lines, '');
 411              $lines[] = '';
 412          }
 413  
 414          foreach ($lines as $i => &$line) {
 415              if (null !== $type) {
 416                  $line = $firstLineIndex === $i ? $type.$line : $lineIndentation.$line;
 417              }
 418  
 419              $line = $prefix.$line;
 420              $line .= str_repeat(' ', $this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line));
 421  
 422              if ($style) {
 423                  $line = sprintf('<%s>%s</>', $style, $line);
 424              }
 425          }
 426  
 427          return $lines;
 428      }
 429  }


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