1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Facade\FlareClient; |
4
|
|
|
|
5
|
|
|
use Facade\FlareClient\Enums\GroupingTypes; |
6
|
|
|
use Throwable; |
7
|
|
|
use Facade\FlareClient\Glows\Glow; |
8
|
|
|
use Facade\IgnitionContracts\Solution; |
9
|
|
|
use Facade\FlareClient\Concerns\UsesTime; |
10
|
|
|
use Facade\FlareClient\Concerns\HasContext; |
11
|
|
|
use Facade\FlareClient\Stacktrace\Stacktrace; |
12
|
|
|
use Facade\FlareClient\Context\ContextInterface; |
13
|
|
|
use Facade\FlareClient\Solutions\ReportSolution; |
14
|
|
|
use Facade\FlareClient\Contracts\ProvidesFlareContext; |
15
|
|
|
|
16
|
|
|
class Report |
17
|
|
|
{ |
18
|
|
|
use UsesTime, HasContext; |
19
|
|
|
|
20
|
|
|
/** @var \Facade\FlareClient\Stacktrace\Stacktrace */ |
21
|
|
|
private $stacktrace; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
private $exceptionClass; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
private $message; |
28
|
|
|
|
29
|
|
|
/** @var array */ |
30
|
|
|
private $glows = []; |
31
|
|
|
|
32
|
|
|
/** @var array */ |
33
|
|
|
private $solutions = []; |
34
|
|
|
|
35
|
|
|
/** @var ContextInterface */ |
36
|
|
|
private $context; |
37
|
|
|
|
38
|
|
|
/** @var string */ |
39
|
|
|
private $applicationPath; |
40
|
|
|
|
41
|
|
|
/** @var array */ |
42
|
|
|
private $userProvidedContext = []; |
43
|
|
|
|
44
|
|
|
/** @var array */ |
45
|
|
|
private $exceptionContext = []; |
46
|
|
|
|
47
|
|
|
/** @var Throwable */ |
48
|
|
|
private $throwable; |
49
|
|
|
|
50
|
|
|
/** @var string */ |
51
|
|
|
private $notifierName; |
52
|
|
|
|
53
|
|
|
/** @var string */ |
54
|
|
|
private $languageVersion; |
55
|
|
|
|
56
|
|
|
/** @var string */ |
57
|
|
|
private $frameworkVersion; |
58
|
|
|
|
59
|
|
|
/** @var int */ |
60
|
|
|
private $openFrameIndex; |
61
|
|
|
|
62
|
|
|
/** @var string */ |
63
|
|
|
private $groupBy; |
64
|
|
|
|
65
|
|
|
public static function createForThrowable(Throwable $throwable, ContextInterface $context, ?string $applicationPath = null): self |
66
|
|
|
{ |
67
|
|
|
return (new static()) |
68
|
|
|
->setApplicationPath($applicationPath) |
69
|
|
|
->throwable($throwable) |
70
|
|
|
->useContext($context) |
71
|
|
|
->exceptionClass(get_class($throwable)) |
72
|
|
|
->message($throwable->getMessage()) |
73
|
|
|
->stackTrace(Stacktrace::createForThrowable($throwable, $applicationPath)) |
74
|
|
|
->exceptionContext($throwable); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function createForMessage(string $message, string $logLevel, ContextInterface $context, ?string $applicationPath = null): self |
78
|
|
|
{ |
79
|
|
|
$stacktrace = Stacktrace::create($applicationPath); |
80
|
|
|
|
81
|
|
|
return (new static()) |
82
|
|
|
->setApplicationPath($applicationPath) |
83
|
|
|
->message($message) |
84
|
|
|
->useContext($context) |
85
|
|
|
->exceptionClass($logLevel) |
86
|
|
|
->stacktrace($stacktrace) |
87
|
|
|
->openFrameIndex($stacktrace->firstApplicationFrameIndex()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function exceptionClass(string $exceptionClass) |
91
|
|
|
{ |
92
|
|
|
$this->exceptionClass = $exceptionClass; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getExceptionClass(): string |
98
|
|
|
{ |
99
|
|
|
return $this->exceptionClass; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function throwable(Throwable $throwable) |
103
|
|
|
{ |
104
|
|
|
$this->throwable = $throwable; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getThrowable(): ?Throwable |
110
|
|
|
{ |
111
|
|
|
return $this->throwable; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function message(string $message) |
115
|
|
|
{ |
116
|
|
|
$this->message = $message; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getMessage(): string |
122
|
|
|
{ |
123
|
|
|
return $this->message; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function stacktrace(Stacktrace $stacktrace) |
127
|
|
|
{ |
128
|
|
|
$this->stacktrace = $stacktrace; |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getStacktrace(): Stacktrace |
134
|
|
|
{ |
135
|
|
|
return $this->stacktrace; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function notifierName(string $notifierName) |
139
|
|
|
{ |
140
|
|
|
$this->notifierName = $notifierName; |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function languageVersion(string $languageVersion) |
146
|
|
|
{ |
147
|
|
|
$this->languageVersion = $languageVersion; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function frameworkVersion(string $frameworkVersion) |
153
|
|
|
{ |
154
|
|
|
$this->frameworkVersion = $frameworkVersion; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function useContext(ContextInterface $request) |
160
|
|
|
{ |
161
|
|
|
$this->context = $request; |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function openFrameIndex(?int $index) |
167
|
|
|
{ |
168
|
|
|
$this->openFrameIndex = $index; |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function setApplicationPath(?string $applicationPath) |
174
|
|
|
{ |
175
|
|
|
$this->applicationPath = $applicationPath; |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function getApplicationPath(): ?string |
181
|
|
|
{ |
182
|
|
|
return $this->applicationPath; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function view(?View $view) |
186
|
|
|
{ |
187
|
|
|
$this->view = $view; |
|
|
|
|
188
|
|
|
|
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function addGlow(Glow $glow) |
193
|
|
|
{ |
194
|
|
|
$this->glows[] = $glow->toArray(); |
195
|
|
|
|
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function addSolution(Solution $solution) |
200
|
|
|
{ |
201
|
|
|
$this->solutions[] = ReportSolution::fromSolution($solution)->toArray(); |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function userProvidedContext(array $userProvidedContext) |
207
|
|
|
{ |
208
|
|
|
$this->userProvidedContext = $userProvidedContext; |
209
|
|
|
|
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function groupByTopFrame() |
214
|
|
|
{ |
215
|
|
|
$this->groupBy = GroupingTypes::TOP_FRAME; |
216
|
|
|
|
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function groupByException() |
221
|
|
|
{ |
222
|
|
|
$this->groupBy = GroupingTypes::EXCEPTION; |
223
|
|
|
|
224
|
|
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function allContext(): array |
228
|
|
|
{ |
229
|
|
|
$context = $this->context->toArray(); |
230
|
|
|
|
231
|
|
|
$context = array_merge_recursive_distinct($context, $this->exceptionContext); |
232
|
|
|
|
233
|
|
|
return array_merge_recursive_distinct($context, $this->userProvidedContext); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
private function exceptionContext(Throwable $throwable) |
237
|
|
|
{ |
238
|
|
|
if ($throwable instanceof ProvidesFlareContext) { |
239
|
|
|
$this->exceptionContext = $throwable->context(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function toArray() |
246
|
|
|
{ |
247
|
|
|
return [ |
248
|
|
|
'notifier' => $this->notifierName ?? 'Flare Client', |
249
|
|
|
'language' => 'PHP', |
250
|
|
|
'framework_version' => $this->frameworkVersion, |
251
|
|
|
'language_version' => $this->languageVersion ?? phpversion(), |
252
|
|
|
'exception_class' => $this->exceptionClass, |
253
|
|
|
'seen_at' => $this->getCurrentTime(), |
254
|
|
|
'message' => $this->message, |
255
|
|
|
'glows' => $this->glows, |
256
|
|
|
'solutions' => $this->solutions, |
257
|
|
|
'stacktrace' => $this->stacktrace->toArray(), |
258
|
|
|
'context' => $this->allContext(), |
259
|
|
|
'stage' => $this->stage, |
260
|
|
|
'message_level' => $this->messageLevel, |
261
|
|
|
'open_frame_index' => $this->openFrameIndex, |
262
|
|
|
'group_by' => $this->groupBy ?? GroupingTypes::TOP_FRAME, |
263
|
|
|
'application_path' => $this->applicationPath, |
264
|
|
|
]; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: