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 {
|
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_ENABLED = 1;
|
||||||
const CONSOLE_LOG_USED_RESOURCES_DISABLED = 0;
|
const CONSOLE_LOG_USED_RESOURCES_DISABLED = 0;
|
||||||
|
|
||||||
|
@ -81,17 +67,17 @@ final class MDConsole {
|
||||||
/**
|
/**
|
||||||
* Formats a message uniformly, maybe also to stdout.
|
* Formats a message uniformly, maybe also to stdout.
|
||||||
*
|
*
|
||||||
* @param string $context Context this is called from.
|
* @param string $context Context this is called from.
|
||||||
* @param integer $level Level of the notification.
|
* @param MDConsoleStatus $level Level of the notification.
|
||||||
* @param integer $editedNo Number of edited entries.
|
* @param integer $editedNo Number of edited entries.
|
||||||
* @param string $additionalNote Optional, additional note.
|
* @param string $additionalNote Optional, additional note.
|
||||||
* @param string $link Link.
|
* @param string $link Link.
|
||||||
*
|
*
|
||||||
* @return void
|
* @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.
|
// Get parts for generating the message to log.
|
||||||
$additionalNote = \strtr($additionalNote, ["ä" => "ae", "ö" => "oe", "ü" => "ue"]);
|
$additionalNote = \strtr($additionalNote, ["ä" => "ae", "ö" => "oe", "ü" => "ue"]);
|
||||||
|
@ -100,7 +86,7 @@ final class MDConsole {
|
||||||
// Generate the message to log.
|
// Generate the message to log.
|
||||||
# $message = sprintf("%1$07s", memory_get_usage()) . " :: {$date} :: {$msg}" . PHP_EOL;
|
# $message = sprintf("%1$07s", memory_get_usage()) . " :: {$date} :: {$msg}" . PHP_EOL;
|
||||||
|
|
||||||
$levelColor = self::CONSOLE_LEVEL_COLORS[$level];
|
$levelColor = $level->color();
|
||||||
|
|
||||||
if ($link === "") {
|
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);
|
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.
|
* Main function of this class.
|
||||||
* Formats a message uniformly and writes it to the log, maybe also to stdout.
|
* Formats a message uniformly and writes it to the log, maybe also to stdout.
|
||||||
*
|
*
|
||||||
* @param string $msg Message to log.
|
* @param string $msg Message to log.
|
||||||
* @param integer $statusCode Status code.
|
* @param MDConsoleStatus $statusCode Status code.
|
||||||
* 0: Note
|
|
||||||
* 1: Skipped
|
|
||||||
* 2: Update
|
|
||||||
* 3: Error.
|
|
||||||
*
|
*
|
||||||
* @return void
|
* @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.
|
// Get parts for generating the message to log.
|
||||||
++$this->_counter; // Increment counter
|
++$this->_counter; // Increment counter
|
||||||
$date = \date("Y-m-d H:i:s"); // Get current date & time.
|
$date = \date("Y-m-d H:i:s"); // Get current date & time.
|
||||||
|
|
||||||
switch ($statusCode) {
|
$statusSymbol = match($statusCode) {
|
||||||
case MDConsole::STATUS_SKIPPED:
|
MDConsoleStatus::SKIPPED => "-",
|
||||||
$statusSymbol = "-";
|
MDConsoleStatus::UPDATE => "U",
|
||||||
break;
|
MDConsoleStatus::ERROR => "E",
|
||||||
case MDConsole::STATUS_UPDATE:
|
MDConsoleStatus::DELETION => "D",
|
||||||
$statusSymbol = "U";
|
MDConsoleStatus::NOTICE => "N",
|
||||||
break;
|
};
|
||||||
case MDConsole::STATUS_ERROR:
|
|
||||||
$statusSymbol = "E";
|
|
||||||
break;
|
|
||||||
case MDConsole::STATUS_DELETION:
|
|
||||||
$statusSymbol = "D";
|
|
||||||
break;
|
|
||||||
case MDConsole::STATUS_NOTICE:
|
|
||||||
default:
|
|
||||||
$statusSymbol = "N";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->_context !== "") $contextStr = "{$this->_context} │ ";
|
if ($this->_context !== "") $contextStr = "{$this->_context} │ ";
|
||||||
else $contextStr = "";
|
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) {
|
if ($this->show_operation_number === true && $this->_counter % 250 === 0) {
|
||||||
echo '―――> ' . \sprintf("%1$07d", $this->_counter) . " operations processed" . PHP_EOL;
|
echo '―――> ' . \sprintf("%1$07d", $this->_counter) . " operations processed" . PHP_EOL;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user