Skip to content

Commit 9d240de

Browse files
Merge branch '2.7' into 2.8
* 2.7: [HttpFoundation] Fix logic when JsonSerializable is missing
2 parents c3f1470 + d1fe415 commit 9d240de

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/Symfony/Component/HttpFoundation/JsonResponse.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -127,30 +127,31 @@ public function setData($data = array())
127127
// but doesn't provide the JsonSerializable interface.
128128
set_error_handler(function () { return false; });
129129
$data = @json_encode($data, $this->encodingOptions);
130-
} else {
130+
restore_error_handler();
131+
} elseif (\PHP_VERSION_ID < 50500) {
131132
// PHP 5.4 and up wrap exceptions thrown by JsonSerializable
132133
// objects in a new exception that needs to be removed.
133134
// Fortunately, PHP 5.5 and up do not trigger any warning anymore.
134-
if (\PHP_VERSION_ID < 50500) {
135-
// Clear json_last_error()
136-
json_encode(null);
137-
$errorHandler = set_error_handler('var_dump');
138-
restore_error_handler();
139-
set_error_handler(function () use ($errorHandler) {
140-
if (JSON_ERROR_NONE === json_last_error()) {
141-
return $errorHandler && false !== call_user_func_array($errorHandler, func_get_args());
142-
}
143-
});
144-
}
145-
135+
// Clear json_last_error()
136+
json_encode(null);
137+
$errorHandler = set_error_handler('var_dump');
138+
restore_error_handler();
139+
set_error_handler(function () use ($errorHandler) {
140+
if (JSON_ERROR_NONE === json_last_error()) {
141+
return $errorHandler && false !== call_user_func_array($errorHandler, func_get_args());
142+
}
143+
});
144+
$data = json_encode($data, $this->encodingOptions);
145+
restore_error_handler();
146+
} else {
146147
$data = json_encode($data, $this->encodingOptions);
147148
}
148-
149-
if (\PHP_VERSION_ID < 50500) {
149+
} catch (\Error $e) {
150+
if (\PHP_VERSION_ID < 50500 || !interface_exists('JsonSerializable', false)) {
150151
restore_error_handler();
151152
}
152153
} catch (\Exception $e) {
153-
if (\PHP_VERSION_ID < 50500) {
154+
if (\PHP_VERSION_ID < 50500 || !interface_exists('JsonSerializable', false)) {
154155
restore_error_handler();
155156
}
156157
if (interface_exists('JsonSerializable', false) && 'Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {

0 commit comments

Comments
 (0)