Permalink
Browse files

Merge branch 'release'

  • Loading branch information...
2 parents 607281b + d1146c8 commit c5fbf8c220efdf1b6c31b5f311b0d1e2f6dccab9 @weierophinney weierophinney committed Sep 13, 2012
@@ -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;
}
}
@@ -12,6 +12,8 @@
use DateTime;
use Zend\Log\Formatter\ErrorHandler;
+use ZendTest\Log\TestAsset\StringObject;
+use ZendTest\Log\TestAsset\NotStringObject;
/**
* @category Zend
@@ -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();
@@ -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);
+ }
+
+
}
@@ -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 c5fbf8c

Please sign in to comment.