1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FOS\RestBundle\Serializer\Normalizer; |
4
|
|
|
|
5
|
|
|
use FOS\RestBundle\Util\ExceptionValueMap; |
6
|
|
|
use JMS\Serializer\Context; |
7
|
|
|
use JMS\Serializer\GraphNavigatorInterface; |
8
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
9
|
|
|
use JMS\Serializer\JsonSerializationVisitor; |
10
|
|
|
use JMS\Serializer\XmlSerializationVisitor; |
11
|
|
|
use Symfony\Component\ErrorHandler\Exception\FlattenException; |
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Christian Flothmann <[email protected]> |
16
|
|
|
* |
17
|
|
|
* @internal |
18
|
|
|
*/ |
19
|
|
|
class FlattenExceptionHandler implements SubscribingHandlerInterface |
20
|
|
|
{ |
21
|
|
|
private $messagesMap; |
22
|
|
|
private $debug; |
23
|
|
|
private $rfc7807; |
24
|
|
|
|
25
|
4 |
|
public function __construct(ExceptionValueMap $messagesMap, bool $debug, bool $rfc7807) |
26
|
|
|
{ |
27
|
4 |
|
$this->messagesMap = $messagesMap; |
28
|
4 |
|
$this->debug = $debug; |
29
|
4 |
|
$this->rfc7807 = $rfc7807; |
30
|
4 |
|
} |
31
|
|
|
|
32
|
3 |
|
public static function getSubscribingMethods(): array |
33
|
|
|
{ |
34
|
|
|
return [ |
35
|
|
|
[ |
36
|
3 |
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
37
|
|
|
'format' => 'json', |
38
|
|
|
'type' => FlattenException::class, |
39
|
|
|
'method' => 'serializeToJson', |
40
|
|
|
], |
41
|
|
|
[ |
42
|
|
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
43
|
|
|
'format' => 'xml', |
44
|
|
|
'type' => FlattenException::class, |
45
|
|
|
'method' => 'serializeToXml', |
46
|
|
|
], |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
public function serializeToJson(JsonSerializationVisitor $visitor, FlattenException $exception, array $type, Context $context) |
51
|
|
|
{ |
52
|
2 |
|
if ($this->rfc7807) { |
53
|
1 |
|
$exception->setHeaders($exception->getHeaders() + ['Content-Type' => 'application/problem+json']); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
return $visitor->visitArray($this->convertToArray($exception, $context), $type, $context); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
public function serializeToXml(XmlSerializationVisitor $visitor, FlattenException $exception, array $type, Context $context) |
|
|
|
|
60
|
|
|
{ |
61
|
2 |
|
if ($this->rfc7807) { |
62
|
1 |
|
$exception->setHeaders($exception->getHeaders() + ['Content-Type' => 'application/problem+xml']); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
$rootName = $this->rfc7807 ? 'response' : 'result'; |
66
|
|
|
|
67
|
2 |
|
$data = $this->convertToArray($exception, $context); |
68
|
|
|
|
69
|
2 |
|
if (method_exists($visitor, 'setDefaultRootName')) { |
70
|
|
|
$visitor->setDefaultRootName($rootName); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
$document = $visitor->getDocument(true); |
|
|
|
|
74
|
|
|
|
75
|
2 |
|
if (!$visitor->getCurrentNode()) { |
76
|
2 |
|
$visitor->createRoot(null, $rootName); |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
foreach ($data as $key => $value) { |
80
|
2 |
|
$entryNode = $document->createElement($key); |
81
|
2 |
|
$visitor->getCurrentNode()->appendChild($entryNode); |
82
|
2 |
|
$visitor->setCurrentNode($entryNode); |
83
|
|
|
|
84
|
2 |
|
$node = $context->getNavigator()->accept($value, null, $context); |
|
|
|
|
85
|
2 |
|
if (null !== $node) { |
86
|
2 |
|
$visitor->getCurrentNode()->appendChild($node); |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
$visitor->revertCurrentNode(); |
90
|
|
|
} |
91
|
2 |
|
} |
92
|
|
|
|
93
|
4 |
|
private function convertToArray(FlattenException $exception, Context $context): array |
94
|
|
|
{ |
95
|
4 |
|
if ($context->hasAttribute('status_code')) { |
96
|
|
|
$statusCode = $context->getAttribute('status_code'); |
97
|
|
|
} else { |
98
|
4 |
|
$statusCode = $exception->getStatusCode(); |
99
|
|
|
} |
100
|
|
|
|
101
|
4 |
|
$showMessage = $this->messagesMap->resolveFromClassName($exception->getClass()); |
102
|
|
|
|
103
|
4 |
View Code Duplication |
if ($showMessage || $this->debug) { |
104
|
4 |
|
$message = $exception->getMessage(); |
105
|
|
|
} else { |
106
|
|
|
$message = Response::$statusTexts[$statusCode] ?? 'error'; |
107
|
|
|
} |
108
|
|
|
|
109
|
4 |
|
if ($this->rfc7807) { |
110
|
|
|
return [ |
111
|
2 |
|
'type' => $context->hasAttribute('type') ? $context->getAttribute('type') : 'https://tools.ietf.org/html/rfc2616#section-10', |
112
|
2 |
|
'title' => $context->hasAttribute('title') ? $context->getAttribute('title') : 'An error occurred', |
113
|
2 |
|
'status' => $statusCode, |
114
|
2 |
|
'detail' => $message, |
115
|
|
|
]; |
116
|
|
|
} else { |
117
|
|
|
return [ |
118
|
2 |
|
'code' => $statusCode, |
119
|
2 |
|
'message' => $message, |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
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
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.