PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.2.0
Independent Analytics – WordPress Analytics Plugin v2.2.0
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 2 years ago NullOutputFormatterStyle.php 2 years ago OutputFormatter.php 2 years ago OutputFormatterInterface.php 2 years ago OutputFormatterStyle.php 2 years ago OutputFormatterStyleInterface.php 2 years ago OutputFormatterStyleStack.php 2 years ago WrappableOutputFormatterInterface.php 2 years ago
OutputFormatter.php
245 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 * @internal
20 */
21 class OutputFormatter implements WrappableOutputFormatterInterface
22 {
23 private $decorated;
24 private $styles = [];
25 private $styleStack;
26 public function __clone()
27 {
28 $this->styleStack = clone $this->styleStack;
29 foreach ($this->styles as $key => $value) {
30 $this->styles[$key] = clone $value;
31 }
32 }
33 /**
34 * Escapes "<" and ">" special chars in given text.
35 *
36 * @return string
37 */
38 public static function escape(string $text)
39 {
40 $text = \preg_replace('/([^\\\\]|^)([<>])/', '$1\\\\$2', $text);
41 return self::escapeTrailingBackslash($text);
42 }
43 /**
44 * Escapes trailing "\" in given text.
45 *
46 * @internal
47 */
48 public static function escapeTrailingBackslash(string $text) : string
49 {
50 if (\str_ends_with($text, '\\')) {
51 $len = \strlen($text);
52 $text = \rtrim($text, '\\');
53 $text = \str_replace("\x00", '', $text);
54 $text .= \str_repeat("\x00", $len - \strlen($text));
55 }
56 return $text;
57 }
58 /**
59 * Initializes console output formatter.
60 *
61 * @param OutputFormatterStyleInterface[] $styles Array of "name => FormatterStyle" instances
62 */
63 public function __construct(bool $decorated = \false, array $styles = [])
64 {
65 $this->decorated = $decorated;
66 $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
67 $this->setStyle('info', new OutputFormatterStyle('green'));
68 $this->setStyle('comment', new OutputFormatterStyle('yellow'));
69 $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
70 foreach ($styles as $name => $style) {
71 $this->setStyle($name, $style);
72 }
73 $this->styleStack = new OutputFormatterStyleStack();
74 }
75 /**
76 * {@inheritdoc}
77 */
78 public function setDecorated(bool $decorated)
79 {
80 $this->decorated = $decorated;
81 }
82 /**
83 * {@inheritdoc}
84 */
85 public function isDecorated()
86 {
87 return $this->decorated;
88 }
89 /**
90 * {@inheritdoc}
91 */
92 public function setStyle(string $name, OutputFormatterStyleInterface $style)
93 {
94 $this->styles[\strtolower($name)] = $style;
95 }
96 /**
97 * {@inheritdoc}
98 */
99 public function hasStyle(string $name)
100 {
101 return isset($this->styles[\strtolower($name)]);
102 }
103 /**
104 * {@inheritdoc}
105 */
106 public function getStyle(string $name)
107 {
108 if (!$this->hasStyle($name)) {
109 throw new InvalidArgumentException(\sprintf('Undefined style: "%s".', $name));
110 }
111 return $this->styles[\strtolower($name)];
112 }
113 /**
114 * {@inheritdoc}
115 */
116 public function format(?string $message)
117 {
118 return $this->formatAndWrap($message, 0);
119 }
120 /**
121 * {@inheritdoc}
122 */
123 public function formatAndWrap(?string $message, int $width)
124 {
125 if (null === $message) {
126 return '';
127 }
128 $offset = 0;
129 $output = '';
130 $openTagRegex = '[a-z](?:[^\\\\<>]*+ | \\\\.)*';
131 $closeTagRegex = '[a-z][^<>]*+';
132 $currentLineLength = 0;
133 \preg_match_all("#<(({$openTagRegex}) | /({$closeTagRegex})?)>#ix", $message, $matches, \PREG_OFFSET_CAPTURE);
134 foreach ($matches[0] as $i => $match) {
135 $pos = $match[1];
136 $text = $match[0];
137 if (0 != $pos && '\\' == $message[$pos - 1]) {
138 continue;
139 }
140 // add the text up to the next tag
141 $output .= $this->applyCurrentStyle(\substr($message, $offset, $pos - $offset), $output, $width, $currentLineLength);
142 $offset = $pos + \strlen($text);
143 // opening tag?
144 if ($open = '/' != $text[1]) {
145 $tag = $matches[1][$i][0];
146 } else {
147 $tag = $matches[3][$i][0] ?? '';
148 }
149 if (!$open && !$tag) {
150 // </>
151 $this->styleStack->pop();
152 } elseif (null === ($style = $this->createStyleFromString($tag))) {
153 $output .= $this->applyCurrentStyle($text, $output, $width, $currentLineLength);
154 } elseif ($open) {
155 $this->styleStack->push($style);
156 } else {
157 $this->styleStack->pop($style);
158 }
159 }
160 $output .= $this->applyCurrentStyle(\substr($message, $offset), $output, $width, $currentLineLength);
161 return \strtr($output, ["\x00" => '\\', '\\<' => '<', '\\>' => '>']);
162 }
163 /**
164 * @return OutputFormatterStyleStack
165 */
166 public function getStyleStack()
167 {
168 return $this->styleStack;
169 }
170 /**
171 * Tries to create new style instance from string.
172 */
173 private function createStyleFromString(string $string) : ?OutputFormatterStyleInterface
174 {
175 if (isset($this->styles[$string])) {
176 return $this->styles[$string];
177 }
178 if (!\preg_match_all('/([^=]+)=([^;]+)(;|$)/', $string, $matches, \PREG_SET_ORDER)) {
179 return null;
180 }
181 $style = new OutputFormatterStyle();
182 foreach ($matches as $match) {
183 \array_shift($match);
184 $match[0] = \strtolower($match[0]);
185 if ('fg' == $match[0]) {
186 $style->setForeground(\strtolower($match[1]));
187 } elseif ('bg' == $match[0]) {
188 $style->setBackground(\strtolower($match[1]));
189 } elseif ('href' === $match[0]) {
190 $url = \preg_replace('{\\\\([<>])}', '$1', $match[1]);
191 $style->setHref($url);
192 } elseif ('options' === $match[0]) {
193 \preg_match_all('([^,;]+)', \strtolower($match[1]), $options);
194 $options = \array_shift($options);
195 foreach ($options as $option) {
196 $style->setOption($option);
197 }
198 } else {
199 return null;
200 }
201 }
202 return $style;
203 }
204 /**
205 * Applies current style from stack to text, if must be applied.
206 */
207 private function applyCurrentStyle(string $text, string $current, int $width, int &$currentLineLength) : string
208 {
209 if ('' === $text) {
210 return '';
211 }
212 if (!$width) {
213 return $this->isDecorated() ? $this->styleStack->getCurrent()->apply($text) : $text;
214 }
215 if (!$currentLineLength && '' !== $current) {
216 $text = \ltrim($text);
217 }
218 if ($currentLineLength) {
219 $prefix = \substr($text, 0, $i = $width - $currentLineLength) . "\n";
220 $text = \substr($text, $i);
221 } else {
222 $prefix = '';
223 }
224 \preg_match('~(\\n)$~', $text, $matches);
225 $text = $prefix . \preg_replace('~([^\\n]{' . $width . '})\\ *~', "\$1\n", $text);
226 $text = \rtrim($text, "\n") . ($matches[1] ?? '');
227 if (!$currentLineLength && '' !== $current && "\n" !== \substr($current, -1)) {
228 $text = "\n" . $text;
229 }
230 $lines = \explode("\n", $text);
231 foreach ($lines as $line) {
232 $currentLineLength += \strlen($line);
233 if ($width <= $currentLineLength) {
234 $currentLineLength = 0;
235 }
236 }
237 if ($this->isDecorated()) {
238 foreach ($lines as $i => $line) {
239 $lines[$i] = $this->styleStack->getCurrent()->apply($line);
240 }
241 }
242 return \implode("\n", $lines);
243 }
244 }
245