1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-06 21:26:43 +02:00

Normalization of arrays containing self references (#1050)

Backport normalization fix from master to 1.x
This commit is contained in:
Andrew Berry
2018-06-17 11:27:33 -04:00
committed by Jordi Boggiano
parent 6e1793e966
commit 0d993d84d1
4 changed files with 23 additions and 6 deletions

View File

@@ -138,8 +138,12 @@ class JsonFormatter extends NormalizerFormatter
* *
* @return mixed * @return mixed
*/ */
protected function normalize($data) protected function normalize($data, $depth = 0)
{ {
if ($depth > 9) {
return 'Over 9 levels deep, aborting normalization';
}
if (is_array($data) || $data instanceof \Traversable) { if (is_array($data) || $data instanceof \Traversable) {
$normalized = array(); $normalized = array();
@@ -150,7 +154,7 @@ class JsonFormatter extends NormalizerFormatter
break; break;
} }
$normalized[$key] = $this->normalize($value); $normalized[$key] = $this->normalize($value, $depth+1);
} }
return $normalized; return $normalized;

View File

@@ -55,8 +55,12 @@ class NormalizerFormatter implements FormatterInterface
return $records; return $records;
} }
protected function normalize($data) protected function normalize($data, $depth = 0)
{ {
if ($depth > 9) {
return 'Over 9 levels deep, aborting normalization';
}
if (null === $data || is_scalar($data)) { if (null === $data || is_scalar($data)) {
if (is_float($data)) { if (is_float($data)) {
if (is_infinite($data)) { if (is_infinite($data)) {
@@ -80,7 +84,7 @@ class NormalizerFormatter implements FormatterInterface
break; break;
} }
$normalized[$key] = $this->normalize($value); $normalized[$key] = $this->normalize($value, $depth+1);
} }
return $normalized; return $normalized;

View File

@@ -102,12 +102,12 @@ class WildfireFormatter extends NormalizerFormatter
throw new \BadMethodCallException('Batch formatting does not make sense for the WildfireFormatter'); throw new \BadMethodCallException('Batch formatting does not make sense for the WildfireFormatter');
} }
protected function normalize($data) protected function normalize($data, $depth = 0)
{ {
if (is_object($data) && !$data instanceof \DateTime) { if (is_object($data) && !$data instanceof \DateTime) {
return $data; return $data;
} }
return parent::normalize($data); return parent::normalize($data, $depth);
} }
} }

View File

@@ -193,6 +193,15 @@ class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(@json_encode(array($foo, $bar)), $res); $this->assertEquals(@json_encode(array($foo, $bar)), $res);
} }
public function testCanNormalizeReferences()
{
$formatter = new NormalizerFormatter();
$x = array('foo' => 'bar');
$y = array('x' => &$x);
$x['y'] = &$y;
$formatter->format($y);
}
public function testIgnoresInvalidTypes() public function testIgnoresInvalidTypes()
{ {
// set up the recursion // set up the recursion