8000 minor #24527 [Console] Sync ConsoleLogger::interpolate with the one i… · symfony/symfony@4a68775 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4a68775

Browse files
author
Robin Chalas
committed
minor #24527 [Console] Sync ConsoleLogger::interpolate with the one in HttpKernel (dunglas)
This PR was merged into the 3.4 branch. Discussion ---------- [Console] Sync ConsoleLogger::interpolate with the one in HttpKernel | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget to update UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Adapted from: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/Log/Logger.php#L92 Better performance and datetime support (maybe should it be merged in a newer version, but I've targeted 2.7 to prevent future merge conflicts). Commits ------- 8fcbc55 [Console] Sync ConsoleLogger::interpolate with the one in HttpKernel
2 parents 42390a2 + 8fcbc55 commit 4a68775

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
@@ -121,15 +121,23 @@ public function hasErrored()
121121
*/
122122
private function interpolate($message, array $context)
123123
{
124-
// build a replacement array with braces around the context keys
125-
$replace = array();
124+
if (false === strpos($message, '{')) {
125+
return $message;
126+
}
127+
128+
$replacements = array();
126129
foreach ($context as $key => $val) {
127-
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
128-
$replace[sprintf('{%s}', $key)] = $val;
130+
if (null === $val || is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) {
131+
$replacements["{{$key}}"] = $val;
132+
} elseif ($val instanceof \DateTimeInterface) {
133+
$replacements["{{$key}}"] = $val->format(\DateTime::RFC3339);
134+
} elseif (\is_object($val)) {
135+
$replacements["{{$key}}"] = '[object '.\get_class($val).']';
136+
} else {
137+
$replacements["{{$key}}"] = '['.\gettype($val).']';
129138
}
130139
}
131140

132-
// interpolate replacement values into the message and return
133-
return strtr($message, $replace);
141+
return strtr($message, $replacements);
134142
}
135143
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,7 @@ public function testObjectCastToString()
166166
} else {
167167
$dummy = $this->getMock('Symfony\Component\Console\Tests\Logger\DummyTest', array('__toString'));
168168
}
169-
$dummy->expects($this->once())
170-
->method('__toString')
171-
->will($this->returnValue('DUMMY'));
169+
$dummy->method('__toString')->will($this->returnValue('DUMMY'));
172170

173171
$this->getLogger()->warning($dummy);
174172

0 commit comments

Comments
 (0)
0