[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Application/CLI/Output/Processor/ -> ColorProcessor.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\Output\Processor;
  11  
  12  use Joomla\CMS\Application\CLI\ColorStyle;
  13  
  14  // phpcs:disable PSR1.Files.SideEffects
  15  \defined('JPATH_PLATFORM') or die;
  16  // phpcs:enable PSR1.Files.SideEffects
  17  
  18  /**
  19   * Command line output processor supporting ANSI-colored output
  20   *
  21   * @since       4.0.0
  22   * @deprecated  5.0  Use the `joomla/console` package instead
  23   */
  24  class ColorProcessor implements ProcessorInterface
  25  {
  26      /**
  27       * Flag to remove color codes from the output
  28       *
  29       * @var    boolean
  30       * @since  4.0.0
  31       */
  32      public $noColors = false;
  33  
  34      /**
  35       * Regex to match tags
  36       *
  37       * @var    string
  38       * @since  4.0.0
  39       */
  40      protected $tagFilter = '/<([a-z=;]+)>(.*?)<\/\\1>/s';
  41  
  42      /**
  43       * Regex used for removing color codes
  44       *
  45       * @var    string
  46       * @since  4.0.0
  47       */
  48      protected static $stripFilter = '/<[\/]?[a-z=;]+>/';
  49  
  50      /**
  51       * Array of ColorStyle objects
  52       *
  53       * @var    ColorStyle[]
  54       * @since  4.0.0
  55       */
  56      protected $styles = [];
  57  
  58      /**
  59       * Class constructor
  60       *
  61       * @param   boolean  $noColors  Defines non-colored mode on construct
  62       *
  63       * @since   4.0.0
  64       */
  65      public function __construct($noColors = null)
  66      {
  67          if ($noColors === null) {
  68              /*
  69               * By default windows cmd.exe and PowerShell does not support ANSI-colored output
  70               * if the variable is not set explicitly colors should be disabled on Windows
  71               */
  72              $noColors = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
  73          }
  74  
  75          $this->noColors = $noColors;
  76  
  77          $this->addPredefinedStyles();
  78      }
  79  
  80      /**
  81       * Add a style.
  82       *
  83       * @param   string      $name   The style name.
  84       * @param   ColorStyle  $style  The color style.
  85       *
  86       * @return  $this
  87       *
  88       * @since   4.0.0
  89       */
  90      public function addStyle($name, ColorStyle $style)
  91      {
  92          $this->styles[$name] = $style;
  93  
  94          return $this;
  95      }
  96  
  97      /**
  98       * Strip color tags from a string.
  99       *
 100       * @param   string  $string  The string.
 101       *
 102       * @return  string
 103       *
 104       * @since   4.0.0
 105       */
 106      public static function stripColors($string)
 107      {
 108          return preg_replace(static::$stripFilter, '', $string);
 109      }
 110  
 111      /**
 112       * Process a string.
 113       *
 114       * @param   string  $string  The string to process.
 115       *
 116       * @return  string
 117       *
 118       * @since   4.0.0
 119       */
 120      public function process($string)
 121      {
 122          preg_match_all($this->tagFilter, $string, $matches);
 123  
 124          if (!$matches) {
 125              return $string;
 126          }
 127  
 128          foreach ($matches[0] as $i => $m) {
 129              if (\array_key_exists($matches[1][$i], $this->styles)) {
 130                  $string = $this->replaceColors($string, $matches[1][$i], $matches[2][$i], $this->styles[$matches[1][$i]]);
 131              } elseif (strpos($matches[1][$i], '=')) {
 132                  // Custom format
 133                  $string = $this->replaceColors($string, $matches[1][$i], $matches[2][$i], ColorStyle::fromString($matches[1][$i]));
 134              }
 135          }
 136  
 137          return $string;
 138      }
 139  
 140      /**
 141       * Replace color tags in a string.
 142       *
 143       * @param   string      $text   The original text.
 144       * @param   string      $tag    The matched tag.
 145       * @param   string      $match  The match.
 146       * @param   ColorStyle  $style  The color style to apply.
 147       *
 148       * @return  mixed
 149       *
 150       * @since   4.0.0
 151       */
 152      private function replaceColors($text, $tag, $match, ColorStyle $style)
 153      {
 154          $replace = $this->noColors
 155              ? $match
 156              : "\033[" . $style . 'm' . $match . "\033[0m";
 157  
 158          return str_replace('<' . $tag . '>' . $match . '</' . $tag . '>', $replace, $text);
 159      }
 160  
 161      /**
 162       * Adds predefined color styles to the ColorProcessor object
 163       *
 164       * @return  $this
 165       *
 166       * @since   4.0.0
 167       */
 168      private function addPredefinedStyles()
 169      {
 170          $this->addStyle(
 171              'info',
 172              new ColorStyle('green', '', ['bold'])
 173          );
 174  
 175          $this->addStyle(
 176              'comment',
 177              new ColorStyle('yellow', '', ['bold'])
 178          );
 179  
 180          $this->addStyle(
 181              'question',
 182              new ColorStyle('black', 'cyan')
 183          );
 184  
 185          $this->addStyle(
 186              'error',
 187              new ColorStyle('white', 'red')
 188          );
 189  
 190          return $this;
 191      }
 192  }


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