Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/console/Formatter/OutputFormatter.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 |
---|---|
19 * @author Konstantin Kudryashov <ever.zet@gmail.com> | 19 * @author Konstantin Kudryashov <ever.zet@gmail.com> |
20 */ | 20 */ |
21 class OutputFormatter implements OutputFormatterInterface | 21 class OutputFormatter implements OutputFormatterInterface |
22 { | 22 { |
23 private $decorated; | 23 private $decorated; |
24 private $styles = array(); | 24 private $styles = []; |
25 private $styleStack; | 25 private $styleStack; |
26 | 26 |
27 /** | 27 /** |
28 * Escapes "<" special char in given text. | 28 * Escapes "<" special char in given text. |
29 * | 29 * |
48 * @internal | 48 * @internal |
49 */ | 49 */ |
50 public static function escapeTrailingBackslash($text) | 50 public static function escapeTrailingBackslash($text) |
51 { | 51 { |
52 if ('\\' === substr($text, -1)) { | 52 if ('\\' === substr($text, -1)) { |
53 $len = strlen($text); | 53 $len = \strlen($text); |
54 $text = rtrim($text, '\\'); | 54 $text = rtrim($text, '\\'); |
55 $text = str_replace("\0", '', $text); | 55 $text = str_replace("\0", '', $text); |
56 $text .= str_repeat("\0", $len - strlen($text)); | 56 $text .= str_repeat("\0", $len - \strlen($text)); |
57 } | 57 } |
58 | 58 |
59 return $text; | 59 return $text; |
60 } | 60 } |
61 | 61 |
63 * Initializes console output formatter. | 63 * Initializes console output formatter. |
64 * | 64 * |
65 * @param bool $decorated Whether this formatter should actually decorate strings | 65 * @param bool $decorated Whether this formatter should actually decorate strings |
66 * @param OutputFormatterStyleInterface[] $styles Array of "name => FormatterStyle" instances | 66 * @param OutputFormatterStyleInterface[] $styles Array of "name => FormatterStyle" instances |
67 */ | 67 */ |
68 public function __construct($decorated = false, array $styles = array()) | 68 public function __construct($decorated = false, array $styles = []) |
69 { | 69 { |
70 $this->decorated = (bool) $decorated; | 70 $this->decorated = (bool) $decorated; |
71 | 71 |
72 $this->setStyle('error', new OutputFormatterStyle('white', 'red')); | 72 $this->setStyle('error', new OutputFormatterStyle('white', 'red')); |
73 $this->setStyle('info', new OutputFormatterStyle('green')); | 73 $this->setStyle('info', new OutputFormatterStyle('green')); |
143 continue; | 143 continue; |
144 } | 144 } |
145 | 145 |
146 // add the text up to the next tag | 146 // add the text up to the next tag |
147 $output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset)); | 147 $output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset)); |
148 $offset = $pos + strlen($text); | 148 $offset = $pos + \strlen($text); |
149 | 149 |
150 // opening tag? | 150 // opening tag? |
151 if ($open = '/' != $text[1]) { | 151 if ($open = '/' != $text[1]) { |
152 $tag = $matches[1][$i][0]; | 152 $tag = $matches[1][$i][0]; |
153 } else { | 153 } else { |
155 } | 155 } |
156 | 156 |
157 if (!$open && !$tag) { | 157 if (!$open && !$tag) { |
158 // </> | 158 // </> |
159 $this->styleStack->pop(); | 159 $this->styleStack->pop(); |
160 } elseif (false === $style = $this->createStyleFromString(strtolower($tag))) { | 160 } elseif (false === $style = $this->createStyleFromString($tag)) { |
161 $output .= $this->applyCurrentStyle($text); | 161 $output .= $this->applyCurrentStyle($text); |
162 } elseif ($open) { | 162 } elseif ($open) { |
163 $this->styleStack->push($style); | 163 $this->styleStack->push($style); |
164 } else { | 164 } else { |
165 $this->styleStack->pop($style); | 165 $this->styleStack->pop($style); |
167 } | 167 } |
168 | 168 |
169 $output .= $this->applyCurrentStyle(substr($message, $offset)); | 169 $output .= $this->applyCurrentStyle(substr($message, $offset)); |
170 | 170 |
171 if (false !== strpos($output, "\0")) { | 171 if (false !== strpos($output, "\0")) { |
172 return strtr($output, array("\0" => '\\', '\\<' => '<')); | 172 return strtr($output, ["\0" => '\\', '\\<' => '<']); |
173 } | 173 } |
174 | 174 |
175 return str_replace('\\<', '<', $output); | 175 return str_replace('\\<', '<', $output); |
176 } | 176 } |
177 | 177 |
201 } | 201 } |
202 | 202 |
203 $style = new OutputFormatterStyle(); | 203 $style = new OutputFormatterStyle(); |
204 foreach ($matches as $match) { | 204 foreach ($matches as $match) { |
205 array_shift($match); | 205 array_shift($match); |
206 $match[0] = strtolower($match[0]); | |
206 | 207 |
207 if ('fg' == $match[0]) { | 208 if ('fg' == $match[0]) { |
208 $style->setForeground($match[1]); | 209 $style->setForeground(strtolower($match[1])); |
209 } elseif ('bg' == $match[0]) { | 210 } elseif ('bg' == $match[0]) { |
210 $style->setBackground($match[1]); | 211 $style->setBackground(strtolower($match[1])); |
211 } elseif ('options' === $match[0]) { | 212 } elseif ('options' === $match[0]) { |
212 preg_match_all('([^,;]+)', $match[1], $options); | 213 preg_match_all('([^,;]+)', strtolower($match[1]), $options); |
213 $options = array_shift($options); | 214 $options = array_shift($options); |
214 foreach ($options as $option) { | 215 foreach ($options as $option) { |
215 try { | 216 try { |
216 $style->setOption($option); | 217 $style->setOption($option); |
217 } catch (\InvalidArgumentException $e) { | 218 } catch (\InvalidArgumentException $e) { |
235 * | 236 * |
236 * @return string Styled text | 237 * @return string Styled text |
237 */ | 238 */ |
238 private function applyCurrentStyle($text) | 239 private function applyCurrentStyle($text) |
239 { | 240 { |
240 return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text; | 241 return $this->isDecorated() && \strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text; |
241 } | 242 } |
242 } | 243 } |