[ Index ] |
PHP Cross Reference of phpBB-3.1.12-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 namespace Symfony\Component\Console\Formatter; 13 14 /** 15 * Formatter style class for defining styles. 16 * 17 * @author Konstantin Kudryashov <ever.zet@gmail.com> 18 */ 19 class OutputFormatterStyle implements OutputFormatterStyleInterface 20 { 21 private static $availableForegroundColors = array( 22 'black' => 30, 23 'red' => 31, 24 'green' => 32, 25 'yellow' => 33, 26 'blue' => 34, 27 'magenta' => 35, 28 'cyan' => 36, 29 'white' => 37, 30 'default' => 39, 31 ); 32 private static $availableBackgroundColors = array( 33 'black' => 40, 34 'red' => 41, 35 'green' => 42, 36 'yellow' => 43, 37 'blue' => 44, 38 'magenta' => 45, 39 'cyan' => 46, 40 'white' => 47, 41 'default' => 49, 42 ); 43 private static $availableOptions = array( 44 'bold' => 1, 45 'underscore' => 4, 46 'blink' => 5, 47 'reverse' => 7, 48 'conceal' => 8, 49 ); 50 51 private $foreground; 52 private $background; 53 private $options = array(); 54 55 /** 56 * Initializes output formatter style. 57 * 58 * @param string|null $foreground The style foreground color name 59 * @param string|null $background The style background color name 60 * @param array $options The style options 61 */ 62 public function __construct($foreground = null, $background = null, array $options = array()) 63 { 64 if (null !== $foreground) { 65 $this->setForeground($foreground); 66 } 67 if (null !== $background) { 68 $this->setBackground($background); 69 } 70 if (count($options)) { 71 $this->setOptions($options); 72 } 73 } 74 75 /** 76 * Sets style foreground color. 77 * 78 * @param string|null $color The color name 79 * 80 * @throws \InvalidArgumentException When the color name isn't defined 81 */ 82 public function setForeground($color = null) 83 { 84 if (null === $color) { 85 $this->foreground = null; 86 87 return; 88 } 89 90 if (!isset(static::$availableForegroundColors[$color])) { 91 throw new \InvalidArgumentException(sprintf( 92 'Invalid foreground color specified: "%s". Expected one of (%s)', 93 $color, 94 implode(', ', array_keys(static::$availableForegroundColors)) 95 )); 96 } 97 98 $this->foreground = static::$availableForegroundColors[$color]; 99 } 100 101 /** 102 * Sets style background color. 103 * 104 * @param string|null $color The color name 105 * 106 * @throws \InvalidArgumentException When the color name isn't defined 107 */ 108 public function setBackground($color = null) 109 { 110 if (null === $color) { 111 $this->background = null; 112 113 return; 114 } 115 116 if (!isset(static::$availableBackgroundColors[$color])) { 117 throw new \InvalidArgumentException(sprintf( 118 'Invalid background color specified: "%s". Expected one of (%s)', 119 $color, 120 implode(', ', array_keys(static::$availableBackgroundColors)) 121 )); 122 } 123 124 $this->background = static::$availableBackgroundColors[$color]; 125 } 126 127 /** 128 * Sets some specific style option. 129 * 130 * @param string $option The option name 131 * 132 * @throws \InvalidArgumentException When the option name isn't defined 133 */ 134 public function setOption($option) 135 { 136 if (!isset(static::$availableOptions[$option])) { 137 throw new \InvalidArgumentException(sprintf( 138 'Invalid option specified: "%s". Expected one of (%s)', 139 $option, 140 implode(', ', array_keys(static::$availableOptions)) 141 )); 142 } 143 144 if (!in_array(static::$availableOptions[$option], $this->options)) { 145 $this->options[] = static::$availableOptions[$option]; 146 } 147 } 148 149 /** 150 * Unsets some specific style option. 151 * 152 * @param string $option The option name 153 * 154 * @throws \InvalidArgumentException When the option name isn't defined 155 */ 156 public function unsetOption($option) 157 { 158 if (!isset(static::$availableOptions[$option])) { 159 throw new \InvalidArgumentException(sprintf( 160 'Invalid option specified: "%s". Expected one of (%s)', 161 $option, 162 implode(', ', array_keys(static::$availableOptions)) 163 )); 164 } 165 166 $pos = array_search(static::$availableOptions[$option], $this->options); 167 if (false !== $pos) { 168 unset($this->options[$pos]); 169 } 170 } 171 172 /** 173 * Sets multiple style options at once. 174 * 175 * @param array $options 176 */ 177 public function setOptions(array $options) 178 { 179 $this->options = array(); 180 181 foreach ($options as $option) { 182 $this->setOption($option); 183 } 184 } 185 186 /** 187 * Applies the style to a given text. 188 * 189 * @param string $text The text to style 190 * 191 * @return string 192 */ 193 public function apply($text) 194 { 195 $codes = array(); 196 197 if (null !== $this->foreground) { 198 $codes[] = $this->foreground; 199 } 200 if (null !== $this->background) { 201 $codes[] = $this->background; 202 } 203 if (count($this->options)) { 204 $codes = array_merge($codes, $this->options); 205 } 206 207 if (0 === count($codes)) { 208 return $text; 209 } 210 211 return sprintf("\033[%sm%s\033[0m", implode(';', $codes), $text); 212 } 213 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 11 00:25:41 2018 | Cross-referenced by PHPXref 0.7.1 |