Add CLI Support

This commit is contained in:
Patrick Niebeling
2025-02-16 22:20:39 +01:00
parent ce5b309f96
commit 0901172ec6

View File

@ -20,6 +20,15 @@
class Logger
{
/**
* Prüft, ob das Skript in der CLI läuft
* @return bool
*/
protected static function isCLI()
{
return PHP_SAPI === 'cli';
}
/**
* $log_file - path and log file name
* @var string
@ -295,19 +304,17 @@ class Logger
}
/**
* Convert absolute path to relative url (using UNIX directory seperators)
*
* E.g.:
* Input: D:\development\htdocs\public\todo-list\index.php
* Output: localhost/todo-list/index.php
*
* @param string Absolute directory/path of file which should be converted to a relative (url) path
* @return string Relative path
* Konvertiert einen absoluten Pfad in einen relativen (unter Berücksichtigung von CLI)
*/
public static function absToRelPath($pathToConvert)
{
if (static::isCLI()) {
return $pathToConvert; // In CLI einfach den absoluten Pfad zurückgeben
}
$pathAbs = str_replace(['/', '\\'], '/', $pathToConvert);
$documentRoot = str_replace(['/', '\\'], '/', $_SERVER['DOCUMENT_ROOT']);
$documentRoot = str_replace(['/', '\\'], '/', $_SERVER['DOCUMENT_ROOT'] ?? '');
return $_SERVER['SERVER_NAME'] . str_replace($documentRoot, '', $pathAbs);
}
@ -346,4 +353,5 @@ class Logger
*/
private function __destruct()
{ }
}
}