Skip to content

Commit

Permalink
minor #24527 [Console] Sync ConsoleLogger::interpolate with the one i…
Browse files Browse the repository at this point in the history
…n 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
  • Loading branch information
Robin Chalas committed Oct 15, 2017
2 parents 42390a2 + 8fcbc55 commit 4a68775
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
20 changes: 14 additions & 6 deletions src/Symfony/Component/Console/Logger/ConsoleLogger.php
Expand Up @@ -121,15 +121,23 @@ public function hasErrored()
*/
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);
}
}
Expand Up @@ -166,9 +166,7 @@ public function testObjectCastToString()
} 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);

Expand Down

0 comments on commit 4a68775

Please sign in to comment.