From 9f2cbeca6fd965754a7988e14beb52b8b628a6ef Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Thu, 26 Sep 2024 13:52:39 +0200 Subject: [PATCH] Improve format_callable() --- src/includes/functions.php | 47 +++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/src/includes/functions.php b/src/includes/functions.php index 14376ac7..fa325bd4 100644 --- a/src/includes/functions.php +++ b/src/includes/functions.php @@ -746,20 +746,45 @@ function dumpFile($file_path, $mime_type=null, $max_age=3600, $force_download=fa /** * Format a callable object for logging - * @param callable $callable The callable object + * @param string|array|\ReflectionMethod|\ReflectionFunction $callable The callable object + * @param null|array $args Optional argument(s) * @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)) - return $callable."()"; - if (is_array($callable) && count($callable)==2) - if (is_string($callable[0])) - return $callable[0]."::".$callable[1]."()"; - elseif (is_object($callable[0])) - return get_class($callable[0])."->".$callable[1]."()"; - else - return "Unkown->".$callable[1]."()"; - return varDump($callable); + return $callable."($formatted_args)"; + if (is_array($callable)) + if (is_string($callable[0])) + return $callable[0]."::".$callable[1]."($formatted_args)"; + elseif (is_object($callable[0])) + return get_class($callable[0])."->".$callable[1]."($formatted_args)"; + else + return "Unknown->".$callable[1]."($formatted_args)"; + 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 $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) {