PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.30.0
Independent Analytics – WordPress Analytics Plugin v1.30.0
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / symfony / console / Formatter / OutputFormatter.php
independent-analytics / vendor / symfony / console / Formatter Last commit date
NullOutputFormatter.php 3 years ago NullOutputFormatterStyle.php 3 years ago OutputFormatter.php 3 years ago OutputFormatterInterface.php 3 years ago OutputFormatterStyle.php 3 years ago OutputFormatterStyleInterface.php 3 years ago OutputFormatterStyleStack.php 3 years ago WrappableOutputFormatterInterface.php 3 years ago
OutputFormatter.php
244 lines
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 namespace IAWP_SCOPED\Symfony\Component\Console\Formatter;
12
13 use IAWP_SCOPED\Symfony\Component\Console\Exception\InvalidArgumentException;
14 /**
15 * Formatter class for console output.
16 *
17 * @author Konstantin Kudryashov <ever.zet@gmail.com>
18 * @author Roland Franssen <franssen.roland@gmail.com>
19 */
20 class OutputFormatter implements WrappableOutputFormatterInterface
21 {
22 private $decorated;
23 private $styles = [];
24 private $styleStack;
25 public function __clone()
26 {
27 $this->styleStack = clone $this->styleStack;
28 foreach ($this->styles as $key => $value) {
29 $this->styles[$key] = clone $value;
30 }
31 }
32 /**
33 * Escapes "<" and ">" special chars in given text.
34 *
35 * @return string
36 */
37 public static function escape(string $text)
38 {
39 $text = \preg_replace('/([^\\\\]|^)([<>])/', '$1\\\\$2', $text);
40 return self::escapeTrailingBackslash($text);
41 }
42 /**
43 * Escapes trailing "\" in given text.
44 *
45 * @internal
46 */
47 public static function escapeTrailingBackslash(string $text) : string
48 {
49 if (\str_ends_with($text, '\\')) {
50 $len = \strlen($text);
51 $text = \rtrim($text, '\\');
52 $text = \str_replace("\x00", '', $text);
53 $text .= \str_repeat("\x00", $len - \strlen($text));
54 }
55 return $text;
56 }
57 /**
58 * Initializes console output formatter.
59 *
60 * @param OutputFormatterStyleInterface[] $styles Array of "name => FormatterStyle" instances
61 */
62 public function __construct(bool $decorated = \false, array $styles = [])
63 {
64 $this->decorated = $decorated;
65 $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
66 $this->setStyle('info', new OutputFormatterStyle('green'));
67 $this->setStyle('comment', new OutputFormatterStyle('yellow'));
68 $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
69 foreach ($styles as $name => $style) {
70 $this->setStyle($name, $style);
71 }
72 $this->styleStack = new OutputFormatterStyleStack();
73 }
74 /**
75 * {@inheritdoc}
76 */
77 public function setDecorated(bool $decorated)
78 {
79 $this->decorated = $decorated;
80 }
81 /**
82 * {@inheritdoc}
83 */
84 public function isDecorated()
85 {
86 return $this->decorated;
87 }
88 /**
89 * {@inheritdoc}
90 */
91 public function setStyle(string $name, OutputFormatterStyleInterface $style)
92 {
93 $this->styles[\strtolower($name)] = $style;
94 }
95 /**
96 * {@inheritdoc}
97 */
98 public function hasStyle(string $name)
99 {
100 return isset($this->styles[\strtolower($name)]);
101 }
102 /**
103 * {@inheritdoc}
104 */
105 public function getStyle(string $name)
106 {
107 if (!$this->hasStyle($name)) {
108 throw new InvalidArgumentException(\sprintf('Undefined style: "%s".', $name));
109 }
110 return $this->styles[\strtolower($name)];
111 }
112 /**
113 * {@inheritdoc}
114 */
115 public function format(?string $message)
116 {
117 return $this->formatAndWrap($message, 0);
118 }
119 /**
120 * {@inheritdoc}
121 */
122 public function formatAndWrap(?string $message, int $width)
123 {
124 if (null === $message) {
125 return '';
126 }
127 $offset = 0;
128 $output = '';
129 $openTagRegex = '[a-z](?:[^\\\\<>]*+ | \\\\.)*';
130 $closeTagRegex = '[a-z][^<>]*+';
131 $currentLineLength = 0;
132 \preg_match_all("#<(({$openTagRegex}) | /({$closeTagRegex})?)>#ix", $message, $matches, \PREG_OFFSET_CAPTURE);
133 foreach ($matches[0] as $i => $match) {
134 $pos = $match[1];
135 $text = $match[0];
136 if (0 != $pos && '\\' == $message[$pos - 1]) {
137 continue;
138 }
139 // add the text up to the next tag
140 $output .= $this->applyCurrentStyle(\substr($message, $offset, $pos - $offset), $output, $width, $currentLineLength);
141 $offset = $pos + \strlen($text);
142 // opening tag?
143 if ($open = '/' != $text[1]) {
144 $tag = $matches[1][$i][0];
145 } else {
146 $tag = $matches[3][$i][0] ?? '';
147 }
148 if (!$open && !$tag) {
149 // </>
150 $this->styleStack->pop();
151 } elseif (null === ($style = $this->createStyleFromString($tag))) {
152 $output .= $this->applyCurrentStyle($text, $output, $width, $currentLineLength);
153 } elseif ($open) {
154 $this->styleStack->push($style);
155 } else {
156 $this->styleStack->pop($style);
157 }
158 }
159 $output .= $this->applyCurrentStyle(\substr($message, $offset), $output, $width, $currentLineLength);
160 return \strtr($output, ["\x00" => '\\', '\\<' => '<', '\\>' => '>']);
161 }
162 /**
163 * @return OutputFormatterStyleStack
164 */
165 public function getStyleStack()
166 {
167 return $this->styleStack;
168 }
169 /**
170 * Tries to create new style instance from string.
171 */
172 private function createStyleFromString(string $string) : ?OutputFormatterStyleInterface
173 {
174 if (isset($this->styles[$string])) {
175 return $this->styles[$string];
176 }
177 if (!\preg_match_all('/([^=]+)=([^;]+)(;|$)/', $string, $matches, \PREG_SET_ORDER)) {
178 return null;
179 }
180 $style = new OutputFormatterStyle();
181 foreach ($matches as $match) {
182 \array_shift($match);
183 $match[0] = \strtolower($match[0]);
184 if ('fg' == $match[0]) {
185 $style->setForeground(\strtolower($match[1]));
186 } elseif ('bg' == $match[0]) {
187 $style->setBackground(\strtolower($match[1]));
188 } elseif ('href' === $match[0]) {
189 $url = \preg_replace('{\\\\([<>])}', '$1', $match[1]);
190 $style->setHref($url);
191 } elseif ('options' === $match[0]) {
192 \preg_match_all('([^,;]+)', \strtolower($match[1]), $options);
193 $options = \array_shift($options);
194 foreach ($options as $option) {
195 $style->setOption($option);
196 }
197 } else {
198 return null;
199 }
200 }
201 return $style;
202 }
203 /**
204 * Applies current style from stack to text, if must be applied.
205 */
206 private function applyCurrentStyle(string $text, string $current, int $width, int &$currentLineLength) : string
207 {
208 if ('' === $text) {
209 return '';
210 }
211 if (!$width) {
212 return $this->isDecorated() ? $this->styleStack->getCurrent()->apply($text) : $text;
213 }
214 if (!$currentLineLength && '' !== $current) {
215 $text = \ltrim($text);
216 }
217 if ($currentLineLength) {
218 $prefix = \substr($text, 0, $i = $width - $currentLineLength) . "\n";
219 $text = \substr($text, $i);
220 } else {
221 $prefix = '';
222 }
223 \preg_match('~(\\n)$~', $text, $matches);
224 $text = $prefix . \preg_replace('~([^\\n]{' . $width . '})\\ *~', "\$1\n", $text);
225 $text = \rtrim($text, "\n") . ($matches[1] ?? '');
226 if (!$currentLineLength && '' !== $current && "\n" !== \substr($current, -1)) {
227 $text = "\n" . $text;
228 }
229 $lines = \explode("\n", $text);
230 foreach ($lines as $line) {
231 $currentLineLength += \strlen($line);
232 if ($width <= $currentLineLength) {
233 $currentLineLength = 0;
234 }
235 }
236 if ($this->isDecorated()) {
237 foreach ($lines as $i => $line) {
238 $lines[$i] = $this->styleStack->getCurrent()->apply($line);
239 }
240 }
241 return \implode("\n", $lines);
242 }
243 }
244