1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WyriHaximus; |
6
|
|
|
|
7
|
|
|
use Doctrine\Instantiator\Exception\ExceptionInterface; |
8
|
|
|
use Doctrine\Instantiator\Instantiator; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
use ReflectionException; |
11
|
|
|
use ReflectionProperty; |
12
|
|
|
use Throwable; |
13
|
|
|
|
14
|
|
|
use function assert; |
15
|
|
|
use function get_class; |
16
|
1 |
|
use function Safe\json_decode; |
17
|
|
|
use function Safe\json_encode; |
18
|
|
|
use function serialize; |
19
|
|
|
use function unserialize; |
20
|
|
|
|
21
|
|
|
function throwable_json_encode(Throwable $throwable): string |
22
|
|
|
{ |
23
|
|
|
return json_encode(throwable_encode($throwable)); |
24
|
2 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return array{class: class-string<Throwable>, message: string, code: mixed, file: string, line: int, previous: string|null, originalTrace: array<int, mixed>, additionalProperties: array<string, string>} |
|
|
|
|
28
|
2 |
|
*/ |
29
|
2 |
|
function throwable_encode(Throwable $throwable): array |
30
|
2 |
|
{ |
31
|
2 |
|
$json = []; |
32
|
2 |
|
$json['class'] = get_class($throwable); |
33
|
2 |
|
$json['message'] = $throwable->getMessage(); |
34
|
2 |
|
$json['code'] = $throwable->getCode(); |
35
|
2 |
|
$json['file'] = $throwable->getFile(); |
36
|
2 |
|
$json['line'] = $throwable->getLine(); |
37
|
2 |
|
/** @psalm-suppress PossiblyNullArgument */ |
38
|
2 |
|
$json['previous'] = $throwable->getPrevious() instanceof Throwable ? throwable_json_encode($throwable->getPrevious()) : null; |
39
|
|
|
|
40
|
|
|
$json['originalTrace'] = []; |
41
|
2 |
|
foreach ($throwable->getTrace() as $item) { |
42
|
|
|
$item['args'] = []; |
43
|
|
|
$json['originalTrace'][] = $item; |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
$json['additionalProperties'] = []; |
47
|
|
|
if ($throwable instanceof AdditionalPropertiesInterface) { |
48
|
|
|
$class = new ReflectionClass($json['class']); |
|
|
|
|
49
|
|
|
foreach ($throwable->additionalProperties() as $key) { |
50
|
|
|
$property = new ReflectionProperty($json['class'], $key); |
51
|
|
|
$property->setAccessible(true); |
52
|
6 |
|
$json['additionalProperties'][$key] = serialize($property->getValue($throwable)); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $json; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
function throwable_json_decode(string $json): Throwable |
60
|
|
|
{ |
61
|
6 |
|
return throwable_decode(json_decode($json, true)); |
62
|
|
|
} |
63
|
6 |
|
|
64
|
|
|
/** |
65
|
6 |
|
* @param array{class: class-string<Throwable>, message: string, code: mixed, file: string, line: int, previous: string|null, originalTrace: array<int, mixed>, additionalProperties: array<string, string>} $json |
|
|
|
|
66
|
6 |
|
* |
67
|
6 |
|
* @throws ExceptionInterface |
68
|
6 |
|
* @throws ReflectionException |
69
|
4 |
|
*/ |
70
|
|
|
function throwable_decode(array $json): Throwable |
71
|
|
|
{ |
72
|
6 |
|
$properties = [ |
73
|
6 |
|
'class' => 'string', |
74
|
6 |
|
'message' => 'string', |
75
|
|
|
'code' => 'integer', |
76
|
|
|
'file' => 'string', |
77
|
6 |
|
'line' => 'integer', |
78
|
|
|
'previous' => ['string', 'NULL'], |
79
|
|
|
'originalTrace' => 'array', |
80
|
|
|
'additionalProperties' => 'array', |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
validate_array($json, $properties, NotAnEncodedThrowableException::class); |
84
|
|
|
|
85
|
|
|
$additionalProperties = $json['additionalProperties']; |
86
|
|
|
unset($json['additionalProperties']); |
87
|
|
|
|
88
|
|
|
if ($json['previous'] !== null) { |
89
|
|
|
$json['previous'] = throwable_json_decode($json['previous']); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$throwable = (new Instantiator())->instantiate($json['class']); |
93
|
|
|
assert($throwable instanceof Throwable); |
94
|
|
|
$class = new ReflectionClass($json['class']); |
95
|
|
|
foreach ($properties as $key => $type) { |
96
|
|
|
if (! $class->hasProperty($key)) { |
97
|
|
|
continue; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$property = new ReflectionProperty($json['class'], $key); |
101
|
|
|
$property->setAccessible(true); |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @psalm-suppress PossiblyInvalidArrayOffset |
105
|
|
|
* @psalm-suppress InvalidArrayOffset |
106
|
|
|
*/ |
107
|
|
|
$property->setValue($throwable, $json[$key]); |
108
|
|
|
$property->setAccessible(false); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
foreach ($additionalProperties as $key => $contents) { |
112
|
|
|
if (! $class->hasProperty($key)) { |
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$property = new ReflectionProperty($json['class'], $key); |
117
|
|
|
$property->setAccessible(true); |
118
|
|
|
$property->setValue($throwable, unserialize($additionalProperties[$key])); |
119
|
|
|
$property->setAccessible(false); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $throwable; |
123
|
|
|
} |
124
|
|
|
|