PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.2.0
Independent Analytics – WordPress Analytics Plugin v2.2.0
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 / Style / SymfonyStyle.php
independent-analytics / vendor / symfony / console / Style Last commit date
OutputStyle.php 2 years ago StyleInterface.php 2 years ago SymfonyStyle.php 2 years ago
SymfonyStyle.php
436 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\Style;
12
13 use IAWP_SCOPED\Symfony\Component\Console\Exception\InvalidArgumentException;
14 use IAWP_SCOPED\Symfony\Component\Console\Exception\RuntimeException;
15 use IAWP_SCOPED\Symfony\Component\Console\Formatter\OutputFormatter;
16 use IAWP_SCOPED\Symfony\Component\Console\Helper\Helper;
17 use IAWP_SCOPED\Symfony\Component\Console\Helper\ProgressBar;
18 use IAWP_SCOPED\Symfony\Component\Console\Helper\SymfonyQuestionHelper;
19 use IAWP_SCOPED\Symfony\Component\Console\Helper\Table;
20 use IAWP_SCOPED\Symfony\Component\Console\Helper\TableCell;
21 use IAWP_SCOPED\Symfony\Component\Console\Helper\TableSeparator;
22 use IAWP_SCOPED\Symfony\Component\Console\Input\InputInterface;
23 use IAWP_SCOPED\Symfony\Component\Console\Output\ConsoleOutputInterface;
24 use IAWP_SCOPED\Symfony\Component\Console\Output\OutputInterface;
25 use IAWP_SCOPED\Symfony\Component\Console\Output\TrimmedBufferOutput;
26 use IAWP_SCOPED\Symfony\Component\Console\Question\ChoiceQuestion;
27 use IAWP_SCOPED\Symfony\Component\Console\Question\ConfirmationQuestion;
28 use IAWP_SCOPED\Symfony\Component\Console\Question\Question;
29 use IAWP_SCOPED\Symfony\Component\Console\Terminal;
30 /**
31 * Output decorator helpers for the Symfony Style Guide.
32 *
33 * @author Kevin Bond <kevinbond@gmail.com>
34 * @internal
35 */
36 class SymfonyStyle extends OutputStyle
37 {
38 public const MAX_LINE_LENGTH = 120;
39 private $input;
40 private $output;
41 private $questionHelper;
42 private $progressBar;
43 private $lineLength;
44 private $bufferedOutput;
45 public function __construct(InputInterface $input, OutputInterface $output)
46 {
47 $this->input = $input;
48 $this->bufferedOutput = new TrimmedBufferOutput(\DIRECTORY_SEPARATOR === '\\' ? 4 : 2, $output->getVerbosity(), \false, clone $output->getFormatter());
49 // Windows cmd wraps lines as soon as the terminal width is reached, whether there are following chars or not.
50 $width = (new Terminal())->getWidth() ?: self::MAX_LINE_LENGTH;
51 $this->lineLength = \min($width - (int) (\DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
52 parent::__construct($this->output = $output);
53 }
54 /**
55 * Formats a message as a block of text.
56 *
57 * @param string|array $messages The message to write in the block
58 */
59 public function block($messages, string $type = null, string $style = null, string $prefix = ' ', bool $padding = \false, bool $escape = \true)
60 {
61 $messages = \is_array($messages) ? \array_values($messages) : [$messages];
62 $this->autoPrependBlock();
63 $this->writeln($this->createBlock($messages, $type, $style, $prefix, $padding, $escape));
64 $this->newLine();
65 }
66 /**
67 * {@inheritdoc}
68 */
69 public function title(string $message)
70 {
71 $this->autoPrependBlock();
72 $this->writeln([\sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)), \sprintf('<comment>%s</>', \str_repeat('=', Helper::width(Helper::removeDecoration($this->getFormatter(), $message))))]);
73 $this->newLine();
74 }
75 /**
76 * {@inheritdoc}
77 */
78 public function section(string $message)
79 {
80 $this->autoPrependBlock();
81 $this->writeln([\sprintf('<comment>%s</>', OutputFormatter::escapeTrailingBackslash($message)), \sprintf('<comment>%s</>', \str_repeat('-', Helper::width(Helper::removeDecoration($this->getFormatter(), $message))))]);
82 $this->newLine();
83 }
84 /**
85 * {@inheritdoc}
86 */
87 public function listing(array $elements)
88 {
89 $this->autoPrependText();
90 $elements = \array_map(function ($element) {
91 return \sprintf(' * %s', $element);
92 }, $elements);
93 $this->writeln($elements);
94 $this->newLine();
95 }
96 /**
97 * {@inheritdoc}
98 */
99 public function text($message)
100 {
101 $this->autoPrependText();
102 $messages = \is_array($message) ? \array_values($message) : [$message];
103 foreach ($messages as $message) {
104 $this->writeln(\sprintf(' %s', $message));
105 }
106 }
107 /**
108 * Formats a command comment.
109 *
110 * @param string|array $message
111 */
112 public function comment($message)
113 {
114 $this->block($message, null, null, '<fg=default;bg=default> // </>', \false, \false);
115 }
116 /**
117 * {@inheritdoc}
118 */
119 public function success($message)
120 {
121 $this->block($message, 'OK', 'fg=black;bg=green', ' ', \true);
122 }
123 /**
124 * {@inheritdoc}
125 */
126 public function error($message)
127 {
128 $this->block($message, 'ERROR', 'fg=white;bg=red', ' ', \true);
129 }
130 /**
131 * {@inheritdoc}
132 */
133 public function warning($message)
134 {
135 $this->block($message, 'WARNING', 'fg=black;bg=yellow', ' ', \true);
136 }
137 /**
138 * {@inheritdoc}
139 */
140 public function note($message)
141 {
142 $this->block($message, 'NOTE', 'fg=yellow', ' ! ');
143 }
144 /**
145 * Formats an info message.
146 *
147 * @param string|array $message
148 */
149 public function info($message)
150 {
151 $this->block($message, 'INFO', 'fg=green', ' ', \true);
152 }
153 /**
154 * {@inheritdoc}
155 */
156 public function caution($message)
157 {
158 $this->block($message, 'CAUTION', 'fg=white;bg=red', ' ! ', \true);
159 }
160 /**
161 * {@inheritdoc}
162 */
163 public function table(array $headers, array $rows)
164 {
165 $this->createTable()->setHeaders($headers)->setRows($rows)->render();
166 $this->newLine();
167 }
168 /**
169 * Formats a horizontal table.
170 */
171 public function horizontalTable(array $headers, array $rows)
172 {
173 $this->createTable()->setHorizontal(\true)->setHeaders($headers)->setRows($rows)->render();
174 $this->newLine();
175 }
176 /**
177 * Formats a list of key/value horizontally.
178 *
179 * Each row can be one of:
180 * * 'A title'
181 * * ['key' => 'value']
182 * * new TableSeparator()
183 *
184 * @param string|array|TableSeparator ...$list
185 */
186 public function definitionList(...$list)
187 {
188 $headers = [];
189 $row = [];
190 foreach ($list as $value) {
191 if ($value instanceof TableSeparator) {
192 $headers[] = $value;
193 $row[] = $value;
194 continue;
195 }
196 if (\is_string($value)) {
197 $headers[] = new TableCell($value, ['colspan' => 2]);
198 $row[] = null;
199 continue;
200 }
201 if (!\is_array($value)) {
202 throw new InvalidArgumentException('Value should be an array, string, or an instance of TableSeparator.');
203 }
204 $headers[] = \key($value);
205 $row[] = \current($value);
206 }
207 $this->horizontalTable($headers, [$row]);
208 }
209 /**
210 * {@inheritdoc}
211 */
212 public function ask(string $question, string $default = null, callable $validator = null)
213 {
214 $question = new Question($question, $default);
215 $question->setValidator($validator);
216 return $this->askQuestion($question);
217 }
218 /**
219 * {@inheritdoc}
220 */
221 public function askHidden(string $question, callable $validator = null)
222 {
223 $question = new Question($question);
224 $question->setHidden(\true);
225 $question->setValidator($validator);
226 return $this->askQuestion($question);
227 }
228 /**
229 * {@inheritdoc}
230 */
231 public function confirm(string $question, bool $default = \true)
232 {
233 return $this->askQuestion(new ConfirmationQuestion($question, $default));
234 }
235 /**
236 * {@inheritdoc}
237 */
238 public function choice(string $question, array $choices, $default = null)
239 {
240 if (null !== $default) {
241 $values = \array_flip($choices);
242 $default = $values[$default] ?? $default;
243 }
244 return $this->askQuestion(new ChoiceQuestion($question, $choices, $default));
245 }
246 /**
247 * {@inheritdoc}
248 */
249 public function progressStart(int $max = 0)
250 {
251 $this->progressBar = $this->createProgressBar($max);
252 $this->progressBar->start();
253 }
254 /**
255 * {@inheritdoc}
256 */
257 public function progressAdvance(int $step = 1)
258 {
259 $this->getProgressBar()->advance($step);
260 }
261 /**
262 * {@inheritdoc}
263 */
264 public function progressFinish()
265 {
266 $this->getProgressBar()->finish();
267 $this->newLine(2);
268 $this->progressBar = null;
269 }
270 /**
271 * {@inheritdoc}
272 */
273 public function createProgressBar(int $max = 0)
274 {
275 $progressBar = parent::createProgressBar($max);
276 if ('\\' !== \DIRECTORY_SEPARATOR || 'Hyper' === \getenv('TERM_PROGRAM')) {
277 $progressBar->setEmptyBarCharacter('');
278 // light shade character \u2591
279 $progressBar->setProgressCharacter('');
280 $progressBar->setBarCharacter('');
281 // dark shade character \u2593
282 }
283 return $progressBar;
284 }
285 /**
286 * @see ProgressBar::iterate()
287 */
288 public function progressIterate(iterable $iterable, int $max = null) : iterable
289 {
290 yield from $this->createProgressBar()->iterate($iterable, $max);
291 $this->newLine(2);
292 }
293 /**
294 * @return mixed
295 */
296 public function askQuestion(Question $question)
297 {
298 if ($this->input->isInteractive()) {
299 $this->autoPrependBlock();
300 }
301 if (!$this->questionHelper) {
302 $this->questionHelper = new SymfonyQuestionHelper();
303 }
304 $answer = $this->questionHelper->ask($this->input, $this, $question);
305 if ($this->input->isInteractive()) {
306 $this->newLine();
307 $this->bufferedOutput->write("\n");
308 }
309 return $answer;
310 }
311 /**
312 * {@inheritdoc}
313 */
314 public function writeln($messages, int $type = self::OUTPUT_NORMAL)
315 {
316 if (!\is_iterable($messages)) {
317 $messages = [$messages];
318 }
319 foreach ($messages as $message) {
320 parent::writeln($message, $type);
321 $this->writeBuffer($message, \true, $type);
322 }
323 }
324 /**
325 * {@inheritdoc}
326 */
327 public function write($messages, bool $newline = \false, int $type = self::OUTPUT_NORMAL)
328 {
329 if (!\is_iterable($messages)) {
330 $messages = [$messages];
331 }
332 foreach ($messages as $message) {
333 parent::write($message, $newline, $type);
334 $this->writeBuffer($message, $newline, $type);
335 }
336 }
337 /**
338 * {@inheritdoc}
339 */
340 public function newLine(int $count = 1)
341 {
342 parent::newLine($count);
343 $this->bufferedOutput->write(\str_repeat("\n", $count));
344 }
345 /**
346 * Returns a new instance which makes use of stderr if available.
347 *
348 * @return self
349 */
350 public function getErrorStyle()
351 {
352 return new self($this->input, $this->getErrorOutput());
353 }
354 public function createTable() : Table
355 {
356 $output = $this->output instanceof ConsoleOutputInterface ? $this->output->section() : $this->output;
357 $style = clone Table::getStyleDefinition('symfony-style-guide');
358 $style->setCellHeaderFormat('<info>%s</info>');
359 return (new Table($output))->setStyle($style);
360 }
361 private function getProgressBar() : ProgressBar
362 {
363 if (!$this->progressBar) {
364 throw new RuntimeException('The ProgressBar is not started.');
365 }
366 return $this->progressBar;
367 }
368 private function autoPrependBlock() : void
369 {
370 $chars = \substr(\str_replace(\PHP_EOL, "\n", $this->bufferedOutput->fetch()), -2);
371 if (!isset($chars[0])) {
372 $this->newLine();
373 // empty history, so we should start with a new line.
374 return;
375 }
376 // Prepend new line for each non LF chars (This means no blank line was output before)
377 $this->newLine(2 - \substr_count($chars, "\n"));
378 }
379 private function autoPrependText() : void
380 {
381 $fetched = $this->bufferedOutput->fetch();
382 // Prepend new line if last char isn't EOL:
383 if (!\str_ends_with($fetched, "\n")) {
384 $this->newLine();
385 }
386 }
387 private function writeBuffer(string $message, bool $newLine, int $type) : void
388 {
389 // We need to know if the last chars are PHP_EOL
390 $this->bufferedOutput->write($message, $newLine, $type);
391 }
392 private function createBlock(iterable $messages, string $type = null, string $style = null, string $prefix = ' ', bool $padding = \false, bool $escape = \false) : array
393 {
394 $indentLength = 0;
395 $prefixLength = Helper::width(Helper::removeDecoration($this->getFormatter(), $prefix));
396 $lines = [];
397 if (null !== $type) {
398 $type = \sprintf('[%s] ', $type);
399 $indentLength = \strlen($type);
400 $lineIndentation = \str_repeat(' ', $indentLength);
401 }
402 // wrap and add newlines for each element
403 foreach ($messages as $key => $message) {
404 if ($escape) {
405 $message = OutputFormatter::escape($message);
406 }
407 $decorationLength = Helper::width($message) - Helper::width(Helper::removeDecoration($this->getFormatter(), $message));
408 $messageLineLength = \min($this->lineLength - $prefixLength - $indentLength + $decorationLength, $this->lineLength);
409 $messageLines = \explode(\PHP_EOL, \wordwrap($message, $messageLineLength, \PHP_EOL, \true));
410 foreach ($messageLines as $messageLine) {
411 $lines[] = $messageLine;
412 }
413 if (\count($messages) > 1 && $key < \count($messages) - 1) {
414 $lines[] = '';
415 }
416 }
417 $firstLineIndex = 0;
418 if ($padding && $this->isDecorated()) {
419 $firstLineIndex = 1;
420 \array_unshift($lines, '');
421 $lines[] = '';
422 }
423 foreach ($lines as $i => &$line) {
424 if (null !== $type) {
425 $line = $firstLineIndex === $i ? $type . $line : $lineIndentation . $line;
426 }
427 $line = $prefix . $line;
428 $line .= \str_repeat(' ', \max($this->lineLength - Helper::width(Helper::removeDecoration($this->getFormatter(), $line)), 0));
429 if ($style) {
430 $line = \sprintf('<%s>%s</>', $style, $line);
431 }
432 }
433 return $lines;
434 }
435 }
436