1
0
mirror of https://github.com/maximebf/php-debugbar.git synced 2025-01-16 21:08:34 +01:00

Add support for PSR-3 message placeholders (#465)

This commit is contained in:
Pierre Rudloff 2021-06-19 19:08:10 +02:00 committed by GitHub
parent 150ef8d8a3
commit 7602079356
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -185,7 +185,29 @@ class MessagesCollector extends AbstractLogger implements DataCollectorInterface
*/
public function log($level, $message, array $context = array())
{
$this->addMessage($message, $level);
$this->addMessage($this->interpolate($message, $context), $level);
}
/**
* Interpolates context values into the message placeholders.
*
* @param $message
* @param array $context
* @return string
*/
function interpolate($message, array $context = array())
{
// build a replacement array with braces around the context keys
$replace = array();
foreach ($context as $key => $val) {
// check that the value can be cast to string
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
$replace['{' . $key . '}'] = $val;
}
}
// interpolate replacement values into the message and return
return strtr($message, $replace);
}
/**