Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

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:58 - Dateigröße: 5.79 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  /**
015   * Formatter style class for defining styles.
016   *
017   * @author Konstantin Kudryashov <ever.zet@gmail.com>
018   *
019   * @api
020   */
021  class OutputFormatterStyle implements OutputFormatterStyleInterface
022  {
023      private static $availableForegroundColors = array(
024          'black'     => 30,
025          'red'       => 31,
026          'green'     => 32,
027          'yellow'    => 33,
028          'blue'      => 34,
029          'magenta'   => 35,
030          'cyan'      => 36,
031          'white'     => 37,
032      );
033      private static $availableBackgroundColors = array(
034          'black'     => 40,
035          'red'       => 41,
036          'green'     => 42,
037          'yellow'    => 43,
038          'blue'      => 44,
039          'magenta'   => 45,
040          'cyan'      => 46,
041          'white'     => 47,
042      );
043      private static $availableOptions = array(
044          'bold'          => 1,
045          'underscore'    => 4,
046          'blink'         => 5,
047          'reverse'       => 7,
048          'conceal'       => 8,
049      );
050   
051      private $foreground;
052      private $background;
053      private $options = array();
054   
055      /**
056       * Initializes output formatter style.
057       *
058       * @param string|null $foreground The style foreground color name
059       * @param string|null $background The style background color name
060       * @param array       $options    The style options
061       *
062       * @api
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       * @api
085       */
086      public function setForeground($color = null)
087      {
088          if (null === $color) {
089              $this->foreground = null;
090   
091              return;
092          }
093   
094          if (!isset(static::$availableForegroundColors[$color])) {
095              throw new \InvalidArgumentException(sprintf(
096                  'Invalid foreground color specified: "%s". Expected one of (%s)',
097                  $color,
098                  implode(', ', array_keys(static::$availableForegroundColors))
099              ));
100          }
101   
102          $this->foreground = static::$availableForegroundColors[$color];
103      }
104   
105      /**
106       * Sets style background color.
107       *
108       * @param string|null $color The color name
109       *
110       * @throws \InvalidArgumentException When the color name isn't defined
111       *
112       * @api
113       */
114      public function setBackground($color = null)
115      {
116          if (null === $color) {
117              $this->background = null;
118   
119              return;
120          }
121   
122          if (!isset(static::$availableBackgroundColors[$color])) {
123              throw new \InvalidArgumentException(sprintf(
124                  'Invalid background color specified: "%s". Expected one of (%s)',
125                  $color,
126                  implode(', ', array_keys(static::$availableBackgroundColors))
127              ));
128          }
129   
130          $this->background = static::$availableBackgroundColors[$color];
131      }
132   
133      /**
134       * Sets some specific style option.
135       *
136       * @param string $option The option name
137       *
138       * @throws \InvalidArgumentException When the option name isn't defined
139       *
140       * @api
141       */
142      public function setOption($option)
143      {
144          if (!isset(static::$availableOptions[$option])) {
145              throw new \InvalidArgumentException(sprintf(
146                  'Invalid option specified: "%s". Expected one of (%s)',
147                  $option,
148                  implode(', ', array_keys(static::$availableOptions))
149              ));
150          }
151   
152          if (false === array_search(static::$availableOptions[$option], $this->options)) {
153              $this->options[] = static::$availableOptions[$option];
154          }
155      }
156   
157      /**
158       * Unsets some specific style option.
159       *
160       * @param string $option The option name
161       *
162       * @throws \InvalidArgumentException When the option name isn't defined
163       *
164       */
165      public function unsetOption($option)
166      {
167          if (!isset(static::$availableOptions[$option])) {
168              throw new \InvalidArgumentException(sprintf(
169                  'Invalid option specified: "%s". Expected one of (%s)',
170                  $option,
171                  implode(', ', array_keys(static::$availableOptions))
172              ));
173          }
174   
175          $pos = array_search(static::$availableOptions[$option], $this->options);
176          if (false !== $pos) {
177              unset($this->options[$pos]);
178          }
179      }
180   
181      /**
182       * Sets multiple style options at once.
183       *
184       * @param array $options
185       */
186      public function setOptions(array $options)
187      {
188          $this->options = array();
189   
190          foreach ($options as $option) {
191              $this->setOption($option);
192          }
193      }
194   
195      /**
196       * Applies the style to a given text.
197       *
198       * @param string $text The text to style
199       *
200       * @return string
201       */
202      public function apply($text)
203      {
204          $codes = array();
205   
206          if (null !== $this->foreground) {
207              $codes[] = $this->foreground;
208          }
209          if (null !== $this->background) {
210              $codes[] = $this->background;
211          }
212          if (count($this->options)) {
213              $codes = array_merge($codes, $this->options);
214          }
215   
216          if (0 === count($codes)) {
217              return $text;
218          }
219   
220          return sprintf("\033[%sm%s\033[0m", implode(';', $codes), $text);
221      }
222  }
223