[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Application/CLI/ -> ColorStyle.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2014 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Application\CLI;
  11  
  12  // phpcs:disable PSR1.Files.SideEffects
  13  \defined('JPATH_PLATFORM') or die;
  14  // phpcs:enable PSR1.Files.SideEffects
  15  
  16  /**
  17   * Class defining ANSI-color styles for command line output
  18   *
  19   * @since       4.0.0
  20   * @deprecated  5.0  Use the `joomla/console` package instead
  21   */
  22  final class ColorStyle
  23  {
  24      /**
  25       * Known colors
  26       *
  27       * @var    array
  28       * @since  4.0.0
  29       */
  30      private static $knownColors = [
  31          'black'   => 0,
  32          'red'     => 1,
  33          'green'   => 2,
  34          'yellow'  => 3,
  35          'blue'    => 4,
  36          'magenta' => 5,
  37          'cyan'    => 6,
  38          'white'   => 7,
  39      ];
  40  
  41      /**
  42       * Known styles
  43       *
  44       * @var    array
  45       * @since  4.0.0
  46       */
  47      private static $knownOptions = [
  48          'bold'       => 1,
  49          'underscore' => 4,
  50          'blink'      => 5,
  51          'reverse'    => 7,
  52      ];
  53  
  54      /**
  55       * Foreground base value
  56       *
  57       * @var    integer
  58       * @since  4.0.0
  59       */
  60      private static $fgBase = 30;
  61  
  62      /**
  63       * Background base value
  64       *
  65       * @var    integer
  66       * @since  4.0.0
  67       */
  68      private static $bgBase = 40;
  69  
  70      /**
  71       * Foreground color
  72       *
  73       * @var    integer
  74       * @since  4.0.0
  75       */
  76      private $fgColor = 0;
  77  
  78      /**
  79       * Background color
  80       *
  81       * @var    integer
  82       * @since  4.0.0
  83       */
  84      private $bgColor = 0;
  85  
  86      /**
  87       * Array of style options
  88       *
  89       * @var    array
  90       * @since  4.0.0
  91       */
  92      private $options = [];
  93  
  94      /**
  95       * Constructor
  96       *
  97       * @param   string  $fg       Foreground color.
  98       * @param   string  $bg       Background color.
  99       * @param   array   $options  Style options.
 100       *
 101       * @since   4.0.0
 102       * @throws  \InvalidArgumentException
 103       */
 104      public function __construct(string $fg = '', string $bg = '', array $options = [])
 105      {
 106          if ($fg) {
 107              if (\array_key_exists($fg, static::$knownColors) == false) {
 108                  throw new \InvalidArgumentException(
 109                      sprintf(
 110                          'Invalid foreground color "%1$s" [%2$s]',
 111                          $fg,
 112                          implode(', ', $this->getKnownColors())
 113                      )
 114                  );
 115              }
 116  
 117              $this->fgColor = static::$fgBase + static::$knownColors[$fg];
 118          }
 119  
 120          if ($bg) {
 121              if (\array_key_exists($bg, static::$knownColors) == false) {
 122                  throw new \InvalidArgumentException(
 123                      sprintf(
 124                          'Invalid background color "%1$s" [%2$s]',
 125                          $bg,
 126                          implode(', ', $this->getKnownColors())
 127                      )
 128                  );
 129              }
 130  
 131              $this->bgColor = static::$bgBase + static::$knownColors[$bg];
 132          }
 133  
 134          foreach ($options as $option) {
 135              if (\array_key_exists($option, static::$knownOptions) == false) {
 136                  throw new \InvalidArgumentException(
 137                      sprintf(
 138                          'Invalid option "%1$s" [%2$s]',
 139                          $option,
 140                          implode(', ', $this->getKnownOptions())
 141                      )
 142                  );
 143              }
 144  
 145              $this->options[] = $option;
 146          }
 147      }
 148  
 149      /**
 150       * Convert to a string.
 151       *
 152       * @return  string
 153       *
 154       * @since   4.0.0
 155       */
 156      public function __toString()
 157      {
 158          return $this->getStyle();
 159      }
 160  
 161      /**
 162       * Create a color style from a parameter string.
 163       *
 164       * Example: fg=red;bg=blue;options=bold,blink
 165       *
 166       * @param   string  $string  The parameter string.
 167       *
 168       * @return  $this
 169       *
 170       * @since   4.0.0
 171       * @throws  \RuntimeException
 172       */
 173      public static function fromString(string $string): self
 174      {
 175          $fg      = '';
 176          $bg      = '';
 177          $options = [];
 178  
 179          $parts = explode(';', $string);
 180  
 181          foreach ($parts as $part) {
 182              $subParts = explode('=', $part);
 183  
 184              if (\count($subParts) < 2) {
 185                  continue;
 186              }
 187  
 188              switch ($subParts[0]) {
 189                  case 'fg':
 190                      $fg = $subParts[1];
 191  
 192                      break;
 193  
 194                  case 'bg':
 195                      $bg = $subParts[1];
 196  
 197                      break;
 198  
 199                  case 'options':
 200                      $options = explode(',', $subParts[1]);
 201  
 202                      break;
 203  
 204                  default:
 205                      throw new \RuntimeException('Invalid option: ' . $subParts[0]);
 206              }
 207          }
 208  
 209          return new self($fg, $bg, $options);
 210      }
 211  
 212      /**
 213       * Get the translated color code.
 214       *
 215       * @return  string
 216       *
 217       * @since   4.0.0
 218       */
 219      public function getStyle(): string
 220      {
 221          $values = [];
 222  
 223          if ($this->fgColor) {
 224              $values[] = $this->fgColor;
 225          }
 226  
 227          if ($this->bgColor) {
 228              $values[] = $this->bgColor;
 229          }
 230  
 231          foreach ($this->options as $option) {
 232              $values[] = static::$knownOptions[$option];
 233          }
 234  
 235          return implode(';', $values);
 236      }
 237  
 238      /**
 239       * Get the known colors.
 240       *
 241       * @return  string[]
 242       *
 243       * @since   4.0.0
 244       */
 245      public function getKnownColors(): array
 246      {
 247          return array_keys(static::$knownColors);
 248      }
 249  
 250      /**
 251       * Get the known options.
 252       *
 253       * @return  string[]
 254       *
 255       * @since   4.0.0
 256       */
 257      public function getKnownOptions(): array
 258      {
 259          return array_keys(static::$knownOptions);
 260      }
 261  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer