Use enums for status codes
This commit is contained in:
parent
cecde961d8
commit
be4752197c
@ -11,20 +11,6 @@ declare(strict_types = 1);
|
||||
*/
|
||||
final class MDConsole {
|
||||
|
||||
const STATUS_NOTICE = 0;
|
||||
const STATUS_UPDATE = 1;
|
||||
const STATUS_DELETION = 2;
|
||||
const STATUS_ERROR = 3;
|
||||
const STATUS_SKIPPED = 4;
|
||||
|
||||
const CONSOLE_LEVEL_COLORS = [
|
||||
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
|
||||
];
|
||||
|
||||
const CONSOLE_LOG_USED_RESOURCES_ENABLED = 1;
|
||||
const CONSOLE_LOG_USED_RESOURCES_DISABLED = 0;
|
||||
|
||||
@ -81,17 +67,17 @@ final class MDConsole {
|
||||
/**
|
||||
* 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.
|
||||
* @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.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
final public static function write(string $context, int $level = self::STATUS_NOTICE, int $editedNo = 0, string $additionalNote = "", string $link = ""):void {
|
||||
final public static function write(string $context, MDConsoleStatus $level = MDConsoleStatus::NOTICE, int $editedNo = 0, string $additionalNote = "", string $link = ""):void {
|
||||
|
||||
if (self::$verbose === false and $level === self::STATUS_NOTICE) return;
|
||||
if (self::$verbose === false and $level === MDConsoleStatus::NOTICE) return;
|
||||
|
||||
// Get parts for generating the message to log.
|
||||
$additionalNote = \strtr($additionalNote, ["ä" => "ae", "ö" => "oe", "ü" => "ue"]);
|
||||
@ -100,7 +86,7 @@ final class MDConsole {
|
||||
// Generate the message to log.
|
||||
# $message = sprintf("%1$07s", memory_get_usage()) . " :: {$date} :: {$msg}" . PHP_EOL;
|
||||
|
||||
$levelColor = self::CONSOLE_LEVEL_COLORS[$level];
|
||||
$levelColor = $level->color();
|
||||
|
||||
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);
|
||||
|
32
src/MDConsoleStatus.php
Normal file
32
src/MDConsoleStatus.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?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);
|
||||
|
||||
enum MDConsoleStatus {
|
||||
case NOTICE;
|
||||
case UPDATE;
|
||||
case DELETION;
|
||||
case ERROR;
|
||||
case SKIPPED;
|
||||
|
||||
/**
|
||||
* Returns the color for each status.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function color():string {
|
||||
|
||||
return match($this) {
|
||||
self::NOTICE => "",
|
||||
self::ERROR => "41", // 0: Red background | Problem
|
||||
self::UPDATE => "42", // 1: Green background | Edited automatically
|
||||
self::DELETION => "44", // 2: Blue background | Deleted
|
||||
self::SKIPPED => "40", // 2: Blue background | Deleted
|
||||
};
|
||||
|
||||
}
|
||||
}
|
@ -119,39 +119,24 @@ final class MDOutputHandler {
|
||||
* Main function of this class.
|
||||
* Formats a message uniformly and writes it to the log, maybe also to stdout.
|
||||
*
|
||||
* @param string $msg Message to log.
|
||||
* @param integer $statusCode Status code.
|
||||
* 0: Note
|
||||
* 1: Skipped
|
||||
* 2: Update
|
||||
* 3: Error.
|
||||
* @param string $msg Message to log.
|
||||
* @param MDConsoleStatus $statusCode Status code.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function toLog(string $msg, int $statusCode = 0):void {
|
||||
public function toLog(string $msg, MDConsoleStatus $statusCode = MDConsoleStatus::NOTICE):void {
|
||||
|
||||
// Get parts for generating the message to log.
|
||||
++$this->_counter; // Increment counter
|
||||
$date = \date("Y-m-d H:i:s"); // Get current date & time.
|
||||
|
||||
switch ($statusCode) {
|
||||
case MDConsole::STATUS_SKIPPED:
|
||||
$statusSymbol = "-";
|
||||
break;
|
||||
case MDConsole::STATUS_UPDATE:
|
||||
$statusSymbol = "U";
|
||||
break;
|
||||
case MDConsole::STATUS_ERROR:
|
||||
$statusSymbol = "E";
|
||||
break;
|
||||
case MDConsole::STATUS_DELETION:
|
||||
$statusSymbol = "D";
|
||||
break;
|
||||
case MDConsole::STATUS_NOTICE:
|
||||
default:
|
||||
$statusSymbol = "N";
|
||||
break;
|
||||
}
|
||||
$statusSymbol = match($statusCode) {
|
||||
MDConsoleStatus::SKIPPED => "-",
|
||||
MDConsoleStatus::UPDATE => "U",
|
||||
MDConsoleStatus::ERROR => "E",
|
||||
MDConsoleStatus::DELETION => "D",
|
||||
MDConsoleStatus::NOTICE => "N",
|
||||
};
|
||||
|
||||
if ($this->_context !== "") $contextStr = "{$this->_context} │ ";
|
||||
else $contextStr = "";
|
||||
@ -166,7 +151,7 @@ final class MDOutputHandler {
|
||||
}
|
||||
}
|
||||
|
||||
if (($this->_verbosity > 0 && \in_array($statusCode, [MDConsole::STATUS_UPDATE, MDConsole::STATUS_DELETION, MDConsole::STATUS_ERROR], true)) || $this->_verbosity === 2) echo $message;
|
||||
if (($this->_verbosity > 0 && \in_array($statusCode, [MDConsoleStatus::UPDATE, MDConsoleStatus::DELETION, MDConsoleStatus::ERROR], true)) || $this->_verbosity === 2) echo $message;
|
||||
|
||||
if ($this->show_operation_number === true && $this->_counter % 250 === 0) {
|
||||
echo '―――> ' . \sprintf("%1$07d", $this->_counter) . " operations processed" . PHP_EOL;
|
||||
|
Loading…
Reference in New Issue
Block a user