/** Datei: library/ARUNOX/Tools/Debug/Debug.php **/
* @copyright 2024 Sparky@ARUNOX
* @version 1.0.0
* @since 1.0.0 File is created
*/
namespace ARUNOX\Tools;
use DateTime;
class Debug {
/* ### VARIABLES ################################################ # */
protected static bool $STATUS = FALSE;
protected static float $STARTTIME = 0;
protected static array $LASTLOGTIME = [];
protected static array $EXCLUDED_URLS = [];
/* ### PRIVATE STATIC FUNCTIONS ################################# # */
private static function parseNames( null|string|array $names = NULL ) {
$names = $names ?? "default";
$names = implode( ",", (array)$names );
$names = explode( ",", "{$names},all" );
$names = array_unique( $names );
$names = array_filter( $names );
return $names;
}
private static function parseEntry( array $entry ) : string {
$durationAll = number_format( $entry["duration"]["all"], 3, ",", "." );
$durationLast = number_format( $entry["duration"]["last"], 3, ",", "." );
return "{$durationAll} » {$durationLast} » {$entry["text"]}";
}
private static function parseUrl( null|string $url = NULL, null|string $method = NULL ) : string {
$url ??= $_SERVER["REQUEST_URI"];
$method ??= $_SERVER["REQUEST_METHOD"];
return "{$method} » {$url}";
}
/* ### PUBLIC STATIC FUNCTIONS ################################## # */
public static function setDebugging( bool $setting ) : void {
static::$STATUS = $setting;
}
public static function startDebugging() : void {
static::$STATUS = TRUE;
}
public static function stopDebugging() : void {
static::$STATUS = FALSE;
}
public static function isDebugging() : bool {
return static::$STATUS;
}
public static function addExcludedUrl( null|string $url = NULL, null|string $method = NULL ) : void {
static::$EXCLUDED_URLS[] = static::parseUrl( $url, $method );
static::$EXCLUDED_URLS = array_filter( static::$EXCLUDED_URLS );
static::$EXCLUDED_URLS = array_unique( static::$EXCLUDED_URLS );
}
public static function isUrlAllowed( null|string $url = NULL, null|string $method = NULL ) : bool {
return !in_array( static::parseUrl( $url, $method ), static::$EXCLUDED_URLS );
}
public static function setStartTime( null|float $startTime = NULL ) : float {
static::$STARTTIME = $startTime ?? microtime( TRUE );
return static::setLastLogTime( NULL, static::$STARTTIME );
}
public static function getStartTime( null|float $startTime = NULL ) : float {
return $startTime ?? static::$STARTTIME ?? microtime( TRUE );
}
public static function getDurationStart( null|float $startTime = NULL ) : float {
return microtime(TRUE ) - static::getStartTime( $startTime );
}
public static function setLastLogTime( null|string|array $names = NULL, null|float $lastLogTime = NULL ) : float {
$time = $lastLogTime ?? microtime( TRUE );
$names = static::parseNames( $names );
foreach( $names as $name ) static::$LASTLOGTIME[ $name ] = $time;
return $time;
}
public static function getLastLogTime( null|string|array $name = NULL, null|float $lastLogTime = NULL ) : float {
$time = $lastLogTime ?? static::$LASTLOGTIME ?? microtime( TRUE );
$name = static::parseNames( $name );
$name = current( $name );
if( !key_exists( $name, static::$LASTLOGTIME ) ) {
static::setLastLogTime( $name, static::getLastLogTime() );
}
return static::$LASTLOGTIME[ $name ] ?? $time;
}
public static function getDurationLast( null|string|array $name = NULL, null|float $lastLogTime = NULL ) : float {
return microtime(TRUE ) - static::getLastLogTime( $name, $lastLogTime );
}
public static function log( string $text, null|string|array $names = NULL ) : void {
if( !static::isDebugging() ) return;
if( !static::isUrlAllowed() ) return;
$names = static::parseNames( $names );
$url = static::parseUrl();
$start = date( "Y-m-d H:i:s", (int)static::getStartTime() );
$entry = [
"text" => $text,
"logtime" => microtime( TRUE ),
"duration" => [
"all" => static::getDurationStart(),
"last" => static::getDurationLast( $names ),
],
];
foreach( $names as $name ) {
static::setLastLogTime( $name );
$_SESSION["debug"]["log"][ $name ][ "{$start} - {$url}" ][] = $entry;
}
}
public static function clearLog() : void {
$_SESSION["debug"]["log"] = [];
}
public static function getLog( string $name ) : array {
$name = static::parseNames( $name );
$name = current( $name );
return $_SESSION["debug"]["log"][ $name ] ?? [];
}
public static function showLog( $clear = FALSE, $name = "default", $exit = TRUE ) {
if( empty( $_SESSION["debug"]["log"] ) ) return FALSE;
if( key_exists( $name, $_SESSION["debug"]["log"] ) ) {
$details = static::getLog( $name );
$details = array_map( fn( $group ) => implode( "\n", array_map( [ static::class, "parseEntry" ], $group ) ), $details );
show(
"DEBUGGING",
// "» " . str_replace( getcwd(), "", $debug["file"] ) . ":" . $debug["line"] . "",
$details,
"NEXT",
FALSE
);
}
if( $clear ) static::clearLog();
if( $exit ) exit;
}
public static function logTime( $startTime, $echo = FALSE, $title = NULL ) {
$duration = microtime(TRUE) - $startTime;
$_SESSION["debug"]["logtime"][] = $duration;
if( $echo ) {
echo "";
}
}
public static function showLogTime( $title = NULL ) {
static::logTime( StartTime, TRUE, $title );
}
public static function getClassName( $class_or_object, $split_by = "\_-" ) {
if( is_object( $class_or_object ) ) $class_name = get_class( $class_or_object );
elseif( is_string( $class_or_object ) ) $class_name = $class_or_object;
if( !isset( $class_name ) ) return FALSE;
$split_by = preg_quote( $split_by );
$class_name = preg_split( "/[{$split_by}]/", $class_name );
$class_name = array_map_ucwords( $class_name );
return $class_name;
}
public static function getClassTitle( $class_or_object, $namespace = "ARUNOX", $replace = "Module" ) {
$class_name = static::getClassName( $class_or_object );
$replace = implode( "\\", [ $namespace, $replace ] );
$replace = preg_quote( $replace );
$title = preg_replace( "/{$replace}/", "", $class_name );
$title = trim( $title, "\\" );
$title = preg_replace( "/(\\\)/", " \\ ", $title );
return $title;
}
public static function getClassHook( $class_or_object, $namespace = "ARUNOX", $replace = "Module" ) {
$class_name = static::getClassName( $class_or_object );
$replace = implode( "\\", [ $namespace, $replace ] );
$replace = preg_quote( $replace );
$hook = preg_replace( "/{$replace}/", "", $class_name );
$hook = trim( $hook, "\\" );
return $hook;
}
public static function getClassPath( $class_or_object, $namespace = "ARUNOX", $replace = "Module" ) {
$hook = static::getClassHook( $class_or_object, $namespace, $replace );
$path = preg_replace( "/\\\\/", "/", "\\{$hook}" );
return $path;
}
public static function getClassUri( $class_or_object, $namespace = "ARUNOX", $replace = "Module" ) {
$path = static::getClassPath( $class_or_object, $namespace, $replace );
$path = strtolower( $path );
return $path;
}
}