From f6c15f130a0f2a9c8e88c85f5edd14a2c5a4b985 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Thu, 28 Dec 2023 11:54:15 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/output/IMessenger.php | 5 ++ src/output/TODO.md | 5 -- src/output/_messenger.php | 73 ++++++++++++++++--------- src/output/log.php | 12 +++- src/output/msg.php | 20 ++++--- src/output/say.php | 12 +++- src/output/std/ProxyMessenger.php | 42 ++++++++++++++ src/output/std/StdMessenger.php | 91 ++++++++++++++++++++++++++++--- tbin/test-console.php | 7 +++ 9 files changed, 215 insertions(+), 52 deletions(-) create mode 100644 src/output/std/ProxyMessenger.php diff --git a/src/output/IMessenger.php b/src/output/IMessenger.php index 84dc3ad..f645f11 100644 --- a/src/output/IMessenger.php +++ b/src/output/IMessenger.php @@ -10,6 +10,11 @@ interface IMessenger { /** réinitialiser les paramètres de l'objet */ function resetParams(?array $params=null): void; + /** + * créer une copie de cet objet avec éventuellement des paramètres différents + */ + function clone(?array $params=null): self; + /** * commencer une section. * diff --git a/src/output/TODO.md b/src/output/TODO.md index 270ebad..2f03b55 100644 --- a/src/output/TODO.md +++ b/src/output/TODO.md @@ -2,11 +2,6 @@ ## TOOD -* [ ] ProxyMessenger, vers un ou plusieurs messengers -* [ ] support sous-système (instance locale ou globale) - * [ ] msg::push() --> nouvelle instance globale - * [ ] msg::get() --> nouvelle instance locale -* [ ] support `add_date` et `date_format` * [ ] affichage des exceptions en message technique. si pas de message, prendre le message de l'exception par défaut * [ ] possibilité de paramétrer le nom du fichier destination pour faire une diff --git a/src/output/_messenger.php b/src/output/_messenger.php index 068b711..43ef89a 100644 --- a/src/output/_messenger.php +++ b/src/output/_messenger.php @@ -1,8 +1,8 @@ clone($params); + } + + /** @var IMessenger */ + protected static $msg; + + /** @var IMessenger[] */ + protected static $stack; + + /** pousser une nouvelle instance avec un nouveau paramétrage sur la pile */ + static function push(?array $params=null) { + self::$stack[] = static::get(); + self::$msg = self::new($params); + } + + /** dépiler la précédente instance */ + static function pop(): IMessenger { + if (self::$stack) $msg = self::$msg = array_pop(self::$stack); + else $msg = self::$msg; + return $msg; + } static final function __callStatic($name, $args) { $name = str::us2camel($name); - foreach (static::get_msgs() as $msg) { - call_user_func_array([$msg, $name], $args); - } + call_user_func_array([static::get(), $name], $args); } ############################################################################# @@ -49,24 +70,24 @@ abstract class _messenger { const MAJOR = IMessenger::LEVEL_MAJOR; const NONE = IMessenger::LEVEL_NONE; - static function reset_params(?array $params) { foreach (static::get_msgs() as $msg) { $msg->resetParams($params); } } - function section($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->section($content, $level); } } - function title($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->title($content, $level); } } - function desc($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->desc($content, $level); } } - function action($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->action($content, $level); } } - function step($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->step($content, $level); } } - function asuccess($content=null): void { foreach (static::get_msgs() as $msg) { $msg->asuccess($content); } } - function afailure($content=null): void { foreach (static::get_msgs() as $msg) { $msg->afailure($content); } } - function adone($content=null): void { foreach (static::get_msgs() as $msg) { $msg->adone($content); } } - function print($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->print($content, $level); } } - function info($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->info($content, $level); } } - function note($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->note($content, $level); } } - function warn($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->warn($content, $level); } } - function error($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->error($content, $level); } } - function debug($content): void { self::info($content, self::DEBUG);} - function important($content): void { self::info($content, self::MAJOR);} - function attention($content): void { self::note($content, self::MAJOR);} - function critical_warn($content): void { self::warn($content, self::MAJOR);} - function critical_error($content): void { self::error($content, self::MAJOR);} - function end(bool $all=false): void { foreach (static::get_msgs() as $msg) { $msg->end($all); } } + static function reset_params(?array $params=null): void { static::get()->resetParams($params); } + static function section($content, ?int $level=null): void { static::get()->section($content, $level); } + static function title($content, ?int $level=null): void { static::get()->title($content, $level); } + static function desc($content, ?int $level=null): void { static::get()->desc($content, $level); } + static function action($content, ?int $level=null): void { static::get()->action($content, $level); } + static function step($content, ?int $level=null): void { static::get()->step($content, $level); } + static function asuccess($content=null): void { static::get()->asuccess($content); } + static function afailure($content=null): void { static::get()->afailure($content); } + static function adone($content=null): void { static::get()->adone($content); } + static function print($content, ?int $level=null): void { static::get()->print($content, $level); } + static function info($content, ?int $level=null): void { static::get()->info($content, $level); } + static function note($content, ?int $level=null): void { static::get()->note($content, $level); } + static function warn($content, ?int $level=null): void { static::get()->warn($content, $level); } + static function error($content, ?int $level=null): void { static::get()->error($content, $level); } + static function debug($content): void { self::info($content, self::DEBUG);} + static function important($content): void { self::info($content, self::MAJOR);} + static function attention($content): void { self::note($content, self::MAJOR);} + static function critwarn($content): void { self::warn($content, self::MAJOR);} + static function criterror($content): void { self::error($content, self::MAJOR);} + static function end(bool $all=false): void { static::get()->end($all); } } diff --git a/src/output/log.php b/src/output/log.php index 0bc35e0..42e7944 100644 --- a/src/output/log.php +++ b/src/output/log.php @@ -1,12 +1,18 @@ msgs = []; + foreach ($msgs as $msg) { + if ($msg !== null) $this->msgs[] = $msg; + } + } + + /** @var IMessenger[] */ + protected $msgs; + + function resetParams(?array $params=null): void { foreach ($this->msgs as $msg) { $msg->resetParams($params); } } + function clone(?array $params=null): self { + $clone = clone $this; + foreach ($clone->msgs as &$msg) { + $msg = $msg->clone($params); + }; unset($msg); + return $clone; + } + function section($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->section($content, $level); } } + function title($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->title($content, $level); } } + function desc($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->desc($content, $level); } } + function action($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->action($content, $level); } } + function step($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->step($content, $level); } } + function asuccess($content=null): void { foreach ($this->msgs as $msg) { $msg->asuccess($content); } } + function afailure($content=null): void { foreach ($this->msgs as $msg) { $msg->afailure($content); } } + function adone($content=null): void { foreach ($this->msgs as $msg) { $msg->adone($content); } } + function print($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->print($content, $level); } } + function info($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->info($content, $level); } } + function note($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->note($content, $level); } } + function warn($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->warn($content, $level); } } + function error($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->error($content, $level); } } + function end(bool $all=false): void { foreach ($this->msgs as $msg) { $msg->end($all); } } +} diff --git a/src/output/std/StdMessenger.php b/src/output/std/StdMessenger.php index 2789383..325e9b4 100644 --- a/src/output/std/StdMessenger.php +++ b/src/output/std/StdMessenger.php @@ -7,6 +7,7 @@ use nur\sery\output\IMessenger; class StdMessenger implements IMessenger { const INDENT = " "; + const DATE_FORMAT = 'Y-m-d\TH:i:s.u'; const VALID_LEVELS = [ self::LEVEL_DEBUG, @@ -92,6 +93,10 @@ class StdMessenger implements IMessenger { if ($minLevel === null) $minLevel = self::LEVEL_NORMAL; $minLevel = self::verifix_level($minLevel, self::LEVEL_NONE); + $addDate = boolval(cl::get($params, "add_date")); + $dateFormat = cl::get($params, "date_format", static::DATE_FORMAT); + $id = cl::get($params, "id"); + $params = [ "color" => $color, "indent" => $indent, @@ -104,11 +109,12 @@ class StdMessenger implements IMessenger { } $this->defaultLevel = $defaultLevel; $this->minLevel = $minLevel; + $this->addDate = $addDate; + $this->dateFormat = $dateFormat; + $this->id = $id; $this->inSection = false; $this->titles = []; - $this->title = null; $this->actions = []; - $this->action = null; } function resetParams(?array $params=null): void { @@ -125,6 +131,10 @@ class StdMessenger implements IMessenger { if ($minLevel === null) $minLevel = cl::get($params, "verbosity"); # alias if ($minLevel !== null) $minLevel = self::verifix_level($minLevel, self::LEVEL_NONE); + $addDate = cl::get($params, "add_date"); + $dateFormat = cl::get($params, "date_format"); + $id = cl::get($params, "id"); + $params = [ "output" => $output, "color" => $color, @@ -138,6 +148,19 @@ class StdMessenger implements IMessenger { } if ($defaultLevel !== null) $this->defaultLevel = $defaultLevel; if ($minLevel !== null) $this->minLevel = $minLevel; + if ($addDate !== null) $this->addDate = boolval($addDate); + if ($dateFormat !== null) $this->dateFormat = $dateFormat; + if ($id !== null) $this->id = $id; + } + + function clone(?array $params=null): IMessenger { + $clone = clone $this; + if ($params !== null) $clone->resetParams($params); + #XXX faut-il marquer la section et les titres du clone à "print" => false? + # ou en faire des références au parent? + # dans tous les cas, on considère qu'il n'y a pas d'actions en cours, et on + # ne doit pas dépiler avec end() plus que l'état que l'on a eu lors du clone + return $clone; } /** @var StdOutput la sortie standard */ @@ -152,12 +175,35 @@ class StdMessenger implements IMessenger { /** @var int level minimum que doivent avoir les messages pour être affichés */ protected $minLevel; + /** @var bool faut-il ajouter la date à chaque ligne? */ + protected $addDate; + + /** @var string format de la date */ + protected $dateFormat; + + /** @var ?string identifiant de ce messenger, à ajouter à chaque ligne */ + protected $id; + + protected function getLinePrefix(): ?string { + $linePrefix = null; + if ($this->addDate) { + $date = date_create()->format($this->dateFormat); + $linePrefix .= "$date "; + } + if ($this->id !== null) { + $linePrefix .= "$this->id "; + } + return $linePrefix; + } + protected function checkLevel(?int &$level): bool { if ($level === null) $level = $this->defaultLevel; return $level >= $this->minLevel; } - protected function _printTitle(int $level, string $type, $content, int $indentLevel, StdOutput $out): void { + protected function _printTitle(?string $linePrefix, int $level, + string $type, $content, + int $indentLevel, StdOutput $out): void { $prefixes = self::GENERIC_PREFIXES[$level][$type]; if ($prefixes[0]) $out->print(); if ($out->isColor()) { @@ -177,13 +223,16 @@ class StdMessenger implements IMessenger { $content = [$content, $len]; }; unset($content); if ($before !== null) { + if ($linePrefix !== null) $out->write($linePrefix); $out->iprint($indentLevel, $prefix, substr($before, 1), str_repeat($before[0], $maxlen), $suffix); } foreach ($lines as [$content, $len]) { + if ($linePrefix !== null) $out->write($linePrefix); $padding = $len < $maxlen? str_repeat(" ", $maxlen - $len): null; $out->iprint($indentLevel, $prefix2, $content, $padding, $suffix2); } if ($after !== null) { + if ($linePrefix !== null) $out->write($linePrefix); $out->iprint($indentLevel, $prefix, substr($after, 1), str_repeat($after[0], $maxlen), $suffix); } } else { @@ -192,13 +241,14 @@ class StdMessenger implements IMessenger { $prefix2 = str_repeat(" ", mb_strlen($prefix)); $lines = $out->getLines(false, $content); foreach ($lines as $content) { + if ($linePrefix !== null) $out->write($linePrefix); $out->iprint($indentLevel, $prefix, $content); $prefix = $prefix2; } } } - protected function _printAction(int $level, + protected function _printAction(?string $linePrefix, int $level, bool $printContent, $content, bool $printResult, ?bool $rsuccess, $rcontent, int $indentLevel, StdOutput $out): void { @@ -228,6 +278,7 @@ class StdMessenger implements IMessenger { } $lines = $out->getLines(false, $content); foreach ($lines as $content) { + if ($linePrefix !== null) $out->write($linePrefix); $out->iprint($indentLevel, $rprefix, $content); $rprefix = $rprefix2; } @@ -247,6 +298,7 @@ class StdMessenger implements IMessenger { } $lines = $out->getLines(false, $content, ":"); foreach ($lines as $content) { + if ($linePrefix !== null) $out->write($linePrefix); $out->iprint($indentLevel, $prefix, $content, $suffix); $prefix = $prefix2; } @@ -260,6 +312,7 @@ class StdMessenger implements IMessenger { $rprefix2 = " $rprefix2"; $lines = $out->getLines(false, $rcontent); foreach ($lines as $rcontent) { + if ($linePrefix !== null) $out->write($linePrefix); $out->iprint($indentLevel, $rprefix, $rcontent); $rprefix = $rprefix2; } @@ -267,6 +320,7 @@ class StdMessenger implements IMessenger { } protected function _printGeneric(int $level, string $type, $content, int $indentLevel, StdOutput $out): void { + $linePrefix = $this->getLinePrefix(); $prefixes = self::GENERIC_PREFIXES[$level][$type]; if ($out->isColor()) { $prefix = $prefixes[1]; @@ -279,6 +333,7 @@ class StdMessenger implements IMessenger { $suffix = $prefixes[2]; $lines = $out->getLines(false, $content); foreach ($lines as $content) { + if ($linePrefix !== null) $out->write($linePrefix); $out->iprint($indentLevel, $prefix, $content, $suffix); $prefix = $prefix2; } @@ -288,6 +343,7 @@ class StdMessenger implements IMessenger { $prefix2 = str_repeat(" ", mb_strlen($prefix)); $lines = $out->getLines(false, $content); foreach ($lines as $content) { + if ($linePrefix !== null) $out->write($linePrefix); $out->iprint($indentLevel, $prefix, $content); $prefix = $prefix2; } @@ -305,6 +361,7 @@ class StdMessenger implements IMessenger { $this->inSection = true; if (!$this->checkLevel($level)) return; $this->section = [ + "line_prefix" => $this->getLinePrefix(), "level" => $level, "content" => $content, "print_content" => true, @@ -314,7 +371,10 @@ class StdMessenger implements IMessenger { protected function printSection() { $section =& $this->section; if ($section["print_content"]) { - $this->_printTitle($section["level"], "section", $section["content"], 0, $this->err); + $this->_printTitle( + $section["line_prefix"], $section["level"], + "section", $section["content"], + 0, $this->err); $section["print_content"] = false; } } @@ -345,6 +405,7 @@ class StdMessenger implements IMessenger { function title($content, ?int $level=null): void { if (!$this->checkLevel($level)) return; $this->titles[] = [ + "line_prefix" => $this->getLinePrefix(), "level" => $level, "content" => $content, "print_content" => true, @@ -358,6 +419,7 @@ class StdMessenger implements IMessenger { if (!$this->checkLevel($level)) return; $title =& $this->title; $title["descs"][] = [ + "line_prefix" => $this->getLinePrefix(), "level" => $level, "content" => $content, ]; @@ -370,7 +432,10 @@ class StdMessenger implements IMessenger { $indentLevel = 0; foreach ($this->titles as &$title) { if ($title["print_content"]) { - $this->_printTitle($title["level"], "title", $title["content"], $indentLevel, $err); + $this->_printTitle( + $title["line_prefix"], $title["level"], + "title", $title["content"], + $indentLevel, $err); $title["print_content"] = false; } if ($title["print_descs"]) { @@ -403,6 +468,7 @@ class StdMessenger implements IMessenger { function action($content, ?int $level=null): void { $this->checkLevel($level); $this->actions[] = [ + "line_prefix" => $this->getLinePrefix(), "level" => $level, "content" => $content, "print_content" => true, @@ -420,15 +486,24 @@ class StdMessenger implements IMessenger { $index = 0; foreach ($this->actions as &$action) { $mergeResult = $index++ == $lastIndex && $willEnd; + $linePrefix = $action["line_prefix"]; $level = $action["level"]; $content = $action["content"]; $printContent = $action["print_content"]; $rsuccess = $action["result_success"]; $rcontent = $action["result_content"]; if ($mergeResult) { - $this->_printAction($level, $printContent, $content, true, $rsuccess, $rcontent, $indentLevel, $err); + $this->_printAction( + $linePrefix, $level, + $printContent, $content, + true, $rsuccess, $rcontent, + $indentLevel, $err); } elseif ($printContent) { - $this->_printAction($level, $printContent, $content, false, $rsuccess, $rcontent, $indentLevel, $err); + $this->_printAction( + $linePrefix, $level, + $printContent, $content, + false, $rsuccess, $rcontent, + $indentLevel, $err); $action["print_content"] = false; } $indentLevel++; diff --git a/tbin/test-console.php b/tbin/test-console.php index 5304b8c..d364f57 100755 --- a/tbin/test-console.php +++ b/tbin/test-console.php @@ -34,6 +34,13 @@ for ($i = 1; $i <= $count; $i++) { case "-M": $params["default_level"] = "major"; break; + case "-i": + $i++; + $params["id"] = $argv[$i]; + break; + case "-t": + $params["add_date"] = true; + break; } } $c = new StdMessenger($params);