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