Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
[zendframework/zendframework#5383] Use error suppression *gulp*
Browse files Browse the repository at this point in the history
- The only viable solution that (a) prevents recursion during serialization, and
  (b) allows the Logger to work as an error handler after a message has been
  formatted, is to use the error suppression operator. See the comment in the
  normalize method for details.
  • Loading branch information
weierophinney committed Mar 3, 2014
1 parent 3fbea69 commit 2c365ec
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Formatter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,19 @@ protected function normalize($value)
$jsonFlags |= defined('JSON_UNESCAPED_UNICODE') ? JSON_UNESCAPED_UNICODE : 0;
}

// Error suppression is used in several of these cases as a fix for each of
// #5383 and #4616. Without it, #4616 fails whenever recursion occurs during
// json_encode() operations; usage of a dedicated error handler callback
// causes #5383 to fail when the Logger is being used as an error handler.
// The only viable solution here is error suppression, ugly as it may be.
if ($value instanceof DateTime) {
$value = $value->format($this->getDateTimeFormat());
} elseif ($value instanceof Traversable) {
$value = json_encode(iterator_to_array($value), $jsonFlags);
$value = @json_encode(iterator_to_array($value), $jsonFlags);
} elseif (is_array($value)) {
$value = json_encode($value, $jsonFlags);
$value = @json_encode($value, $jsonFlags);
} elseif (is_object($value) && !method_exists($value, '__toString')) {
$value = sprintf('object(%s) %s', get_class($value), json_encode($value));
$value = sprintf('object(%s) %s', get_class($value), @json_encode($value));
} elseif (is_resource($value)) {
$value = sprintf('resource(%s)', get_resource_type($value));
} elseif (!is_object($value)) {
Expand Down

0 comments on commit 2c365ec

Please # to comment.