Improve format_callable()

This commit is contained in:
Benjamin Renard 2024-09-26 13:52:39 +02:00
parent 0e8009420c
commit 9f2cbeca6f
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -746,20 +746,45 @@ function dumpFile($file_path, $mime_type=null, $max_age=3600, $force_download=fa
/** /**
* Format a callable object for logging * Format a callable object for logging
* @param callable $callable The callable object * @param string|array|\ReflectionMethod|\ReflectionFunction $callable The callable object
* @param null|array<int,mixed> $args Optional argument(s)
* @return string The callable object string representation * @return string The callable object string representation
*/ */
function format_callable($callable) { function format_callable($callable, $args=null) {
$formatted_args = format_callable_args($args);
if (is_string($callable)) if (is_string($callable))
return $callable."()"; return $callable."($formatted_args)";
if (is_array($callable) && count($callable)==2) if (is_array($callable))
if (is_string($callable[0])) if (is_string($callable[0]))
return $callable[0]."::".$callable[1]."()"; return $callable[0]."::".$callable[1]."($formatted_args)";
elseif (is_object($callable[0])) elseif (is_object($callable[0]))
return get_class($callable[0])."->".$callable[1]."()"; return get_class($callable[0])."->".$callable[1]."($formatted_args)";
else else
return "Unkown->".$callable[1]."()"; return "Unknown->".$callable[1]."($formatted_args)";
return varDump($callable); if ($callable instanceof \ReflectionFunction)
return sprintf("%s(%s)", $callable->name, $formatted_args);
if ($callable instanceof \ReflectionMethod)
return sprintf(
"%s::%s(%s)",
$callable->class,
$callable->name,
$formatted_args
);
return sprintf("%s(%s)", varDump($callable), $formatted_args);
}
/**
* Format callable arguments for logging
* @param array<mixed> $args Arguments
* @return string
*/
function format_callable_args($args) {
if (!is_array($args) || empty($args))
return "";
$formatted_args = [];
foreach($args as $arg)
$formatted_args[] = str_replace("\n", '\n', var_export($arg, true));
return implode(", ", $formatted_args);
} }
function is_empty($val) { function is_empty($val) {