-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathConsole.php
More file actions
167 lines (137 loc) · 5.9 KB
/
Console.php
File metadata and controls
167 lines (137 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
declare(strict_types=1);
namespace Atk4\Core\ExceptionRenderer;
use Atk4\Core\Exception;
class Console extends RendererAbstract
{
private const RESET = "\e[0m";
private const FORMAT_BOLD = "\e[1m";
private const COLOR_BLACK = "\e[30m";
private const COLOR_RED = "\e[31m";
private const COLOR_GREEN = "\e[32m";
private const COLOR_YELLOW = "\e[33m";
private const BACKGROUND_COLOR_RED = "\e[41m";
private const BACKGROUND_COLOR_MAGENTA = "\e[45m";
private const COLOR_BRIGHT_RED = "\e[91m";
private const COLOR_BRIGHT_GREEN = "\e[92m";
/**
* @param list<self::FORMAT_*|self::COLOR_*|self::BACKGROUND_COLOR_*> $formats
*/
private function text(string $text, array $formats = []): string
{
$text = str_replace("\e", '', $text);
return $formats === []
? $text
: implode('', $formats) . $text . self::RESET;
}
private function optimizeText(string $value): string
{
$res = preg_replace("~\e\\[\\d{1,2}m\e\\[0m~", '', $value);
$res = preg_replace("~(?<=\e\\[\\d|\e\\[\\d{2})m\e\\[(\\d{1,2})(?=m)~", ';$1', $res);
return implode("\n", array_map(static fn ($v) => rtrim($v, ' '), explode("\n", $res)));
}
#[\Override]
protected function processAll(): void
{
parent::processAll();
$this->output = $this->optimizeText(self::RESET . $this->output);
}
#[\Override]
protected function processHeader(): void
{
$title = $this->getExceptionTitle();
$class = $this->formatClass(get_class($this->exception));
$this->output .= $this->text('--[ ' . $title . ' ]', [self::FORMAT_BOLD, self::BACKGROUND_COLOR_RED]) . "\n"
. $this->text($class . ': ')
. $this->text($this->getExceptionMessage(), [self::FORMAT_BOLD, self::COLOR_BLACK])
. ($this->exception->getCode() !== 0 ? ' ' . $this->text('[code: ' . $this->exception->getCode() . ']', [self::COLOR_RED]) : '');
}
#[\Override]
protected function processParams(): void
{
if (!$this->exception instanceof Exception) {
return;
}
/** @var Exception $exception */
$exception = $this->exception;
if (count($exception->getParams()) === 0) {
return;
}
foreach ($exception->getParams() as $key => $val) {
$key = str_pad($key, 19, ' ', \STR_PAD_LEFT);
$this->output .= "\n" . $this->text($key . ': ' . static::toSafeString($val), [self::COLOR_BRIGHT_RED]);
}
}
#[\Override]
protected function processSolutions(): void
{
if (!$this->exception instanceof Exception) {
return;
}
if (count($this->exception->getSolutions()) === 0) {
return;
}
foreach ($this->exception->getSolutions() as $key => $val) {
$this->output .= "\n" . $this->text('Solution: ' . $val, [self::COLOR_BRIGHT_GREEN]);
}
}
#[\Override]
protected function processStackTrace(): void
{
$this->output .= "\n" . $this->text('--[ Stack Trace ]', [self::FORMAT_BOLD, self::BACKGROUND_COLOR_RED]) . "\n";
$this->processStackTraceInternal();
}
#[\Override]
protected function processStackTraceInternal(): void
{
$text = '{FILE}:'
. $this->text('{LINE}', [self::COLOR_RED]) . ' '
. '{OBJECT} {CLASS}{FUNCTION}{FUNCTION_ARGS}';
$inAtk = true;
$shortTrace = $this->getStackTrace(true);
$isShortened = array_key_last($shortTrace) > 0 && array_key_last($shortTrace) !== 'self';
foreach ($shortTrace as $index => $call) {
$call = $this->parseStackTraceFrame($call);
$escapeFrame = false;
if ($inAtk && $call['file'] !== '' && !preg_match('~atk4[/\\\][^/\\\]+[/\\\]src[/\\\]~', $call['file'])) {
$escapeFrame = true;
$inAtk = false;
}
$functionColor = $escapeFrame ? self::COLOR_RED : self::COLOR_YELLOW;
$tokens = [
'{FILE}' => $this->text(str_pad(mb_substr($call['file_rel'], -40), 40, ' ', \STR_PAD_LEFT)),
'{LINE}' => $this->text(str_pad($call['line'], 4, ' ', \STR_PAD_LEFT)),
'{OBJECT}' => $call['object'] !== null ? ' - ' . $this->text($call['object_formatted'], [self::COLOR_GREEN]) : '',
'{CLASS}' => $call['class'] !== null ? $this->text($call['class_formatted'] . '::', [self::COLOR_GREEN]) : '',
'{FUNCTION}' => $call['function'] !== null ? $this->text($call['function'], [$functionColor]) : '',
];
if ($index === 'self') {
$tokens['{FUNCTION_ARGS}'] = '';
} elseif (count($call['args']) === 0) {
$tokens['{FUNCTION_ARGS}'] = $this->text('()', [$functionColor]);
} else {
if ($escapeFrame) {
$tokens['{FUNCTION_ARGS}'] = $this->text('(' . "\n" . str_repeat(' ', 40) . implode(',' . "\n" . str_repeat(' ', 40), array_map(static function ($arg) {
return static::toSafeString($arg);
}, $call['args'])) . ')', [self::COLOR_RED]);
} else {
$tokens['{FUNCTION_ARGS}'] = $this->text('(...)', [$functionColor]);
}
}
$this->output .= $this->replaceTokens($text, $tokens) . "\n";
}
if ($isShortened) {
$this->output .= str_pad('...', 40, ' ', \STR_PAD_LEFT) . "\n";
}
}
#[\Override]
protected function processPreviousException(): void
{
if (!$this->exception->getPrevious()) {
return;
}
$this->output .= "\n"
. $this->text('Caused by Previous Exception:', [self::FORMAT_BOLD, self::BACKGROUND_COLOR_MAGENTA]) . "\n"
. ((string) (new static($this->exception->getPrevious(), $this->adapter, $this->exception)));
}
}