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

Commit

Permalink
Merge branch 'hotfix/zf2-549' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
39 changes: 32 additions & 7 deletions src/Formatter/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,41 @@ public function format($event)
$event['timestamp'] = $event['timestamp']->format($this->getDateTimeFormat());
}

foreach ($event as $name => $value) {
if (is_array($value)) {
foreach ($value as $sname => $svalue) {
$output = str_replace("%{$name}[{$sname}]%", $svalue, $output);
foreach ($this->buildReplacementsFromArray($event) as $name => $value) {
$output = str_replace("%$name%", $value, $output);
}

return $output;
}

/**
* Flatten the multi-dimensional $event array into a single dimensional
* array
*
* @param array $event
* @param string $key
* @return array
*/
protected function buildReplacementsFromArray ($event, $key = null)
{
$result = array();
foreach ($event as $index => $value) {
$nextIndex = $key === null ? $index : $key . '[' . $index . ']';
if ($value === null) {
continue;
}
if (! is_array($value)) {
if ($key === null) {
$result[$nextIndex] = $value;
} else {
if (! is_object($value) || method_exists($value, "__toString")) {
$result[$nextIndex] = $value;
}
}
} else {
$output = str_replace("%$name%", $value, $output);
$result = array_merge($result, $this->buildReplacementsFromArray($value, $nextIndex));
}
}

return $output;
return $result;
}
}
36 changes: 35 additions & 1 deletion test/Formatter/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

use DateTime;
use Zend\Log\Formatter\ErrorHandler;
use ZendTest\Log\TestAsset\StringObject;
use ZendTest\Log\TestAsset\NotStringObject;

/**
* @category Zend
Expand All @@ -33,7 +35,8 @@ public function testFormat()
'extra' => array (
'errno' => 1,
'file' => 'test.php',
'line' => 1
'line' => 1,
'context' => array('object' => new DateTime(), 'string' => 'test')
)
);
$formatter = new ErrorHandler();
Expand All @@ -50,4 +53,35 @@ public function testSetDateTimeFormat()
$this->assertSame($formatter, $formatter->setDateTimeFormat('r'));
$this->assertEquals('r', $formatter->getDateTimeFormat());
}

public function testComplexEvent ()
{
$date = new DateTime();
$stringObject = new StringObject();
$event = array(
'timestamp' => $date,
'message' => 'test',
'priority' => 1,
'priorityName' => 'CRIT',
'extra' => array(
'errno' => 1,
'file' => 'test.php',
'line' => 1,
'context' => array(
'object1' => new StringObject(),
'object2' => new NotStringObject(),
'string' => 'test1',
'array' => array(
'key' => 'test2'
)
)
)
);
$formatString = '%extra[context][object1]% %extra[context][object2]% %extra[context][string]% %extra[context][array]% %extra[context][array][key]%';
$formatter = new ErrorHandler($formatString);
$output = $formatter->format($event);
$this->assertEquals($stringObject->__toString() .' %extra[context][object2]% test1 %extra[context][array]% test2', $output);
}


}
16 changes: 16 additions & 0 deletions test/TestAsset/NotStringObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Log
*/

namespace ZendTest\Log\TestAsset;

class NotStringObject
{
// This object has no __toString method
}

0 comments on commit 1a9df55

Please # to comment.