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['trace'] = []; |
29
|
2 |
|
foreach ($throwable->getTrace() as $item) { |
30
|
2 |
|
$item['args'] = []; |
31
|
2 |
|
$json['trace'][] = $item; |
32
|
|
|
} |
33
|
|
|
|
34
|
2 |
|
return $json; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
function throwable_json_decode($json) |
38
|
|
|
{ |
39
|
1 |
|
return throwable_decode(json_decode($json, true)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
function throwable_decode($json) |
43
|
|
|
{ |
44
|
|
|
$properties = [ |
45
|
6 |
|
'message', |
46
|
|
|
'code', |
47
|
|
|
'file', |
48
|
|
|
'line', |
49
|
|
|
'trace', |
50
|
|
|
'class', |
51
|
|
|
]; |
52
|
|
|
|
53
|
6 |
|
foreach ($properties as $property) { |
54
|
6 |
|
if (!isset($json[$property])) { |
55
|
6 |
|
throw new NotAnEncodedThrowableException($json); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
6 |
|
array_pop($properties); |
60
|
|
|
|
61
|
6 |
|
$throwable = (new Instantiator())->instantiate($json['class']); |
62
|
6 |
|
$class = new ReflectionClass($json['class']); |
63
|
6 |
|
foreach ($properties as $key) { |
64
|
6 |
|
if (!$class->hasProperty($key)) { |
65
|
4 |
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
6 |
|
$property = new ReflectionProperty($json['class'], $key); |
69
|
6 |
|
$property->setAccessible(true); |
70
|
6 |
|
$property->setValue($throwable, $json[$key]); |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
return $throwable; |
74
|
|
|
} |
75
|
|
|
|