[Console] Sync ConsoleLogger::interpolate with the one in HttpKernel

This commit is contained in:
Kévin Dunglas 2017-10-12 09:04:00 +02:00
parent 42390a2b00
commit 8fcbc5572b
No known key found for this signature in database
GPG Key ID: 4D04EBEF06AAF3A6
2 changed files with 15 additions and 9 deletions

View File

@ -121,15 +121,23 @@ class ConsoleLogger extends AbstractLogger
*/
private function interpolate($message, array $context)
{
// build a replacement array with braces around the context keys
$replace = array();
if (false === strpos($message, '{')) {
return $message;
}
$replacements = array();
foreach ($context as $key => $val) {
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
$replace[sprintf('{%s}', $key)] = $val;
if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
$replacements["{{$key}}"] = $val;
} elseif ($val instanceof \DateTimeInterface) {
$replacements["{{$key}}"] = $val->format(\DateTime::RFC3339);
} elseif (\is_object($val)) {
$replacements["{{$key}}"] = '[object '.\get_class($val).']';
} else {
$replacements["{{$key}}"] = '['.\gettype($val).']';
}
}
// interpolate replacement values into the message and return
return strtr($message, $replace);
return strtr($message, $replacements);
}
}

View File

@ -166,9 +166,7 @@ class ConsoleLoggerTest extends TestCase
} else {
$dummy = $this->getMock('Symfony\Component\Console\Tests\Logger\DummyTest', array('__toString'));
}
$dummy->expects($this->once())
->method('__toString')
->will($this->returnValue('DUMMY'));
$dummy->method('__toString')->will($this->returnValue('DUMMY'));
$this->getLogger()->warning($dummy);