mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-06 05:07:36 +02:00
Merge pull request #808 from naderman/fix/normalizer-recusion
Normalization of arrays containing self references
This commit is contained in:
@@ -135,8 +135,12 @@ class JsonFormatter extends NormalizerFormatter
|
||||
*
|
||||
* @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) {
|
||||
$normalized = [];
|
||||
|
||||
@@ -146,7 +150,7 @@ class JsonFormatter extends NormalizerFormatter
|
||||
$normalized['...'] = 'Over 1000 items, aborting normalization';
|
||||
break;
|
||||
}
|
||||
$normalized[$key] = $this->normalize($value);
|
||||
$normalized[$key] = $this->normalize($value, $depth+1);
|
||||
}
|
||||
|
||||
return $normalized;
|
||||
|
@@ -56,8 +56,12 @@ class NormalizerFormatter implements FormatterInterface
|
||||
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 (is_float($data)) {
|
||||
if (is_infinite($data)) {
|
||||
@@ -80,7 +84,7 @@ class NormalizerFormatter implements FormatterInterface
|
||||
$normalized['...'] = 'Over 1000 items, aborting normalization';
|
||||
break;
|
||||
}
|
||||
$normalized[$key] = $this->normalize($value);
|
||||
$normalized[$key] = $this->normalize($value, $depth+1);
|
||||
}
|
||||
|
||||
return $normalized;
|
||||
|
@@ -102,12 +102,12 @@ class WildfireFormatter extends NormalizerFormatter
|
||||
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 \DateTimeInterface) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
return parent::normalize($data);
|
||||
return parent::normalize($data, $depth);
|
||||
}
|
||||
}
|
||||
|
@@ -166,6 +166,15 @@ class NormalizerFormatterTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(@json_encode([$foo, $bar]), $res);
|
||||
}
|
||||
|
||||
public function testCanNormalizeReferences()
|
||||
{
|
||||
$formatter = new NormalizerFormatter();
|
||||
$x = ['foo' => 'bar'];
|
||||
$y = ['x' => &$x];
|
||||
$x['y'] = &$y;
|
||||
$formatter->format($y);
|
||||
}
|
||||
|
||||
public function testIgnoresInvalidTypes()
|
||||
{
|
||||
// set up the recursion
|
||||
|
Reference in New Issue
Block a user