[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/symfony/console/Formatter/ -> OutputFormatterStyle.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\Formatter;
  13  
  14  use Symfony\Component\Console\Exception\InvalidArgumentException;
  15  
  16  /**
  17   * Formatter style class for defining styles.
  18   *
  19   * @author Konstantin Kudryashov <ever.zet@gmail.com>
  20   */
  21  class OutputFormatterStyle implements OutputFormatterStyleInterface
  22  {
  23      private static $availableForegroundColors = [
  24          'black' => ['set' => 30, 'unset' => 39],
  25          'red' => ['set' => 31, 'unset' => 39],
  26          'green' => ['set' => 32, 'unset' => 39],
  27          'yellow' => ['set' => 33, 'unset' => 39],
  28          'blue' => ['set' => 34, 'unset' => 39],
  29          'magenta' => ['set' => 35, 'unset' => 39],
  30          'cyan' => ['set' => 36, 'unset' => 39],
  31          'white' => ['set' => 37, 'unset' => 39],
  32          'default' => ['set' => 39, 'unset' => 39],
  33      ];
  34      private static $availableBackgroundColors = [
  35          'black' => ['set' => 40, 'unset' => 49],
  36          'red' => ['set' => 41, 'unset' => 49],
  37          'green' => ['set' => 42, 'unset' => 49],
  38          'yellow' => ['set' => 43, 'unset' => 49],
  39          'blue' => ['set' => 44, 'unset' => 49],
  40          'magenta' => ['set' => 45, 'unset' => 49],
  41          'cyan' => ['set' => 46, 'unset' => 49],
  42          'white' => ['set' => 47, 'unset' => 49],
  43          'default' => ['set' => 49, 'unset' => 49],
  44      ];
  45      private static $availableOptions = [
  46          'bold' => ['set' => 1, 'unset' => 22],
  47          'underscore' => ['set' => 4, 'unset' => 24],
  48          'blink' => ['set' => 5, 'unset' => 25],
  49          'reverse' => ['set' => 7, 'unset' => 27],
  50          'conceal' => ['set' => 8, 'unset' => 28],
  51      ];
  52  
  53      private $foreground;
  54      private $background;
  55      private $options = [];
  56  
  57      /**
  58       * Initializes output formatter style.
  59       *
  60       * @param string|null $foreground The style foreground color name
  61       * @param string|null $background The style background color name
  62       * @param array       $options    The style options
  63       */
  64      public function __construct($foreground = null, $background = null, array $options = [])
  65      {
  66          if (null !== $foreground) {
  67              $this->setForeground($foreground);
  68          }
  69          if (null !== $background) {
  70              $this->setBackground($background);
  71          }
  72          if (\count($options)) {
  73              $this->setOptions($options);
  74          }
  75      }
  76  
  77      /**
  78       * {@inheritdoc}
  79       */
  80      public function setForeground($color = null)
  81      {
  82          if (null === $color) {
  83              $this->foreground = null;
  84  
  85              return;
  86          }
  87  
  88          if (!isset(static::$availableForegroundColors[$color])) {
  89              throw new InvalidArgumentException(sprintf('Invalid foreground color specified: "%s". Expected one of (%s).', $color, implode(', ', array_keys(static::$availableForegroundColors))));
  90          }
  91  
  92          $this->foreground = static::$availableForegroundColors[$color];
  93      }
  94  
  95      /**
  96       * {@inheritdoc}
  97       */
  98      public function setBackground($color = null)
  99      {
 100          if (null === $color) {
 101              $this->background = null;
 102  
 103              return;
 104          }
 105  
 106          if (!isset(static::$availableBackgroundColors[$color])) {
 107              throw new InvalidArgumentException(sprintf('Invalid background color specified: "%s". Expected one of (%s).', $color, implode(', ', array_keys(static::$availableBackgroundColors))));
 108          }
 109  
 110          $this->background = static::$availableBackgroundColors[$color];
 111      }
 112  
 113      /**
 114       * {@inheritdoc}
 115       */
 116      public function setOption($option)
 117      {
 118          if (!isset(static::$availableOptions[$option])) {
 119              throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s).', $option, implode(', ', array_keys(static::$availableOptions))));
 120          }
 121  
 122          if (!\in_array(static::$availableOptions[$option], $this->options)) {
 123              $this->options[] = static::$availableOptions[$option];
 124          }
 125      }
 126  
 127      /**
 128       * {@inheritdoc}
 129       */
 130      public function unsetOption($option)
 131      {
 132          if (!isset(static::$availableOptions[$option])) {
 133              throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s).', $option, implode(', ', array_keys(static::$availableOptions))));
 134          }
 135  
 136          $pos = array_search(static::$availableOptions[$option], $this->options);
 137          if (false !== $pos) {
 138              unset($this->options[$pos]);
 139          }
 140      }
 141  
 142      /**
 143       * {@inheritdoc}
 144       */
 145      public function setOptions(array $options)
 146      {
 147          $this->options = [];
 148  
 149          foreach ($options as $option) {
 150              $this->setOption($option);
 151          }
 152      }
 153  
 154      /**
 155       * {@inheritdoc}
 156       */
 157      public function apply($text)
 158      {
 159          $setCodes = [];
 160          $unsetCodes = [];
 161  
 162          if (null !== $this->foreground) {
 163              $setCodes[] = $this->foreground['set'];
 164              $unsetCodes[] = $this->foreground['unset'];
 165          }
 166          if (null !== $this->background) {
 167              $setCodes[] = $this->background['set'];
 168              $unsetCodes[] = $this->background['unset'];
 169          }
 170          if (\count($this->options)) {
 171              foreach ($this->options as $option) {
 172                  $setCodes[] = $option['set'];
 173                  $unsetCodes[] = $option['unset'];
 174              }
 175          }
 176  
 177          if (0 === \count($setCodes)) {
 178              return $text;
 179          }
 180  
 181          return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
 182      }
 183  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1