Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
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. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
OutputFormatterStyle.php
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 = [
024 'black' => ['set' => 30, 'unset' => 39],
025 'red' => ['set' => 31, 'unset' => 39],
026 'green' => ['set' => 32, 'unset' => 39],
027 'yellow' => ['set' => 33, 'unset' => 39],
028 'blue' => ['set' => 34, 'unset' => 39],
029 'magenta' => ['set' => 35, 'unset' => 39],
030 'cyan' => ['set' => 36, 'unset' => 39],
031 'white' => ['set' => 37, 'unset' => 39],
032 'default' => ['set' => 39, 'unset' => 39],
033 ];
034 private static $availableBackgroundColors = [
035 'black' => ['set' => 40, 'unset' => 49],
036 'red' => ['set' => 41, 'unset' => 49],
037 'green' => ['set' => 42, 'unset' => 49],
038 'yellow' => ['set' => 43, 'unset' => 49],
039 'blue' => ['set' => 44, 'unset' => 49],
040 'magenta' => ['set' => 45, 'unset' => 49],
041 'cyan' => ['set' => 46, 'unset' => 49],
042 'white' => ['set' => 47, 'unset' => 49],
043 'default' => ['set' => 49, 'unset' => 49],
044 ];
045 private static $availableOptions = [
046 'bold' => ['set' => 1, 'unset' => 22],
047 'underscore' => ['set' => 4, 'unset' => 24],
048 'blink' => ['set' => 5, 'unset' => 25],
049 'reverse' => ['set' => 7, 'unset' => 27],
050 'conceal' => ['set' => 8, 'unset' => 28],
051 ];
052
053 private $foreground;
054 private $background;
055 private $options = [];
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 = [])
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 * {@inheritdoc}
079 */
080 public function setForeground($color = null)
081 {
082 if (null === $color) {
083 $this->foreground = null;
084
085 return;
086 }
087
088 if (!isset(static::$availableForegroundColors[$color])) {
089 throw new InvalidArgumentException(sprintf('Invalid foreground color specified: "%s". Expected one of (%s).', $color, implode(', ', array_keys(static::$availableForegroundColors))));
090 }
091
092 $this->foreground = static::$availableForegroundColors[$color];
093 }
094
095 /**
096 * {@inheritdoc}
097 */
098 public function setBackground($color = null)
099 {
100 if (null === $color) {
101 $this->background = null;
102
103 return;
104 }
105
106 if (!isset(static::$availableBackgroundColors[$color])) {
107 throw new InvalidArgumentException(sprintf('Invalid background color specified: "%s". Expected one of (%s).', $color, implode(', ', array_keys(static::$availableBackgroundColors))));
108 }
109
110 $this->background = static::$availableBackgroundColors[$color];
111 }
112
113 /**
114 * {@inheritdoc}
115 */
116 public function setOption($option)
117 {
118 if (!isset(static::$availableOptions[$option])) {
119 throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s).', $option, implode(', ', array_keys(static::$availableOptions))));
120 }
121
122 if (!\in_array(static::$availableOptions[$option], $this->options)) {
123 $this->options[] = static::$availableOptions[$option];
124 }
125 }
126
127 /**
128 * {@inheritdoc}
129 */
130 public function unsetOption($option)
131 {
132 if (!isset(static::$availableOptions[$option])) {
133 throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s).', $option, implode(', ', array_keys(static::$availableOptions))));
134 }
135
136 $pos = array_search(static::$availableOptions[$option], $this->options);
137 if (false !== $pos) {
138 unset($this->options[$pos]);
139 }
140 }
141
142 /**
143 * {@inheritdoc}
144 */
145 public function setOptions(array $options)
146 {
147 $this->options = [];
148
149 foreach ($options as $option) {
150 $this->setOption($option);
151 }
152 }
153
154 /**
155 * {@inheritdoc}
156 */
157 public function apply($text)
158 {
159 $setCodes = [];
160 $unsetCodes = [];
161
162 if (null !== $this->foreground) {
163 $setCodes[] = $this->foreground['set'];
164 $unsetCodes[] = $this->foreground['unset'];
165 }
166 if (null !== $this->background) {
167 $setCodes[] = $this->background['set'];
168 $unsetCodes[] = $this->background['unset'];
169 }
170 if (\count($this->options)) {
171 foreach ($this->options as $option) {
172 $setCodes[] = $option['set'];
173 $unsetCodes[] = $option['unset'];
174 }
175 }
176
177 if (0 === \count($setCodes)) {
178 return $text;
179 }
180
181 return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
182 }
183 }
184