Skip to content

Commit 7602079

Browse files
authored
Add support for PSR-3 message placeholders (php-debugbar#465)
1 parent 150ef8d commit 7602079

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/DebugBar/DataCollector/MessagesCollector.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,29 @@ public function getMessages()
185185
*/
186186
public function log($level, $message, array $context = array())
187187
{
188-
$this->addMessage($message, $level);
188+
$this->addMessage($this->interpolate($message, $context), $level);
189+
}
190+
191+
/**
192+
* Interpolates context values into the message placeholders.
193+
*
194+
* @param $message
195+
* @param array $context
196+
* @return string
197+
*/
198+
function interpolate($message, array $context = array())
199+
{
200+
// build a replacement array with braces around the context keys
201+
$replace = array();
202+
foreach ($context as $key => $val) {
203+
// check that the value can be cast to string
204+
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
205+
$replace['{' . $key . '}'] = $val;
206+
}
207+
}
208+
209+
// interpolate replacement values into the message and return
210+
return strtr($message, $replace);
189211
}
190212

191213
/**

0 commit comments

Comments
 (0)