Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

OutputFormatterStyle.php

Zuletzt modifiziert: 09.10.2024, 12:56 - Dateigröße: 6.69 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Component\Console\Formatter;
013   
014  use Symfony\Component\Console\Exception\InvalidArgumentException;
015   
016  /**
017   * Formatter style class for defining styles.
018   *
019   * @author Konstantin Kudryashov <ever.zet@gmail.com>
020   */
021  class OutputFormatterStyle implements OutputFormatterStyleInterface
022  {
023      private static $availableForegroundColors = array(
024          'black' => array('set' => 30, 'unset' => 39),
025          'red' => array('set' => 31, 'unset' => 39),
026          'green' => array('set' => 32, 'unset' => 39),
027          'yellow' => array('set' => 33, 'unset' => 39),
028          'blue' => array('set' => 34, 'unset' => 39),
029          'magenta' => array('set' => 35, 'unset' => 39),
030          'cyan' => array('set' => 36, 'unset' => 39),
031          'white' => array('set' => 37, 'unset' => 39),
032          'default' => array('set' => 39, 'unset' => 39),
033      );
034      private static $availableBackgroundColors = array(
035          'black' => array('set' => 40, 'unset' => 49),
036          'red' => array('set' => 41, 'unset' => 49),
037          'green' => array('set' => 42, 'unset' => 49),
038          'yellow' => array('set' => 43, 'unset' => 49),
039          'blue' => array('set' => 44, 'unset' => 49),
040          'magenta' => array('set' => 45, 'unset' => 49),
041          'cyan' => array('set' => 46, 'unset' => 49),
042          'white' => array('set' => 47, 'unset' => 49),
043          'default' => array('set' => 49, 'unset' => 49),
044      );
045      private static $availableOptions = array(
046          'bold' => array('set' => 1, 'unset' => 22),
047          'underscore' => array('set' => 4, 'unset' => 24),
048          'blink' => array('set' => 5, 'unset' => 25),
049          'reverse' => array('set' => 7, 'unset' => 27),
050          'conceal' => array('set' => 8, 'unset' => 28),
051      );
052   
053      private $foreground;
054      private $background;
055      private $options = array();
056   
057      /**
058       * Initializes output formatter style.
059       *
060       * @param string|null $foreground The style foreground color name
061       * @param string|null $background The style background color name
062       * @param array       $options    The style options
063       */
064      public function __construct($foreground = null, $background = null, array $options = array())
065      {
066          if (null !== $foreground) {
067              $this->setForeground($foreground);
068          }
069          if (null !== $background) {
070              $this->setBackground($background);
071          }
072          if (count($options)) {
073              $this->setOptions($options);
074          }
075      }
076   
077      /**
078       * Sets style foreground color.
079       *
080       * @param string|null $color The color name
081       *
082       * @throws InvalidArgumentException When the color name isn't defined
083       */
084      public function setForeground($color = null)
085      {
086          if (null === $color) {
087              $this->foreground = null;
088   
089              return;
090          }
091   
092          if (!isset(static::$availableForegroundColors[$color])) {
093              throw new InvalidArgumentException(sprintf(
094                  'Invalid foreground color specified: "%s". Expected one of (%s)',
095                  $color,
096                  implode(', ', array_keys(static::$availableForegroundColors))
097              ));
098          }
099   
100          $this->foreground = static::$availableForegroundColors[$color];
101      }
102   
103      /**
104       * Sets style background color.
105       *
106       * @param string|null $color The color name
107       *
108       * @throws InvalidArgumentException When the color name isn't defined
109       */
110      public function setBackground($color = null)
111      {
112          if (null === $color) {
113              $this->background = null;
114   
115              return;
116          }
117   
118          if (!isset(static::$availableBackgroundColors[$color])) {
119              throw new InvalidArgumentException(sprintf(
120                  'Invalid background color specified: "%s". Expected one of (%s)',
121                  $color,
122                  implode(', ', array_keys(static::$availableBackgroundColors))
123              ));
124          }
125   
126          $this->background = static::$availableBackgroundColors[$color];
127      }
128   
129      /**
130       * Sets some specific style option.
131       *
132       * @param string $option The option name
133       *
134       * @throws InvalidArgumentException When the option name isn't defined
135       */
136      public function setOption($option)
137      {
138          if (!isset(static::$availableOptions[$option])) {
139              throw new InvalidArgumentException(sprintf(
140                  'Invalid option specified: "%s". Expected one of (%s)',
141                  $option,
142                  implode(', ', array_keys(static::$availableOptions))
143              ));
144          }
145   
146          if (!in_array(static::$availableOptions[$option], $this->options)) {
147              $this->options[] = static::$availableOptions[$option];
148          }
149      }
150   
151      /**
152       * Unsets some specific style option.
153       *
154       * @param string $option The option name
155       *
156       * @throws InvalidArgumentException When the option name isn't defined
157       */
158      public function unsetOption($option)
159      {
160          if (!isset(static::$availableOptions[$option])) {
161              throw new InvalidArgumentException(sprintf(
162                  'Invalid option specified: "%s". Expected one of (%s)',
163                  $option,
164                  implode(', ', array_keys(static::$availableOptions))
165              ));
166          }
167   
168          $pos = array_search(static::$availableOptions[$option], $this->options);
169          if (false !== $pos) {
170              unset($this->options[$pos]);
171          }
172      }
173   
174      /**
175       * Sets multiple style options at once.
176       *
177       * @param array $options
178       */
179      public function setOptions(array $options)
180      {
181          $this->options = array();
182   
183          foreach ($options as $option) {
184              $this->setOption($option);
185          }
186      }
187   
188      /**
189       * Applies the style to a given text.
190       *
191       * @param string $text The text to style
192       *
193       * @return string
194       */
195      public function apply($text)
196      {
197          $setCodes = array();
198          $unsetCodes = array();
199   
200          if (null !== $this->foreground) {
201              $setCodes[] = $this->foreground['set'];
202              $unsetCodes[] = $this->foreground['unset'];
203          }
204          if (null !== $this->background) {
205              $setCodes[] = $this->background['set'];
206              $unsetCodes[] = $this->background['unset'];
207          }
208          if (count($this->options)) {
209              foreach ($this->options as $option) {
210                  $setCodes[] = $option['set'];
211                  $unsetCodes[] = $option['unset'];
212              }
213          }
214   
215          if (0 === count($setCodes)) {
216              return $text;
217          }
218   
219          return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
220      }
221  }
222