|
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 since 2.8 |
|
22
|
|
|
*/ |
|
23
|
|
|
class ExceptionHandler extends AbstractExceptionNormalizer implements SubscribingHandlerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @return array |
|
27
|
|
|
*/ |
|
28
|
3 |
|
public static function getSubscribingMethods() |
|
29
|
|
|
{ |
|
30
|
|
|
return [ |
|
31
|
|
|
[ |
|
32
|
3 |
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
|
33
|
3 |
|
'format' => 'json', |
|
34
|
|
|
'type' => \Error::class, |
|
35
|
3 |
|
'method' => 'serializeErrorToJson', |
|
36
|
|
|
], |
|
37
|
|
|
[ |
|
38
|
3 |
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
|
39
|
3 |
|
'format' => 'json', |
|
40
|
|
|
'type' => \Exception::class, |
|
41
|
3 |
|
'method' => 'serializeToJson', |
|
42
|
|
|
], |
|
43
|
|
|
[ |
|
44
|
3 |
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
|
45
|
3 |
|
'format' => 'xml', |
|
46
|
|
|
'type' => \Error::class, |
|
47
|
3 |
|
'method' => 'serializeErrorToXml', |
|
48
|
|
|
], |
|
49
|
|
|
[ |
|
50
|
3 |
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
|
51
|
3 |
|
'format' => 'xml', |
|
52
|
|
|
'type' => \Exception::class, |
|
53
|
3 |
|
'method' => 'serializeToXml', |
|
54
|
|
|
], |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param JsonSerializationVisitor $visitor |
|
60
|
|
|
* @param \Exception $exception |
|
61
|
|
|
* @param array $type |
|
62
|
|
|
* @param Context $context |
|
63
|
|
|
* |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
2 |
|
public function serializeToJson( |
|
67
|
|
|
JsonSerializationVisitor $visitor, |
|
68
|
|
|
\Exception $exception, |
|
69
|
|
|
array $type, |
|
70
|
|
|
Context $context |
|
71
|
|
|
) { |
|
72
|
2 |
|
$data = $this->convertToArray($exception, $context); |
|
73
|
|
|
|
|
74
|
2 |
|
return $visitor->visitArray($data, $type, $context); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function serializeErrorToJson( |
|
78
|
|
|
JsonSerializationVisitor $visitor, |
|
79
|
|
|
\Throwable $exception, |
|
80
|
|
|
array $type, |
|
81
|
|
|
Context $context |
|
82
|
|
|
) { |
|
83
|
|
|
$data = $this->convertThrowableToArray($exception, $context); |
|
84
|
|
|
|
|
85
|
|
|
return $visitor->visitArray($data, $type, $context); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param XmlSerializationVisitor $visitor |
|
90
|
|
|
* @param \Exception $exception |
|
91
|
|
|
* @param array $type |
|
92
|
|
|
* @param Context $context |
|
93
|
|
|
*/ |
|
94
|
1 |
View Code Duplication |
public function serializeToXml( |
|
|
|
|
|
|
95
|
|
|
XmlSerializationVisitor $visitor, |
|
96
|
|
|
\Exception $exception, |
|
97
|
|
|
array $type, |
|
|
|
|
|
|
98
|
|
|
Context $context |
|
99
|
|
|
) { |
|
100
|
1 |
|
$data = $this->convertToArray($exception, $context); |
|
101
|
|
|
|
|
102
|
1 |
|
$document = $visitor->getDocument(true); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
1 |
|
if (!$visitor->getCurrentNode()) { |
|
105
|
1 |
|
$visitor->createRoot(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
foreach ($data as $key => $value) { |
|
109
|
1 |
|
$entryNode = $document->createElement($key); |
|
110
|
1 |
|
$visitor->getCurrentNode()->appendChild($entryNode); |
|
111
|
1 |
|
$visitor->setCurrentNode($entryNode); |
|
112
|
|
|
|
|
113
|
1 |
|
$node = $context->getNavigator()->accept($value, null, $context); |
|
|
|
|
|
|
114
|
1 |
|
if (null !== $node) { |
|
115
|
1 |
|
$visitor->getCurrentNode()->appendChild($node); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
$visitor->revertCurrentNode(); |
|
119
|
|
|
} |
|
120
|
1 |
|
} |
|
121
|
|
|
|
|
122
|
|
View Code Duplication |
public function serializeErrorToXml( |
|
|
|
|
|
|
123
|
|
|
XmlSerializationVisitor $visitor, |
|
124
|
|
|
\Throwable $exception, |
|
125
|
|
|
array $type, |
|
|
|
|
|
|
126
|
|
|
Context $context |
|
127
|
|
|
) { |
|
128
|
|
|
$data = $this->convertThrowableToArray($exception, $context); |
|
129
|
|
|
|
|
130
|
|
|
$document = $visitor->getDocument(true); |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
if (!$visitor->getCurrentNode()) { |
|
133
|
|
|
$visitor->createRoot(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
foreach ($data as $key => $value) { |
|
137
|
|
|
$entryNode = $document->createElement($key); |
|
138
|
|
|
$visitor->getCurrentNode()->appendChild($entryNode); |
|
139
|
|
|
$visitor->setCurrentNode($entryNode); |
|
140
|
|
|
|
|
141
|
|
|
$node = $context->getNavigator()->accept($value, null, $context); |
|
|
|
|
|
|
142
|
|
|
if (null !== $node) { |
|
143
|
|
|
$visitor->getCurrentNode()->appendChild($node); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$visitor->revertCurrentNode(); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param \Exception $exception |
|
152
|
|
|
* @param Context $context |
|
153
|
|
|
* |
|
154
|
|
|
* @return array |
|
155
|
|
|
*/ |
|
156
|
3 |
|
protected function convertToArray(\Exception $exception, Context $context) |
|
157
|
|
|
{ |
|
158
|
3 |
|
return $this->convertThrowableToArray($exception, $context); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
3 |
|
private function convertThrowableToArray(\Throwable $throwable, Context $context): array |
|
162
|
|
|
{ |
|
163
|
3 |
|
$data = []; |
|
164
|
|
|
|
|
165
|
3 |
|
if ($context->hasAttribute('status_code')) { |
|
166
|
3 |
|
$data['code'] = $context->getAttribute('status_code'); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
3 |
|
$data['message'] = $this->getMessageFromThrowable($throwable, isset($statusCode) ? $statusCode : null); |
|
|
|
|
|
|
170
|
|
|
|
|
171
|
3 |
|
return $data; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
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.