1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Collector; |
6
|
|
|
|
7
|
|
|
use Throwable; |
8
|
|
|
use Yiisoft\ErrorHandler\Event\ApplicationError; |
9
|
|
|
|
10
|
|
|
final class ExceptionCollector implements SummaryCollectorInterface |
11
|
|
|
{ |
12
|
|
|
use CollectorTrait; |
13
|
|
|
|
14
|
|
|
private ?Throwable $exception = null; |
15
|
|
|
|
16
|
|
|
public function __construct(private TimelineCollector $timelineCollector) |
17
|
|
|
{ |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function getCollected(): array |
21
|
|
|
{ |
22
|
|
|
if ($this->exception === null) { |
23
|
|
|
return []; |
24
|
|
|
} |
25
|
|
|
$throwable = $this->exception; |
26
|
|
|
$exceptions = [ |
27
|
|
|
$throwable, |
28
|
|
|
]; |
29
|
|
|
while (($throwable = $throwable->getPrevious()) !== null) { |
30
|
|
|
$exceptions[] = $throwable; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return array_map([$this, 'serializeException'], $exceptions); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function collect(ApplicationError $error): void |
37
|
|
|
{ |
38
|
|
|
if (!$this->isActive()) { |
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->exception = $error->getThrowable(); |
43
|
|
|
$this->timelineCollector->collect($this, $error::class); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getSummary(): array |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
'exception' => $this->exception === null ? [] : [ |
50
|
|
|
'class' => $this->exception::class, |
51
|
|
|
'message' => $this->exception->getMessage(), |
52
|
|
|
'file' => $this->exception->getFile(), |
53
|
|
|
'line' => $this->exception->getLine(), |
54
|
|
|
'code' => $this->exception->getCode(), |
55
|
|
|
], |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function reset(): void |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$this->exception = null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function serializeException(Throwable $throwable): array |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
'class' => $throwable::class, |
68
|
|
|
'message' => $throwable->getMessage(), |
69
|
|
|
'file' => $throwable->getFile(), |
70
|
|
|
'line' => $throwable->getLine(), |
71
|
|
|
'code' => $throwable->getCode(), |
72
|
|
|
'trace' => $throwable->getTrace(), |
73
|
|
|
'traceAsString' => $throwable->getTraceAsString(), |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.