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