|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WyriHaximus; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use ReflectionClass; |
|
7
|
|
|
use ReflectionProperty; |
|
8
|
|
|
use Throwable; |
|
9
|
|
|
|
|
10
|
|
|
function throwable_json_encode($throwable) |
|
11
|
|
|
{ |
|
12
|
1 |
|
return json_encode(throwable_encode($throwable)); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
function throwable_encode($throwable) |
|
16
|
|
|
{ |
|
17
|
2 |
|
if (!($throwable instanceof Exception) && !($throwable instanceof Throwable)) { |
|
|
|
|
|
|
18
|
|
|
throw new NotAThrowableException($throwable); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
2 |
|
$json = []; |
|
22
|
2 |
|
$json['class'] = get_class($throwable); |
|
23
|
2 |
|
$json['message'] = $throwable->getMessage(); |
|
24
|
2 |
|
$json['code'] = $throwable->getCode(); |
|
25
|
2 |
|
$json['file'] = $throwable->getFile(); |
|
26
|
2 |
|
$json['line'] = $throwable->getLine(); |
|
27
|
2 |
|
$json['trace'] = []; |
|
28
|
2 |
|
foreach ($throwable->getTrace() as $item) { |
|
29
|
2 |
|
$item['args'] = []; |
|
30
|
2 |
|
$json['trace'][] = $item; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
return $json; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
function throwable_json_decode($json) |
|
37
|
|
|
{ |
|
38
|
1 |
|
return throwable_decode(json_decode($json, true)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
function throwable_decode($json) |
|
42
|
|
|
{ |
|
43
|
|
|
$properties = [ |
|
44
|
6 |
|
'message', |
|
45
|
|
|
'code', |
|
46
|
|
|
'file', |
|
47
|
|
|
'line', |
|
48
|
|
|
'trace', |
|
49
|
|
|
'class', |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
6 |
|
foreach ($properties as $property) { |
|
53
|
6 |
|
if (!isset($json[$property])) { |
|
54
|
6 |
|
throw new NotAnEncodedThrowableException($json); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
6 |
|
array_pop($properties); |
|
59
|
|
|
|
|
60
|
6 |
|
if (PHP_MAJOR_VERSION === 7) { |
|
61
|
|
|
try { |
|
62
|
6 |
|
$throwable = (new ReflectionClass($json['class']))->newInstanceWithoutConstructor(); |
|
63
|
2 |
|
} catch (\Exception $e) { |
|
|
|
|
|
|
64
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
6 |
|
$class = new ReflectionClass($json['class']); |
|
68
|
6 |
|
if (!isset($throwable)) { |
|
69
|
|
|
try { |
|
70
|
2 |
|
$throwable = new $json['class'](); |
|
71
|
1 |
|
} catch (\Exception $e) { |
|
|
|
|
|
|
72
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
6 |
|
if (!isset($throwable)) { |
|
76
|
1 |
|
$throwable = unserialize('O:' . strlen($json['class']) . ':"' . $json['class'] . '":0:{}"', [$json['class']]); |
|
77
|
|
|
} |
|
78
|
6 |
|
foreach ($properties as $key) { |
|
79
|
6 |
|
if (!$class->hasProperty($key)) { |
|
80
|
4 |
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
6 |
|
$property = new ReflectionProperty($json['class'], $key); |
|
84
|
6 |
|
$property->setAccessible(true); |
|
85
|
6 |
|
$property->setValue($throwable, $json[$key]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
6 |
|
return $throwable; |
|
89
|
|
|
} |
|
90
|
|
|
|