DebugFormatterHelper.php
1 year ago
DescriptorHelper.php
1 year ago
Dumper.php
1 year ago
FormatterHelper.php
1 year ago
Helper.php
1 year ago
HelperInterface.php
2 years ago
HelperSet.php
2 years ago
InputAwareHelper.php
2 years ago
ProcessHelper.php
2 years ago
ProgressBar.php
1 year ago
ProgressIndicator.php
1 year ago
QuestionHelper.php
1 year ago
SymfonyQuestionHelper.php
1 year ago
Table.php
1 year ago
TableCell.php
2 years ago
TableCellStyle.php
2 years ago
TableRows.php
2 years ago
TableSeparator.php
2 years ago
TableStyle.php
1 year ago
DebugFormatterHelper.php
93 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 Matomo\Dependencies\Symfony\Component\Console\Helper; |
| 12 | |
| 13 | /** |
| 14 | * Helps outputting debug information when running an external program from a command. |
| 15 | * |
| 16 | * An external program can be a Process, an HTTP request, or anything else. |
| 17 | * |
| 18 | * @author Fabien Potencier <fabien@symfony.com> |
| 19 | */ |
| 20 | class DebugFormatterHelper extends Helper |
| 21 | { |
| 22 | private const COLORS = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'default']; |
| 23 | private $started = []; |
| 24 | private $count = -1; |
| 25 | /** |
| 26 | * Starts a debug formatting session. |
| 27 | * |
| 28 | * @return string |
| 29 | */ |
| 30 | public function start(string $id, string $message, string $prefix = 'RUN') |
| 31 | { |
| 32 | $this->started[$id] = ['border' => ++$this->count % \count(self::COLORS)]; |
| 33 | return sprintf("%s<bg=blue;fg=white> %s </> <fg=blue>%s</>\n", $this->getBorder($id), $prefix, $message); |
| 34 | } |
| 35 | /** |
| 36 | * Adds progress to a formatting session. |
| 37 | * |
| 38 | * @return string |
| 39 | */ |
| 40 | public function progress(string $id, string $buffer, bool $error = \false, string $prefix = 'OUT', string $errorPrefix = 'ERR') |
| 41 | { |
| 42 | $message = ''; |
| 43 | if ($error) { |
| 44 | if (isset($this->started[$id]['out'])) { |
| 45 | $message .= "\n"; |
| 46 | unset($this->started[$id]['out']); |
| 47 | } |
| 48 | if (!isset($this->started[$id]['err'])) { |
| 49 | $message .= sprintf('%s<bg=red;fg=white> %s </> ', $this->getBorder($id), $errorPrefix); |
| 50 | $this->started[$id]['err'] = \true; |
| 51 | } |
| 52 | $message .= str_replace("\n", sprintf("\n%s<bg=red;fg=white> %s </> ", $this->getBorder($id), $errorPrefix), $buffer); |
| 53 | } else { |
| 54 | if (isset($this->started[$id]['err'])) { |
| 55 | $message .= "\n"; |
| 56 | unset($this->started[$id]['err']); |
| 57 | } |
| 58 | if (!isset($this->started[$id]['out'])) { |
| 59 | $message .= sprintf('%s<bg=green;fg=white> %s </> ', $this->getBorder($id), $prefix); |
| 60 | $this->started[$id]['out'] = \true; |
| 61 | } |
| 62 | $message .= str_replace("\n", sprintf("\n%s<bg=green;fg=white> %s </> ", $this->getBorder($id), $prefix), $buffer); |
| 63 | } |
| 64 | return $message; |
| 65 | } |
| 66 | /** |
| 67 | * Stops a formatting session. |
| 68 | * |
| 69 | * @return string |
| 70 | */ |
| 71 | public function stop(string $id, string $message, bool $successful, string $prefix = 'RES') |
| 72 | { |
| 73 | $trailingEOL = isset($this->started[$id]['out']) || isset($this->started[$id]['err']) ? "\n" : ''; |
| 74 | if ($successful) { |
| 75 | return sprintf("%s%s<bg=green;fg=white> %s </> <fg=green>%s</>\n", $trailingEOL, $this->getBorder($id), $prefix, $message); |
| 76 | } |
| 77 | $message = sprintf("%s%s<bg=red;fg=white> %s </> <fg=red>%s</>\n", $trailingEOL, $this->getBorder($id), $prefix, $message); |
| 78 | unset($this->started[$id]['out'], $this->started[$id]['err']); |
| 79 | return $message; |
| 80 | } |
| 81 | private function getBorder(string $id) : string |
| 82 | { |
| 83 | return sprintf('<bg=%s> </>', self::COLORS[$this->started[$id]['border']]); |
| 84 | } |
| 85 | /** |
| 86 | * {@inheritdoc} |
| 87 | */ |
| 88 | public function getName() |
| 89 | { |
| 90 | return 'debug_formatter'; |
| 91 | } |
| 92 | } |
| 93 |