[ Index ]

PHP Cross Reference of phpBB-3.2.11-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 = array(
  24          'black' => array('set' => 30, 'unset' => 39),
  25          'red' => array('set' => 31, 'unset' => 39),
  26          'green' => array('set' => 32, 'unset' => 39),
  27          'yellow' => array('set' => 33, 'unset' => 39),
  28          'blue' => array('set' => 34, 'unset' => 39),
  29          'magenta' => array('set' => 35, 'unset' => 39),
  30          'cyan' => array('set' => 36, 'unset' => 39),
  31          'white' => array('set' => 37, 'unset' => 39),
  32          'default' => array('set' => 39, 'unset' => 39),
  33      );
  34      private static $availableBackgroundColors = array(
  35          'black' => array('set' => 40, 'unset' => 49),
  36          'red' => array('set' => 41, 'unset' => 49),
  37          'green' => array('set' => 42, 'unset' => 49),
  38          'yellow' => array('set' => 43, 'unset' => 49),
  39          'blue' => array('set' => 44, 'unset' => 49),
  40          'magenta' => array('set' => 45, 'unset' => 49),
  41          'cyan' => array('set' => 46, 'unset' => 49),
  42          'white' => array('set' => 47, 'unset' => 49),
  43          'default' => array('set' => 49, 'unset' => 49),
  44      );
  45      private static $availableOptions = array(
  46          'bold' => array('set' => 1, 'unset' => 22),
  47          'underscore' => array('set' => 4, 'unset' => 24),
  48          'blink' => array('set' => 5, 'unset' => 25),
  49          'reverse' => array('set' => 7, 'unset' => 27),
  50          'conceal' => array('set' => 8, 'unset' => 28),
  51      );
  52  
  53      private $foreground;
  54      private $background;
  55      private $options = array();
  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 = array())
  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       * Sets style foreground color.
  79       *
  80       * @param string|null $color The color name
  81       *
  82       * @throws InvalidArgumentException When the color name isn't defined
  83       */
  84      public function setForeground($color = null)
  85      {
  86          if (null === $color) {
  87              $this->foreground = null;
  88  
  89              return;
  90          }
  91  
  92          if (!isset(static::$availableForegroundColors[$color])) {
  93              throw new InvalidArgumentException(sprintf('Invalid foreground color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableForegroundColors))));
  94          }
  95  
  96          $this->foreground = static::$availableForegroundColors[$color];
  97      }
  98  
  99      /**
 100       * Sets style background color.
 101       *
 102       * @param string|null $color The color name
 103       *
 104       * @throws InvalidArgumentException When the color name isn't defined
 105       */
 106      public function setBackground($color = null)
 107      {
 108          if (null === $color) {
 109              $this->background = null;
 110  
 111              return;
 112          }
 113  
 114          if (!isset(static::$availableBackgroundColors[$color])) {
 115              throw new InvalidArgumentException(sprintf('Invalid background color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableBackgroundColors))));
 116          }
 117  
 118          $this->background = static::$availableBackgroundColors[$color];
 119      }
 120  
 121      /**
 122       * Sets some specific style option.
 123       *
 124       * @param string $option The option name
 125       *
 126       * @throws InvalidArgumentException When the option name isn't defined
 127       */
 128      public function setOption($option)
 129      {
 130          if (!isset(static::$availableOptions[$option])) {
 131              throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions))));
 132          }
 133  
 134          if (!\in_array(static::$availableOptions[$option], $this->options)) {
 135              $this->options[] = static::$availableOptions[$option];
 136          }
 137      }
 138  
 139      /**
 140       * Unsets some specific style option.
 141       *
 142       * @param string $option The option name
 143       *
 144       * @throws InvalidArgumentException When the option name isn't defined
 145       */
 146      public function unsetOption($option)
 147      {
 148          if (!isset(static::$availableOptions[$option])) {
 149              throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions))));
 150          }
 151  
 152          $pos = array_search(static::$availableOptions[$option], $this->options);
 153          if (false !== $pos) {
 154              unset($this->options[$pos]);
 155          }
 156      }
 157  
 158      /**
 159       * {@inheritdoc}
 160       */
 161      public function setOptions(array $options)
 162      {
 163          $this->options = array();
 164  
 165          foreach ($options as $option) {
 166              $this->setOption($option);
 167          }
 168      }
 169  
 170      /**
 171       * Applies the style to a given text.
 172       *
 173       * @param string $text The text to style
 174       *
 175       * @return string
 176       */
 177      public function apply($text)
 178      {
 179          $setCodes = array();
 180          $unsetCodes = array();
 181  
 182          if (null !== $this->foreground) {
 183              $setCodes[] = $this->foreground['set'];
 184              $unsetCodes[] = $this->foreground['unset'];
 185          }
 186          if (null !== $this->background) {
 187              $setCodes[] = $this->background['set'];
 188              $unsetCodes[] = $this->background['unset'];
 189          }
 190          if (\count($this->options)) {
 191              foreach ($this->options as $option) {
 192                  $setCodes[] = $option['set'];
 193                  $unsetCodes[] = $option['unset'];
 194              }
 195          }
 196  
 197          if (0 === \count($setCodes)) {
 198              return $text;
 199          }
 200  
 201          return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
 202      }
 203  }


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