Report.php
8.1 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
namespace Facade\FlareClient;
use Facade\FlareClient\Concerns\HasContext;
use Facade\FlareClient\Concerns\UsesTime;
use Facade\FlareClient\Context\ContextInterface;
use Facade\FlareClient\Contracts\ProvidesFlareContext;
use Facade\FlareClient\Enums\GroupingTypes;
use Facade\FlareClient\Glows\Glow;
use Facade\FlareClient\Solutions\ReportSolution;
use Facade\FlareClient\Stacktrace\Stacktrace;
use Facade\IgnitionContracts\Solution;
use Throwable;
class Report
{
use UsesTime;
use HasContext;
/** @var \Facade\FlareClient\Stacktrace\Stacktrace */
private $stacktrace;
/** @var string */
private $exceptionClass;
/** @var string */
private $message;
/** @var array */
private $glows = [];
/** @var array */
private $solutions = [];
/** @var ContextInterface */
private $context;
/** @var string */
private $applicationPath;
/** @var ?string */
private $applicationVersion;
/** @var array */
private $userProvidedContext = [];
/** @var array */
private $exceptionContext = [];
/** @var Throwable */
private $throwable;
/** @var string */
private $notifierName;
/** @var string */
private $languageVersion;
/** @var string */
private $frameworkVersion;
/** @var int */
private $openFrameIndex;
/** @var string */
private $groupBy ;
/** @var string */
private $trackingUuid;
/** @var null string|null */
public static $fakeTrackingUuid = null;
public static function createForThrowable(
Throwable $throwable,
ContextInterface $context,
?string $applicationPath = null,
?string $version = null
): self {
return (new static())
->setApplicationPath($applicationPath)
->throwable($throwable)
->useContext($context)
->exceptionClass(self::getClassForThrowable($throwable))
->message($throwable->getMessage())
->stackTrace(Stacktrace::createForThrowable($throwable, $applicationPath))
->exceptionContext($throwable)
->setApplicationVersion($version);
}
protected static function getClassForThrowable(Throwable $throwable): string
{
if ($throwable instanceof \Facade\Ignition\Exceptions\ViewException) {
if ($previous = $throwable->getPrevious()) {
return get_class($previous);
}
}
return get_class($throwable);
}
public static function createForMessage(string $message, string $logLevel, ContextInterface $context, ?string $applicationPath = null): self
{
$stacktrace = Stacktrace::create($applicationPath);
return (new static())
->setApplicationPath($applicationPath)
->message($message)
->useContext($context)
->exceptionClass($logLevel)
->stacktrace($stacktrace)
->openFrameIndex($stacktrace->firstApplicationFrameIndex());
}
public function __construct()
{
$this->trackingUuid = self::$fakeTrackingUuid ?? $this->generateUuid();
}
public function trackingUuid(): string
{
return $this->trackingUuid;
}
public function exceptionClass(string $exceptionClass)
{
$this->exceptionClass = $exceptionClass;
return $this;
}
public function getExceptionClass(): string
{
return $this->exceptionClass;
}
public function throwable(Throwable $throwable)
{
$this->throwable = $throwable;
return $this;
}
public function getThrowable(): ?Throwable
{
return $this->throwable;
}
public function message(string $message)
{
$this->message = $message;
return $this;
}
public function getMessage(): string
{
return $this->message;
}
public function stacktrace(Stacktrace $stacktrace)
{
$this->stacktrace = $stacktrace;
return $this;
}
public function getStacktrace(): Stacktrace
{
return $this->stacktrace;
}
public function notifierName(string $notifierName)
{
$this->notifierName = $notifierName;
return $this;
}
public function languageVersion(string $languageVersion)
{
$this->languageVersion = $languageVersion;
return $this;
}
public function frameworkVersion(string $frameworkVersion)
{
$this->frameworkVersion = $frameworkVersion;
return $this;
}
public function useContext(ContextInterface $request)
{
$this->context = $request;
return $this;
}
public function openFrameIndex(?int $index)
{
$this->openFrameIndex = $index;
return $this;
}
public function setApplicationPath(?string $applicationPath)
{
$this->applicationPath = $applicationPath;
return $this;
}
public function getApplicationPath(): ?string
{
return $this->applicationPath;
}
public function setApplicationVersion(?string $applicationVersion)
{
$this->applicationVersion = $applicationVersion;
return $this;
}
public function getApplicationVersion(): ?string
{
return $this->applicationVersion;
}
public function view(?View $view)
{
$this->view = $view;
return $this;
}
public function addGlow(Glow $glow)
{
$this->glows[] = $glow->toArray();
return $this;
}
public function addSolution(Solution $solution)
{
$this->solutions[] = ReportSolution::fromSolution($solution)->toArray();
return $this;
}
public function userProvidedContext(array $userProvidedContext)
{
$this->userProvidedContext = $userProvidedContext;
return $this;
}
/** @deprecated */
public function groupByTopFrame()
{
$this->groupBy = GroupingTypes::TOP_FRAME;
return $this;
}
/** @deprecated */
public function groupByException()
{
$this->groupBy = GroupingTypes::EXCEPTION;
return $this;
}
public function allContext(): array
{
$context = $this->context->toArray();
$context = array_merge_recursive_distinct($context, $this->exceptionContext);
return array_merge_recursive_distinct($context, $this->userProvidedContext);
}
private function exceptionContext(Throwable $throwable)
{
if ($throwable instanceof ProvidesFlareContext) {
$this->exceptionContext = $throwable->context();
}
return $this;
}
public function toArray()
{
return [
'notifier' => $this->notifierName ?? 'Flare Client',
'language' => 'PHP',
'framework_version' => $this->frameworkVersion,
'language_version' => $this->languageVersion ?? phpversion(),
'exception_class' => $this->exceptionClass,
'seen_at' => $this->getCurrentTime(),
'message' => $this->message,
'glows' => $this->glows,
'solutions' => $this->solutions,
'stacktrace' => $this->stacktrace->toArray(),
'context' => $this->allContext(),
'stage' => $this->stage,
'message_level' => $this->messageLevel,
'open_frame_index' => $this->openFrameIndex,
'application_path' => $this->applicationPath,
'application_version' => $this->applicationVersion,
'tracking_uuid' => $this->trackingUuid,
];
}
/*
* Found on https://stackoverflow.com/questions/2040240/php-function-to-generate-v4-uuid/15875555#15875555
*/
private function generateUuid(): string
{
// Generate 16 bytes (128 bits) of random data or use the data passed into the function.
$data = $data ?? random_bytes(16);
// Set version to 0100
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
// Set bits 6-7 to 10
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
// Output the 36 character UUID.
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
}