|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSRestBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace FOS\RestBundle\Serializer\Normalizer; |
|
13
|
|
|
|
|
14
|
|
|
use JMS\Serializer\Context; |
|
15
|
|
|
use JMS\Serializer\GraphNavigatorInterface; |
|
16
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
|
17
|
|
|
use JMS\Serializer\JsonSerializationVisitor; |
|
18
|
|
|
use JMS\Serializer\XmlSerializationVisitor; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @internal |
|
22
|
|
|
*/ |
|
23
|
|
|
class ExceptionHandler extends AbstractExceptionNormalizer implements SubscribingHandlerInterface |
|
24
|
|
|
{ |
|
25
|
3 |
|
public static function getSubscribingMethods() |
|
26
|
|
|
{ |
|
27
|
|
|
return [ |
|
28
|
|
|
[ |
|
29
|
3 |
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
|
30
|
3 |
|
'format' => 'json', |
|
31
|
|
|
'type' => \Error::class, |
|
32
|
3 |
|
'method' => 'serializeErrorToJson', |
|
33
|
|
|
], |
|
34
|
|
|
[ |
|
35
|
3 |
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
|
36
|
3 |
|
'format' => 'json', |
|
37
|
|
|
'type' => \Exception::class, |
|
38
|
3 |
|
'method' => 'serializeToJson', |
|
39
|
|
|
], |
|
40
|
|
|
[ |
|
41
|
3 |
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
|
42
|
3 |
|
'format' => 'xml', |
|
43
|
|
|
'type' => \Error::class, |
|
44
|
3 |
|
'method' => 'serializeErrorToXml', |
|
45
|
|
|
], |
|
46
|
|
|
[ |
|
47
|
3 |
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
|
48
|
3 |
|
'format' => 'xml', |
|
49
|
|
|
'type' => \Exception::class, |
|
50
|
3 |
|
'method' => 'serializeToXml', |
|
51
|
|
|
], |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
public function serializeToJson( |
|
56
|
|
|
JsonSerializationVisitor $visitor, |
|
57
|
|
|
\Exception $exception, |
|
58
|
|
|
array $type, |
|
59
|
|
|
Context $context |
|
60
|
|
|
) { |
|
61
|
2 |
|
$data = $this->convertToArray($exception, $context); |
|
62
|
|
|
|
|
63
|
2 |
|
return $visitor->visitArray($data, $type, $context); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function serializeErrorToJson( |
|
67
|
|
|
JsonSerializationVisitor $visitor, |
|
68
|
|
|
\Throwable $exception, |
|
69
|
|
|
array $type, |
|
70
|
|
|
Context $context |
|
71
|
|
|
) { |
|
72
|
|
|
$data = $this->convertToArray($exception, $context); |
|
73
|
|
|
|
|
74
|
|
|
return $visitor->visitArray($data, $type, $context); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
View Code Duplication |
public function serializeToXml( |
|
|
|
|
|
|
78
|
|
|
XmlSerializationVisitor $visitor, |
|
79
|
|
|
\Exception $exception, |
|
80
|
|
|
array $type, |
|
|
|
|
|
|
81
|
|
|
Context $context |
|
82
|
|
|
) { |
|
83
|
1 |
|
$data = $this->convertToArray($exception, $context); |
|
84
|
|
|
|
|
85
|
1 |
|
$document = $visitor->getDocument(true); |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
1 |
|
if (!$visitor->getCurrentNode()) { |
|
88
|
1 |
|
$visitor->createRoot(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
foreach ($data as $key => $value) { |
|
92
|
1 |
|
$entryNode = $document->createElement($key); |
|
93
|
1 |
|
$visitor->getCurrentNode()->appendChild($entryNode); |
|
94
|
1 |
|
$visitor->setCurrentNode($entryNode); |
|
95
|
|
|
|
|
96
|
1 |
|
$node = $context->getNavigator()->accept($value, null, $context); |
|
|
|
|
|
|
97
|
1 |
|
if (null !== $node) { |
|
98
|
1 |
|
$visitor->getCurrentNode()->appendChild($node); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
$visitor->revertCurrentNode(); |
|
102
|
|
|
} |
|
103
|
1 |
|
} |
|
104
|
|
|
|
|
105
|
|
View Code Duplication |
public function serializeErrorToXml( |
|
|
|
|
|
|
106
|
|
|
XmlSerializationVisitor $visitor, |
|
107
|
|
|
\Throwable $exception, |
|
108
|
|
|
array $type, |
|
|
|
|
|
|
109
|
|
|
Context $context |
|
110
|
|
|
) { |
|
111
|
|
|
$data = $this->convertToArray($exception, $context); |
|
112
|
|
|
|
|
113
|
|
|
$document = $visitor->getDocument(true); |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
if (!$visitor->getCurrentNode()) { |
|
116
|
|
|
$visitor->createRoot(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
foreach ($data as $key => $value) { |
|
120
|
|
|
$entryNode = $document->createElement($key); |
|
121
|
|
|
$visitor->getCurrentNode()->appendChild($entryNode); |
|
122
|
|
|
$visitor->setCurrentNode($entryNode); |
|
123
|
|
|
|
|
124
|
|
|
$node = $context->getNavigator()->accept($value, null, $context); |
|
|
|
|
|
|
125
|
|
|
if (null !== $node) { |
|
126
|
|
|
$visitor->getCurrentNode()->appendChild($node); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$visitor->revertCurrentNode(); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
3 |
|
private function convertToArray(\Throwable $throwable, Context $context): array |
|
134
|
|
|
{ |
|
135
|
3 |
|
$data = []; |
|
136
|
|
|
|
|
137
|
3 |
|
if ($context->hasAttribute('status_code')) { |
|
138
|
3 |
|
$data['code'] = $context->getAttribute('status_code'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
3 |
|
$data['message'] = $this->getMessageFromThrowable($throwable, isset($statusCode) ? $statusCode : null); |
|
|
|
|
|
|
142
|
|
|
|
|
143
|
3 |
|
return $data; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.