mirror of
https://github.com/maximebf/php-debugbar.git
synced 2025-07-26 03:01:23 +02:00
Update interpolate on MessagesCollector (#572)
This commit is contained in:
@@ -195,7 +195,7 @@ class MessagesCollector extends AbstractLogger implements DataCollectorInterface
|
|||||||
/**
|
/**
|
||||||
* Interpolates context values into the message placeholders.
|
* Interpolates context values into the message placeholders.
|
||||||
*
|
*
|
||||||
* @param $message
|
* @param string $message
|
||||||
* @param array $context
|
* @param array $context
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
@@ -204,9 +204,24 @@ class MessagesCollector extends AbstractLogger implements DataCollectorInterface
|
|||||||
// build a replacement array with braces around the context keys
|
// build a replacement array with braces around the context keys
|
||||||
$replace = array();
|
$replace = array();
|
||||||
foreach ($context as $key => $val) {
|
foreach ($context as $key => $val) {
|
||||||
|
$placeholder = '{' . $key . '}';
|
||||||
|
if (strpos($message, $placeholder) === false) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
// check that the value can be cast to string
|
// check that the value can be cast to string
|
||||||
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
|
if (null === $val || is_scalar($val) || (is_object($val) && method_exists($val, "__toString"))) {
|
||||||
$replace['{' . $key . '}'] = $val;
|
$replace[$placeholder] = $val;
|
||||||
|
} elseif ($val instanceof \DateTimeInterface) {
|
||||||
|
$replace[$placeholder] = $val->format("Y-m-d\TH:i:s.uP");
|
||||||
|
} elseif ($val instanceof \UnitEnum) {
|
||||||
|
$replace[$placeholder] = $val instanceof \BackedEnum ? $val->value : $val->name;
|
||||||
|
} elseif (is_object($val)) {
|
||||||
|
$replace[$placeholder] = '[object ' . $this->getDataFormatter()->formatClassName($val) . ']';
|
||||||
|
} elseif (is_array($val)) {
|
||||||
|
$json = @json_encode($val);
|
||||||
|
$replace[$placeholder] = false === $json ? 'null' : 'array' . $json;
|
||||||
|
} else {
|
||||||
|
$replace[$placeholder] = '['.gettype($val).']';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -84,4 +84,23 @@ class DataFormatter implements DataFormatterInterface
|
|||||||
$suffixes = array('B', 'KB', 'MB', 'GB', 'TB');
|
$suffixes = array('B', 'KB', 'MB', 'GB', 'TB');
|
||||||
return $sign . round(pow(1024, $base - floor($base)), $precision) . $suffixes[(int) floor($base)];
|
return $sign . round(pow(1024, $base - floor($base)), $precision) . $suffixes[(int) floor($base)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param object $object
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function formatClassName($object)
|
||||||
|
{
|
||||||
|
$class = \get_class($object);
|
||||||
|
|
||||||
|
if (false === ($pos = \strpos($class, "@anonymous\0"))) {
|
||||||
|
return $class;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (false === ($parent = \get_parent_class($class))) {
|
||||||
|
return \substr($class, 0, $pos + 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $parent . '@anonymous';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user