MDConsole/src/MDConsole.php

135 lines
4.0 KiB
PHP
Raw Normal View History

2020-08-08 21:45:24 +02:00
<?PHP
/**
* Provides an interface for CLI outputs of museum-digital's dev and admin tools.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
2020-08-08 21:45:24 +02:00
/**
* Interface for CLI outputs of museum-digital's dev and admin tools.
*/
2020-08-29 12:57:08 +02:00
final class MDConsole {
2020-08-08 21:45:24 +02:00
public const CONSOLE_LOG_USED_RESOURCES_ENABLED = 1;
public const CONSOLE_LOG_USED_RESOURCES_DISABLED = 0;
2020-08-08 21:45:24 +02:00
/** @var boolean */
public static $verbose = true;
2020-08-08 21:45:24 +02:00
/**
* Prints a headline.
*
* @param string $msg Message to print.
* @param integer $level Heading level.
*
* @return void
*/
final public static function heading(string $msg, int $level = 2):void {
if ($level < 1 || $level > 6) {
throw new MDConsoleInvalidHeadingNumber("Invalid heading number");
}
$msgLen = \strlen($msg);
echo \sprintf(" \033[1;30;m\033[0m\033[1m%s \033[0m" . PHP_EOL, $msg);
2020-08-08 21:45:24 +02:00
if ($level >= 3) return;
switch ($level) {
case 1:
$headingLvlChar = "=";
case 2:
default:
$headingLvlChar = "-";
}
2021-02-06 21:41:39 +01:00
for ($i = 0; $i < $msgLen + 6; $i++) {
echo $headingLvlChar;
}
2020-08-08 21:45:24 +02:00
echo PHP_EOL;
}
/**
* Prints a headline.
*
* @param string $msg Message to print.
*
* @return void
*/
final public static function note(string $msg):void {
echo PHP_EOL . \sprintf("\033[30m(%s)%s \033[0m" . PHP_EOL . PHP_EOL, \date("H:i:s"), $msg);
2020-08-08 21:45:24 +02:00
}
/**
* Formats a message uniformly, maybe also to stdout.
*
2022-08-15 15:51:34 +02:00
* @param string $context Context this is called from.
* @param MDConsoleStatus $level Level of the notification.
* @param integer $editedNo Number of edited entries.
* @param string $additionalNote Optional, additional note.
* @param string $link Link.
2020-08-08 21:45:24 +02:00
*
* @return void
*/
2022-08-15 15:51:34 +02:00
final public static function write(string $context, MDConsoleStatus $level = MDConsoleStatus::NOTICE, int $editedNo = 0, string $additionalNote = "", string $link = ""):void {
2020-08-08 21:45:24 +02:00
2022-08-15 15:51:34 +02:00
if (self::$verbose === false and $level === MDConsoleStatus::NOTICE) return;
2020-08-08 21:45:24 +02:00
// Get parts for generating the message to log.
$additionalNote = \strtr($additionalNote, ["ä" => "ae", "ö" => "oe", "ü" => "ue"]);
$date = \date("H:i:s"); // Get current date & time.
2020-08-08 21:45:24 +02:00
// Generate the message to log.
# $message = sprintf("%1$07s", memory_get_usage()) . " :: {$date} :: {$msg}" . PHP_EOL;
2022-08-15 15:51:34 +02:00
$levelColor = $level->color();
2020-08-08 21:45:24 +02:00
2021-02-06 21:41:39 +01:00
if ($link === "") {
echo \sprintf("\033[1;30;" . $levelColor . "m_____\033[0m\033[0m %-20s \033[40m::\033[0m # \033[32m%+9s\033[0m \033[40m::\033[0m 📝 %-50s \033[40m::\033[0m 📅 %+20s\n", $context, $editedNo, $additionalNote, $date);
2021-02-06 21:41:39 +01:00
}
else {
echo \sprintf("\033[1;30;" . $levelColor . "m_____\033[0m\033[0m %-20s \033[40m::\033[0m # \033[32m%+9s\033[0m \033[40m::\033[0m 📝 %-50s \033[40m::\033[0m 📅 %+20s \033[40m::\033[0m $link\n", $context, $editedNo, $additionalNote, $date);
}
2020-08-08 21:45:24 +02:00
}
/**
* Sets up last line of CLI output.
*
* @return void
*/
final public static function setupCLI():void {
$startTime = \microtime(true);
\register_shutdown_function(function($startTime) :void {
2020-08-08 21:45:24 +02:00
$duration = \microtime(true) - $startTime;
2020-08-08 21:45:24 +02:00
if ($duration < 30) return;
2020-09-05 22:32:12 +02:00
$longDurationMsg = "Long script run" . PHP_EOL;
$longDurationMsg .= "Script run time, that's: " . \round($duration, 2) . PHP_EOL;
$longDurationMsg .= "Peak memory usage: " . \memory_get_peak_usage() . PHP_EOL;
$longDurationMsg .= "Memory usage at end of script: " . \memory_get_usage() . PHP_EOL;
self::note($longDurationMsg);
2020-08-08 21:45:24 +02:00
}, $startTime);
}
/**
* Constructor.
*
* @return void
*/
public function __construct() {
self::setupCLI();
}
}