8000 [Console] Sync ConsoleLogger::interpolate with the one in HttpKernel · symfony/symfony@9e6ace0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e6ace0

Browse files
committed
[Console] Sync ConsoleLogger::interpolate with the one in HttpKernel
1 parent da25d44 commit 9e6ace0

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/Symfony/Component/Console/Logger/ConsoleLogger.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,23 @@ public function log($level, $message, array $context = array())
105105
*/
106106
private function interpolate($message, array $context)
107107
{
108-
// build a replacement array with braces around the context keys
109-
$replace = array();
108+
if (false === strpos($message, '{')) {
109+
return $message;
110+
}
111+
112+
$replacements = array();
110113
foreach ($context as $key => $val) {
111-
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
112-
$replace[sprintf('{%s}', $key)] = $val;
114+
if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
115+
$replacements["{{$key}}"] = $val;
116+
} elseif ($val instanceof \DateTime || $val instanceof \DateTimeInterface) {
117+
$replacements["{{$key}}"] = $val->format(\DateTime::RFC3339);
118+
} elseif (\is_object($val)) {
119+
$replacements["{{$key}}"] = '[object '.\get_class($val).']';
120+
} else {
121+
$replacements["{{$key}}"] = '['.\gettype($val).']';
113122
}
114123
}
115124

116-
// interpolate replacement values into the message and return
117-
return strtr($message, $replace);
125+
return strtr($message, $replacements);
118126
}
119127
}

src/Symfony/Component/Console/Tests/Logger/ConsoleLoggerTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ public function testObjectCastToString()
120120
} else {
121121
$dummy = $this->getMock('Symfony\Component\Console\Tests\Logger\DummyTest', array('__toString'));
122122
}
123-
$dummy->expects($this->once())
124-
->method('__toString')
125-
->will($this->returnValue('DUMMY'));
123+
$dummy->method('__toString')->will($this->returnValue('DUMMY'));
126124

127125
$this->getLogger()->warning($dummy);
128126

0 commit comments

Comments
 (0)
0