Updates for v9.0 release
This commit is contained in:
@@ -21,7 +21,7 @@ class Support
|
||||
*/
|
||||
public static function printTime(int $set_microtime = -1): string
|
||||
{
|
||||
list($microtime, $timestamp) = explode(' ', microtime());
|
||||
[$microtime, $timestamp] = explode(' ', microtime());
|
||||
$string = date("Y-m-d H:i:s", (int)$timestamp);
|
||||
// if microtime flag is -1 no round, if 0, no microtime, if >= 1, round that size
|
||||
if ($set_microtime == -1) {
|
||||
@@ -37,23 +37,21 @@ class Support
|
||||
* prints a html formatted (pre) array
|
||||
*
|
||||
* @param array<mixed> $array any array
|
||||
* @param bool $no_html set to true to use ##HTMLPRE##
|
||||
* @param bool $no_html default add <pre>
|
||||
* @return string formatted array for output with <pre> tag added
|
||||
*/
|
||||
public static function printAr(array $array, bool $no_html = false): string
|
||||
{
|
||||
if ($no_html === false) {
|
||||
return "<pre>" . print_r($array, true) . "</pre>";
|
||||
} else {
|
||||
return '##HTMLPRE##' . print_r($array, true) . '##/HTMLPRE##';
|
||||
}
|
||||
return $no_html ?
|
||||
print_r($array, true) :
|
||||
'<pre>' . print_r($array, true) . '</pre>';
|
||||
}
|
||||
|
||||
/**
|
||||
* alternate name for printAr function
|
||||
*
|
||||
* @param array<mixed> $array any array
|
||||
* @param bool $no_html set to true to use ##HTMLPRE##
|
||||
* @param bool $no_html default add <pre>
|
||||
* @return string formatted array for output with <pre> tag added
|
||||
*/
|
||||
public static function printArray(array $array, bool $no_html = false): string
|
||||
@@ -61,26 +59,61 @@ class Support
|
||||
return self::printAr($array, $no_html);
|
||||
}
|
||||
|
||||
/**
|
||||
* A replacement for the \CoreLibs\Debug\Support::printAr
|
||||
* But this does not wrap it in <pre></pre>
|
||||
* Do not use this without using it in a string in debug function
|
||||
* Note: for full data debug dumps use Support::dumpVar()
|
||||
*
|
||||
* @param array<mixed> $a Array to format
|
||||
* @return string print_r formated
|
||||
*/
|
||||
public static function prAr(array $a): string
|
||||
{
|
||||
return self::printAr($a, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* convert bool value to string
|
||||
* if $name is set prefix with nae
|
||||
* default true: true, false: false
|
||||
*
|
||||
* @param bool $bool Variable to convert
|
||||
* @param string $name [default: ''] Prefix name
|
||||
* @param string $true [default: true] True string
|
||||
* @param string $false [default: false] False string
|
||||
* @return string String with converted bool text for debug
|
||||
* @param bool $bool Variable to convert
|
||||
* @param string $name [default: ''] Prefix name
|
||||
* @param string $true [default: 'true'] True string
|
||||
* @param string $false [default: 'false'] False string
|
||||
* @param bool $no_html [default: false] if true do not print html
|
||||
* @return string String with converted bool text for debug
|
||||
*/
|
||||
public static function printBool(
|
||||
bool $bool,
|
||||
string $name = '',
|
||||
string $true = 'true',
|
||||
string $false = 'false',
|
||||
bool $no_html = false,
|
||||
): string {
|
||||
return
|
||||
(!empty($name) ?
|
||||
($no_html ?
|
||||
$name : '<b>' . $name . '</b>') . ': '
|
||||
: '')
|
||||
. ($bool ? $true : $false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert bool value to string value. Short name alias for printBool
|
||||
*
|
||||
* @param bool $bool Bool value to be transformed
|
||||
* @param string $true [default: 'true'] Override default string 'true'
|
||||
* @param string $false [default: 'false'] Override default string 'false'
|
||||
* @return string $true or $false string for true/false bool
|
||||
*/
|
||||
public static function prBl(
|
||||
bool $bool,
|
||||
string $true = 'true',
|
||||
string $false = 'false'
|
||||
): string {
|
||||
$string = (!empty($name) ? '<b>' . $name . '</b>: ' : '')
|
||||
. ($bool ? $true : $false);
|
||||
return $string;
|
||||
return self::printBool($bool, '', $true, $false, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,9 +122,10 @@ class Support
|
||||
* if object return get_class
|
||||
* for array use printAr function, can be controlled with no_html for
|
||||
* Debug\Logging compatible output
|
||||
* Recommended to use Support::dumpVar()
|
||||
*
|
||||
* @param mixed $mixed
|
||||
* @param bool $no_html set to true to use ##HTMLPRE##or html escape
|
||||
* @param bool $no_html set to true to strip <pre> tags
|
||||
* @return string
|
||||
*/
|
||||
public static function printToString(mixed $mixed, bool $no_html = false): string
|
||||
@@ -119,12 +153,93 @@ class Support
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dumps var data and returns it as string
|
||||
* var_dump based
|
||||
* Recommended debug output
|
||||
*
|
||||
* @param mixed $data Anything
|
||||
* @param bool $no_html [default=false] If true strip all html tags
|
||||
* (for text print)
|
||||
* @return string A text string
|
||||
*/
|
||||
public static function dumpVar(
|
||||
mixed $data,
|
||||
bool $no_html = false,
|
||||
): string {
|
||||
// dump data
|
||||
ob_start();
|
||||
var_dump($data);
|
||||
$debug_dump = ob_get_clean() ?: '[FAILED TO GET var_dump() data]';
|
||||
// check if the original caller is dV, if yes, up the caller level for
|
||||
// the file line get by 1, so we get file + pos from the dV call and
|
||||
// not this call
|
||||
$caller_level = 1;
|
||||
$caller_list = self::getCallerMethodList();
|
||||
if ($caller_list[0] == 'dV') {
|
||||
echo "Raise caller level<br>: " . $caller_list[0] . "<br>";
|
||||
$caller_level = 2;
|
||||
}
|
||||
// we need to strip the string in <small></small that is
|
||||
// "path ... CoreLibs/Debug/Support.php:<number>:
|
||||
// and replace it with the caller methods and location
|
||||
$caller_file_number = self::getCallerFileLine($caller_level);
|
||||
$debug_dump = preg_replace(
|
||||
'|<small>(/.*:\d+:)</small>|',
|
||||
'<small>' . $caller_file_number . ':</small>',
|
||||
$debug_dump
|
||||
) ?? $debug_dump; // in case of failure keep original
|
||||
// if strip is ture, remove all HTML tags and convert any html entities back
|
||||
return $no_html ?
|
||||
str_replace(
|
||||
// things to replace in the string if set
|
||||
['>', '<', ' ', ' '],
|
||||
['>', '<', "\r", "\n"],
|
||||
strip_tags($debug_dump)
|
||||
) :
|
||||
$debug_dump;
|
||||
}
|
||||
|
||||
/**
|
||||
* exports (dumps) var, in more printable design, but without detail info
|
||||
*
|
||||
* @param mixed $data Anything
|
||||
* @param bool $no_html If true true do not add <pre> tags
|
||||
* @return string A text string
|
||||
*/
|
||||
public static function exportVar(mixed $data, bool $no_html = false): string
|
||||
{
|
||||
return $no_html ?
|
||||
var_export($data, true) :
|
||||
'<pre>' . var_export($data, true) . '</pre>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return file name and line number where this was called
|
||||
* One level up
|
||||
*
|
||||
* @param int $level trace level, default 1
|
||||
* @return string|null null or file name:line number
|
||||
*/
|
||||
public static function getCallerFileLine(int $level = 1): ?string
|
||||
{
|
||||
$traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
// print \CoreLibs\Debug\Support::printAr($traces);
|
||||
// We should check from top down if unset?
|
||||
// sets the start point here, and in level two (the sub call) we find this
|
||||
if (isset($traces[$level])) {
|
||||
return ($traces[$level]['file'] ?? $traces[$level]['function'])
|
||||
. ':' . ($traces[$level]['line'] ?? '-');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* if there is a need to find out which parent method called a child method,
|
||||
* eg for debugging, this function does this
|
||||
*
|
||||
* call this method in the child method and you get the parent function that called
|
||||
* @param int $level debug level, default 1
|
||||
* @param int $level trace level, default 1
|
||||
* @return ?string null or the function that called the function
|
||||
* where this method is called
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user