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