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>
|
|
|
|
*/
|
2020-08-29 12:16:59 +02:00
|
|
|
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
|
|
|
|
2020-08-08 22:07:39 +02:00
|
|
|
const STATUS_NOTICE = 0;
|
|
|
|
const STATUS_UPDATE = 1;
|
|
|
|
const STATUS_DELETION = 2;
|
|
|
|
const STATUS_ERROR = 3;
|
2020-08-08 22:23:27 +02:00
|
|
|
const STATUS_SKIPPED = 4;
|
2020-08-08 21:45:24 +02:00
|
|
|
|
|
|
|
const CONSOLE_LEVEL_COLORS = [
|
2020-08-08 22:07:39 +02:00
|
|
|
self::STATUS_NOTICE => "",
|
|
|
|
self::STATUS_ERROR => "41", // 0: Red background | Problem
|
|
|
|
self::STATUS_UPDATE => "42", // 1: Green background | Edited automatically
|
|
|
|
self::STATUS_DELETION => "44", // 2: Blue background | Deleted
|
|
|
|
self::STATUS_SKIPPED => "40", // 2: Blue background | Deleted
|
2020-08-08 21:45:24 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
const CONSOLE_LOG_USED_RESOURCES_ENABLED = 1;
|
|
|
|
const CONSOLE_LOG_USED_RESOURCES_DISABLED = 0;
|
|
|
|
|
2021-01-21 08:39:42 +01: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");
|
|
|
|
}
|
|
|
|
|
2020-09-03 19:56:55 +02:00
|
|
|
$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 = "-";
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($i = 0; $i < $msgLen + 6; $i++) echo $headingLvlChar;
|
|
|
|
echo PHP_EOL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints a headline.
|
|
|
|
*
|
|
|
|
* @param string $msg Message to print.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
final public static function note(string $msg):void {
|
|
|
|
|
2020-09-03 19:56:55 +02:00
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param string $context Context this is called from.
|
|
|
|
* @param integer $level Level of the notification.
|
|
|
|
* @param integer $editedNo Number of edited entries.
|
|
|
|
* @param string $additionalNote Optional, additional note.
|
|
|
|
* @param string $link Link.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-08-08 22:07:39 +02:00
|
|
|
final public static function write(string $context, int $level = self::STATUS_NOTICE, int $editedNo = 0, string $additionalNote = "", string $link = ""):void {
|
2020-08-08 21:45:24 +02:00
|
|
|
|
2021-01-21 08:39:42 +01:00
|
|
|
if (self::$verbose === false and $level === self::STATUS_NOTICE) return;
|
|
|
|
|
2020-08-08 21:45:24 +02:00
|
|
|
// Get parts for generating the message to log.
|
2020-09-03 19:56:55 +02:00
|
|
|
$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;
|
|
|
|
|
|
|
|
$levelColor = self::CONSOLE_LEVEL_COLORS[$level];
|
|
|
|
|
2020-08-27 16:14:18 +02:00
|
|
|
if ($link === "")
|
2020-09-03 19:56:55 +02:00
|
|
|
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);
|
2020-08-08 21:45:24 +02:00
|
|
|
else
|
2020-09-03 19:56:55 +02:00
|
|
|
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 {
|
|
|
|
|
2020-09-03 19:56:55 +02:00
|
|
|
$startTime = \microtime(true);
|
|
|
|
\register_shutdown_function(function($startTime) :void {
|
2020-08-08 21:45:24 +02:00
|
|
|
|
2020-09-03 19:56:55 +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();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|