|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Exceptions\Renderer; |
|
6
|
|
|
|
|
7
|
|
|
use Spiral\Exceptions\Style\PlainStyle; |
|
8
|
|
|
use Spiral\Exceptions\Verbosity; |
|
9
|
|
|
|
|
10
|
|
|
final class PlainRenderer extends AbstractRenderer |
|
11
|
|
|
{ |
|
12
|
|
|
protected const FORMATS = ['text/plain', 'text', 'plain', 'cli', 'console']; |
|
13
|
|
|
// Lines to show around targeted line. |
|
14
|
|
|
private const SHOW_LINES = 2; |
|
15
|
|
|
|
|
16
|
9 |
|
private array $lines = []; |
|
17
|
|
|
|
|
18
|
|
|
public function render( |
|
19
|
|
|
\Throwable $exception, |
|
20
|
|
|
?Verbosity $verbosity = null, |
|
21
|
9 |
|
string $format = null |
|
22
|
9 |
|
): string { |
|
23
|
9 |
|
$verbosity ??= $this->defaultVerbosity; |
|
24
|
9 |
|
$exceptions = [$exception]; |
|
25
|
9 |
|
while ($exception = $exception->getPrevious()) { |
|
26
|
9 |
|
$exceptions[] = $exception; |
|
27
|
9 |
|
} |
|
28
|
9 |
|
|
|
29
|
|
|
$exceptions = \array_reverse($exceptions); |
|
30
|
9 |
|
|
|
31
|
3 |
|
$result = []; |
|
32
|
6 |
|
$rootDir = \getcwd(); |
|
33
|
1 |
|
|
|
34
|
|
|
foreach ($exceptions as $exception) { |
|
35
|
|
|
$file = \str_starts_with($exception->getFile(), $rootDir) |
|
36
|
9 |
|
? \substr($exception->getFile(), \strlen($rootDir) + 1) |
|
37
|
|
|
: $exception->getFile(); |
|
38
|
|
|
|
|
39
|
|
|
$row = \sprintf( |
|
40
|
|
|
"[%s]\n%s in %s:%s\n", |
|
41
|
|
|
$exception::class, |
|
42
|
4 |
|
$exception->getMessage(), |
|
43
|
|
|
$file, |
|
44
|
4 |
|
$exception->getLine(), |
|
45
|
4 |
|
); |
|
46
|
|
|
|
|
47
|
|
|
if ($verbosity->value >= Verbosity::DEBUG->value) { |
|
48
|
|
|
$row .= $this->renderTrace($exception, new Highlighter(new PlainStyle())); |
|
49
|
4 |
|
} elseif ($verbosity->value >= Verbosity::VERBOSE->value) { |
|
50
|
|
|
$row .= $this->renderTrace($exception); |
|
51
|
4 |
|
} |
|
52
|
4 |
|
|
|
53
|
4 |
|
$result[] = $row; |
|
54
|
4 |
|
} |
|
55
|
4 |
|
|
|
56
|
4 |
|
$this->lines = []; |
|
57
|
4 |
|
|
|
58
|
4 |
|
return \implode('', \array_reverse($result)); |
|
59
|
|
|
} |
|
60
|
4 |
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Render exception call stack. |
|
63
|
4 |
|
*/ |
|
64
|
4 |
|
private function renderTrace(\Throwable $e, Highlighter $h = null): string |
|
65
|
|
|
{ |
|
66
|
|
|
$stacktrace = $this->getStacktrace($e); |
|
67
|
|
|
if ($stacktrace === []) { |
|
68
|
|
|
return ''; |
|
69
|
4 |
|
} |
|
70
|
|
|
|
|
71
|
4 |
|
$result = "\n"; |
|
72
|
3 |
|
$rootDir = \getcwd(); |
|
73
|
3 |
|
|
|
74
|
3 |
|
$pad = \strlen((string)\count($stacktrace)); |
|
75
|
3 |
|
|
|
76
|
3 |
|
foreach ($stacktrace as $i => $trace) { |
|
77
|
|
|
if (isset($trace['type'], $trace['class'])) { |
|
78
|
|
|
$line = \sprintf( |
|
79
|
|
|
'%s. %s%s%s()', |
|
80
|
4 |
|
\str_pad((string)($i + 1), $pad, ' ', \STR_PAD_LEFT), |
|
81
|
|
|
$trace['class'], |
|
82
|
|
|
$trace['type'], |
|
83
|
|
|
$trace['function'] |
|
84
|
|
|
); |
|
85
|
|
|
} else { |
|
86
|
|
|
$line = $trace['function']; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (isset($trace['file'])) { |
|
90
|
|
|
$file = \str_starts_with($trace['file'], $rootDir) |
|
91
|
|
|
? \substr($trace['file'], \strlen($rootDir) + 1) |
|
92
|
|
|
: $trace['file']; |
|
93
|
|
|
|
|
94
|
|
|
$line .= \sprintf(' at %s:%s', $file, $trace['line']); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (\in_array($line, $this->lines, true)) { |
|
98
|
|
|
continue; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$this->lines[] = $line; |
|
102
|
|
|
|
|
103
|
|
|
$result .= $line . "\n"; |
|
104
|
|
|
|
|
105
|
|
|
if ($h !== null && !empty($trace['file'])) { |
|
106
|
|
|
$result .= $h->highlightLines( |
|
107
|
|
|
\file_get_contents($trace['file']), |
|
108
|
|
|
$trace['line'], |
|
109
|
|
|
self::SHOW_LINES |
|
110
|
|
|
) . "\n"; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $result . "\n"; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|