comparison vendor/symfony/console/Formatter/OutputFormatterStyle.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
18 * 18 *
19 * @author Konstantin Kudryashov <ever.zet@gmail.com> 19 * @author Konstantin Kudryashov <ever.zet@gmail.com>
20 */ 20 */
21 class OutputFormatterStyle implements OutputFormatterStyleInterface 21 class OutputFormatterStyle implements OutputFormatterStyleInterface
22 { 22 {
23 private static $availableForegroundColors = array( 23 private static $availableForegroundColors = [
24 'black' => array('set' => 30, 'unset' => 39), 24 'black' => ['set' => 30, 'unset' => 39],
25 'red' => array('set' => 31, 'unset' => 39), 25 'red' => ['set' => 31, 'unset' => 39],
26 'green' => array('set' => 32, 'unset' => 39), 26 'green' => ['set' => 32, 'unset' => 39],
27 'yellow' => array('set' => 33, 'unset' => 39), 27 'yellow' => ['set' => 33, 'unset' => 39],
28 'blue' => array('set' => 34, 'unset' => 39), 28 'blue' => ['set' => 34, 'unset' => 39],
29 'magenta' => array('set' => 35, 'unset' => 39), 29 'magenta' => ['set' => 35, 'unset' => 39],
30 'cyan' => array('set' => 36, 'unset' => 39), 30 'cyan' => ['set' => 36, 'unset' => 39],
31 'white' => array('set' => 37, 'unset' => 39), 31 'white' => ['set' => 37, 'unset' => 39],
32 'default' => array('set' => 39, 'unset' => 39), 32 'default' => ['set' => 39, 'unset' => 39],
33 ); 33 ];
34 private static $availableBackgroundColors = array( 34 private static $availableBackgroundColors = [
35 'black' => array('set' => 40, 'unset' => 49), 35 'black' => ['set' => 40, 'unset' => 49],
36 'red' => array('set' => 41, 'unset' => 49), 36 'red' => ['set' => 41, 'unset' => 49],
37 'green' => array('set' => 42, 'unset' => 49), 37 'green' => ['set' => 42, 'unset' => 49],
38 'yellow' => array('set' => 43, 'unset' => 49), 38 'yellow' => ['set' => 43, 'unset' => 49],
39 'blue' => array('set' => 44, 'unset' => 49), 39 'blue' => ['set' => 44, 'unset' => 49],
40 'magenta' => array('set' => 45, 'unset' => 49), 40 'magenta' => ['set' => 45, 'unset' => 49],
41 'cyan' => array('set' => 46, 'unset' => 49), 41 'cyan' => ['set' => 46, 'unset' => 49],
42 'white' => array('set' => 47, 'unset' => 49), 42 'white' => ['set' => 47, 'unset' => 49],
43 'default' => array('set' => 49, 'unset' => 49), 43 'default' => ['set' => 49, 'unset' => 49],
44 ); 44 ];
45 private static $availableOptions = array( 45 private static $availableOptions = [
46 'bold' => array('set' => 1, 'unset' => 22), 46 'bold' => ['set' => 1, 'unset' => 22],
47 'underscore' => array('set' => 4, 'unset' => 24), 47 'underscore' => ['set' => 4, 'unset' => 24],
48 'blink' => array('set' => 5, 'unset' => 25), 48 'blink' => ['set' => 5, 'unset' => 25],
49 'reverse' => array('set' => 7, 'unset' => 27), 49 'reverse' => ['set' => 7, 'unset' => 27],
50 'conceal' => array('set' => 8, 'unset' => 28), 50 'conceal' => ['set' => 8, 'unset' => 28],
51 ); 51 ];
52 52
53 private $foreground; 53 private $foreground;
54 private $background; 54 private $background;
55 private $options = array(); 55 private $options = [];
56 56
57 /** 57 /**
58 * Initializes output formatter style. 58 * Initializes output formatter style.
59 * 59 *
60 * @param string|null $foreground The style foreground color name 60 * @param string|null $foreground The style foreground color name
61 * @param string|null $background The style background color name 61 * @param string|null $background The style background color name
62 * @param array $options The style options 62 * @param array $options The style options
63 */ 63 */
64 public function __construct($foreground = null, $background = null, array $options = array()) 64 public function __construct($foreground = null, $background = null, array $options = [])
65 { 65 {
66 if (null !== $foreground) { 66 if (null !== $foreground) {
67 $this->setForeground($foreground); 67 $this->setForeground($foreground);
68 } 68 }
69 if (null !== $background) { 69 if (null !== $background) {
70 $this->setBackground($background); 70 $this->setBackground($background);
71 } 71 }
72 if (count($options)) { 72 if (\count($options)) {
73 $this->setOptions($options); 73 $this->setOptions($options);
74 } 74 }
75 } 75 }
76 76
77 /** 77 /**
88 88
89 return; 89 return;
90 } 90 }
91 91
92 if (!isset(static::$availableForegroundColors[$color])) { 92 if (!isset(static::$availableForegroundColors[$color])) {
93 throw new InvalidArgumentException(sprintf( 93 throw new InvalidArgumentException(sprintf('Invalid foreground color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableForegroundColors))));
94 'Invalid foreground color specified: "%s". Expected one of (%s)',
95 $color,
96 implode(', ', array_keys(static::$availableForegroundColors))
97 ));
98 } 94 }
99 95
100 $this->foreground = static::$availableForegroundColors[$color]; 96 $this->foreground = static::$availableForegroundColors[$color];
101 } 97 }
102 98
114 110
115 return; 111 return;
116 } 112 }
117 113
118 if (!isset(static::$availableBackgroundColors[$color])) { 114 if (!isset(static::$availableBackgroundColors[$color])) {
119 throw new InvalidArgumentException(sprintf( 115 throw new InvalidArgumentException(sprintf('Invalid background color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableBackgroundColors))));
120 'Invalid background color specified: "%s". Expected one of (%s)',
121 $color,
122 implode(', ', array_keys(static::$availableBackgroundColors))
123 ));
124 } 116 }
125 117
126 $this->background = static::$availableBackgroundColors[$color]; 118 $this->background = static::$availableBackgroundColors[$color];
127 } 119 }
128 120
134 * @throws InvalidArgumentException When the option name isn't defined 126 * @throws InvalidArgumentException When the option name isn't defined
135 */ 127 */
136 public function setOption($option) 128 public function setOption($option)
137 { 129 {
138 if (!isset(static::$availableOptions[$option])) { 130 if (!isset(static::$availableOptions[$option])) {
139 throw new InvalidArgumentException(sprintf( 131 throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions))));
140 'Invalid option specified: "%s". Expected one of (%s)', 132 }
141 $option, 133
142 implode(', ', array_keys(static::$availableOptions)) 134 if (!\in_array(static::$availableOptions[$option], $this->options)) {
143 ));
144 }
145
146 if (!in_array(static::$availableOptions[$option], $this->options)) {
147 $this->options[] = static::$availableOptions[$option]; 135 $this->options[] = static::$availableOptions[$option];
148 } 136 }
149 } 137 }
150 138
151 /** 139 /**
156 * @throws InvalidArgumentException When the option name isn't defined 144 * @throws InvalidArgumentException When the option name isn't defined
157 */ 145 */
158 public function unsetOption($option) 146 public function unsetOption($option)
159 { 147 {
160 if (!isset(static::$availableOptions[$option])) { 148 if (!isset(static::$availableOptions[$option])) {
161 throw new InvalidArgumentException(sprintf( 149 throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions))));
162 'Invalid option specified: "%s". Expected one of (%s)',
163 $option,
164 implode(', ', array_keys(static::$availableOptions))
165 ));
166 } 150 }
167 151
168 $pos = array_search(static::$availableOptions[$option], $this->options); 152 $pos = array_search(static::$availableOptions[$option], $this->options);
169 if (false !== $pos) { 153 if (false !== $pos) {
170 unset($this->options[$pos]); 154 unset($this->options[$pos]);
174 /** 158 /**
175 * {@inheritdoc} 159 * {@inheritdoc}
176 */ 160 */
177 public function setOptions(array $options) 161 public function setOptions(array $options)
178 { 162 {
179 $this->options = array(); 163 $this->options = [];
180 164
181 foreach ($options as $option) { 165 foreach ($options as $option) {
182 $this->setOption($option); 166 $this->setOption($option);
183 } 167 }
184 } 168 }
190 * 174 *
191 * @return string 175 * @return string
192 */ 176 */
193 public function apply($text) 177 public function apply($text)
194 { 178 {
195 $setCodes = array(); 179 $setCodes = [];
196 $unsetCodes = array(); 180 $unsetCodes = [];
197 181
198 if (null !== $this->foreground) { 182 if (null !== $this->foreground) {
199 $setCodes[] = $this->foreground['set']; 183 $setCodes[] = $this->foreground['set'];
200 $unsetCodes[] = $this->foreground['unset']; 184 $unsetCodes[] = $this->foreground['unset'];
201 } 185 }
202 if (null !== $this->background) { 186 if (null !== $this->background) {
203 $setCodes[] = $this->background['set']; 187 $setCodes[] = $this->background['set'];
204 $unsetCodes[] = $this->background['unset']; 188 $unsetCodes[] = $this->background['unset'];
205 } 189 }
206 if (count($this->options)) { 190 if (\count($this->options)) {
207 foreach ($this->options as $option) { 191 foreach ($this->options as $option) {
208 $setCodes[] = $option['set']; 192 $setCodes[] = $option['set'];
209 $unsetCodes[] = $option['unset']; 193 $unsetCodes[] = $option['unset'];
210 } 194 }
211 } 195 }
212 196
213 if (0 === count($setCodes)) { 197 if (0 === \count($setCodes)) {
214 return $text; 198 return $text;
215 } 199 }
216 200
217 return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes)); 201 return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
218 } 202 }