From 54de648f106944a222401b9b70b495cefdc11717 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Wed, 8 Oct 2025 21:06:14 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- php/src/UserException.php | 2 +- php/src/app/cli/Application.php | 8 +- php/src/output/log.php | 4 +- ...{_TMessenger.php => AbstractMessenger.php} | 43 ++++--- ...{StdMessenger.php => ConsoleMessenger.php} | 116 ++++++++---------- php/src/output/std/JsonLogMessenger.php | 5 - php/src/output/std/JsonMessenger.php | 5 + .../{StdLogMessenger.php => LogMessenger.php} | 74 ++++++----- php/src/output/std/StdOutput.php | 2 +- php/tbin/test-mysql.php | 4 +- php/tbin/test-output-forever.php | 4 +- .../{test-console.php => test-output.php} | 12 +- php/tests/cache/_TestCase.php | 4 +- php/tests/db/sqlite/ChannelMigrationTest.php | 4 +- 14 files changed, 149 insertions(+), 138 deletions(-) rename php/src/output/std/{_TMessenger.php => AbstractMessenger.php} (90%) rename php/src/output/std/{StdMessenger.php => ConsoleMessenger.php} (93%) delete mode 100644 php/src/output/std/JsonLogMessenger.php create mode 100644 php/src/output/std/JsonMessenger.php rename php/src/output/std/{StdLogMessenger.php => LogMessenger.php} (84%) rename php/tbin/{test-console.php => test-output.php} (95%) diff --git a/php/src/UserException.php b/php/src/UserException.php index 7578093..e41dc2c 100644 --- a/php/src/UserException.php +++ b/php/src/UserException.php @@ -28,7 +28,7 @@ class UserException extends RuntimeException { } function setTechMessage($techMessage): self { - $techMessage ??= c::resolve($techMessage); + if ($techMessage !== null) $techMessage = c::resolve($techMessage); $this->techMessage = $techMessage; return $this; } diff --git a/php/src/app/cli/Application.php b/php/src/app/cli/Application.php index 02f0901..2f2e29d 100644 --- a/php/src/app/cli/Application.php +++ b/php/src/app/cli/Application.php @@ -13,7 +13,7 @@ use nulib\ext\yaml; use nulib\output\console; use nulib\output\log; use nulib\output\msg; -use nulib\output\std\StdMessenger; +use nulib\output\std\ConsoleMessenger; use nulib\ref\ref_profiles; /** @@ -192,7 +192,7 @@ EOT); protected static function _initialize_app(): void { app::init(static::class); app::set_fact(app::FACT_CLI_APP); - msg::set_messenger(new StdMessenger([ + msg::set_messenger(new ConsoleMessenger([ "min_level" => msg::DEBUG, ])); } @@ -201,11 +201,11 @@ EOT); config::configure(config::CONFIGURE_INITIAL_ONLY); $msgs = null; - $msgs["console"] = new StdMessenger([ + $msgs["console"] = new ConsoleMessenger([ "min_level" => msg::NORMAL, ]); if (static::USE_LOGFILE) { - $msgs["log"] = new StdMessenger([ + $msgs["log"] = new ConsoleMessenger([ "output" => app::get()->getLogfile(), "min_level" => msg::MINOR, "add_date" => true, diff --git a/php/src/output/log.php b/php/src/output/log.php index 2e1d3ec..1703367 100644 --- a/php/src/output/log.php +++ b/php/src/output/log.php @@ -2,7 +2,7 @@ namespace nulib\output; use nulib\output\std\ProxyMessenger; -use nulib\output\std\StdMessenger; +use nulib\output\std\ConsoleMessenger; /** * Class log: inscrire un message dans les logs uniquement @@ -30,7 +30,7 @@ class log extends _messenger { static function set_output(string $logfile): void { self::create_or_reset_params([ "output" => $logfile, - ], StdMessenger::class, [ + ], ConsoleMessenger::class, [ "add_date" => true, "min_level" => self::MINOR, ]); diff --git a/php/src/output/std/_TMessenger.php b/php/src/output/std/AbstractMessenger.php similarity index 90% rename from php/src/output/std/_TMessenger.php rename to php/src/output/std/AbstractMessenger.php index 4e98a6c..6a75f37 100644 --- a/php/src/output/std/_TMessenger.php +++ b/php/src/output/std/AbstractMessenger.php @@ -8,7 +8,7 @@ use nulib\exceptions; use nulib\ExceptionShadow; use Throwable; -trait _TMessenger { +abstract class AbstractMessenger implements _IMessenger { protected static function verifix_level($level, int $max_level=self::MAX_LEVEL): int { if (!in_array($level, self::VALID_LEVELS, true)) { $level = cl::get(self::LEVEL_MAP, $level, $level); @@ -22,6 +22,24 @@ trait _TMessenger { return $level; } + /** @var StdOutput la sortie standard */ + protected StdOutput $out; + + /** @var int level par défaut dans lequel les messages sont affichés */ + protected int $defaultLevel; + + /** @var int level minimum que doivent avoir les messages pour être affichés */ + protected int $minLevel; + + /** @var bool faut-il ajouter la date à chaque ligne? */ + protected bool $addDate; + + /** @var string format de la date */ + protected string $dateFormat; + + /** @var ?string identifiant de ce messenger, à ajouter à chaque ligne */ + protected ?string $id; + protected function getLinePrefix(): ?string { $linePrefix = null; if ($this->addDate) { @@ -46,18 +64,6 @@ trait _TMessenger { return $level >= $this->minLevel; } - protected function getIndentLevel(bool $withActions=true): int { - $indentLevel = count($this->titles) - 1; - if ($indentLevel < 0) $indentLevel = 0; - if ($withActions) { - foreach ($this->actions as $action) { - if ($action["level"] < $this->minLevel) continue; - $indentLevel++; - } - } - return $indentLevel; - } - protected function _printTitle( ?string $linePrefix, int $level, string $type, $content, @@ -108,6 +114,8 @@ trait _TMessenger { } } + protected abstract function flushActions(bool $endAction=false, ?int $overrideLevel=null): void; + protected function _printAction( ?string $linePrefix, int $level, bool $printContent, $content, @@ -243,10 +251,10 @@ trait _TMessenger { $content = null; } - $printActions = true; + $flushActions = true; $showContent = $this->checkLevel($level); if ($content !== null && $showContent) { - $this->printActions(); $printActions = false; + $this->flushActions(); $flushActions = false; $this->_printGeneric($linePrefix, $level, $type, $content, $indentLevel, $out); } if ($exceptions !== null) { @@ -256,12 +264,12 @@ trait _TMessenger { # tout d'abord message $message = exceptions::get_message($exception); if ($showContent) { - if ($printActions) { $this->printActions(); $printActions = false; } + if ($flushActions) { $this->flushActions(); $flushActions = false; } $this->_printGeneric($linePrefix, $level, $type, $message, $indentLevel, $out); } # puis summary et traceback if ($showTraceback) { - if ($printActions) { $this->printActions(); $printActions = false; } + if ($flushActions) { $this->flushActions(); $flushActions = false; } $summary = exceptions::get_summary($exception, false); $this->_printGeneric($linePrefix, $level1, $type, $summary, $indentLevel, $out); $traceback = exceptions::get_traceback($exception); @@ -270,5 +278,4 @@ trait _TMessenger { } } } - } diff --git a/php/src/output/std/StdMessenger.php b/php/src/output/std/ConsoleMessenger.php similarity index 93% rename from php/src/output/std/StdMessenger.php rename to php/src/output/std/ConsoleMessenger.php index 00d35a1..7652615 100644 --- a/php/src/output/std/StdMessenger.php +++ b/php/src/output/std/ConsoleMessenger.php @@ -9,9 +9,7 @@ use nulib\ExceptionShadow; use nulib\output\IMessenger; use Throwable; -class StdMessenger implements _IMessenger { - use _TMessenger; - +class ConsoleMessenger extends AbstractMessenger { function __construct(?array $params=null) { $output = cl::get($params, "output"); $color = cl::get($params, "color"); @@ -103,27 +101,9 @@ class StdMessenger implements _IMessenger { return $clone; } - /** @var StdOutput la sortie standard */ - protected $out; - /** @var StdOutput la sortie d'erreur */ protected $err; - /** @var int level par défaut dans lequel les messages sont affichés */ - protected $defaultLevel; - - /** @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; - /** @var bool est-on dans une section? */ protected $inSection; @@ -171,6 +151,24 @@ class StdMessenger implements _IMessenger { /** @var array */ protected $title; + /** @var array */ + protected $actions; + + /** @var array */ + protected $action; + + protected function getIndentLevel(bool $withActions=true): int { + $indentLevel = count($this->titles) - 1; + if ($indentLevel < 0) $indentLevel = 0; + if ($withActions) { + foreach ($this->actions as $action) { + if ($action["level"] < $this->minLevel) continue; + $indentLevel++; + } + } + return $indentLevel; + } + function _getTitleMark(): int { return count($this->titles); } @@ -246,44 +244,7 @@ class StdMessenger implements _IMessenger { } } - /** @var array */ - protected $actions; - - /** @var array */ - protected $action; - - function _getActionMark(): int { - return count($this->actions); - } - - function action($content, ?callable $func=null, ?int $level=null): void { - $this->checkLevel($level); - $until = $this->_getActionMark(); - $this->actions[] = [ - "line_prefix" => $this->getLinePrefix(), - "level" => $level, - "content" => $content, - "print_content" => true, - "result_success" => null, - "result_content" => null, - ]; - $this->action =& $this->actions[$until]; - if ($func !== null) { - try { - $result = $func($this); - if ($this->_getActionMark() > $until) { - $this->aresult($result); - } - } catch (Exception $e) { - $this->afailure($e); - throw $e; - } finally { - $this->_endAction($until); - } - } - } - - function printActions(bool $endAction=false, ?int $overrideLevel=null): void { + protected function flushActions(bool $endAction=false, ?int $overrideLevel=null): void { $this->printTitles(); $err = $this->err; $indentLevel = $this->getIndentLevel(false); @@ -317,6 +278,37 @@ class StdMessenger implements _IMessenger { if ($endAction) $this->_endAction(); } + function _getActionMark(): int { + return count($this->actions); + } + + function action($content, ?callable $func=null, ?int $level=null): void { + $this->checkLevel($level); + $until = $this->_getActionMark(); + $this->actions[] = [ + "line_prefix" => $this->getLinePrefix(), + "level" => $level, + "content" => $content, + "print_content" => true, + "result_success" => null, + "result_content" => null, + ]; + $this->action =& $this->actions[$until]; + if ($func !== null) { + try { + $result = $func($this); + if ($this->_getActionMark() > $until) { + $this->aresult($result); + } + } catch (Exception $e) { + $this->afailure($e); + throw $e; + } finally { + $this->_endAction($until); + } + } + } + function step($content, ?int $level=null): void { $this->_printGenericOrException($level, "step", $content, $this->getIndentLevel(), $this->err); } @@ -325,21 +317,21 @@ class StdMessenger implements _IMessenger { if (!$this->actions) $this->action(null); $this->action["result_success"] = true; $this->action["result_content"] = $content; - $this->printActions(true, $overrideLevel); + $this->flushActions(true, $overrideLevel); } function afailure($content=null, ?int $overrideLevel=null): void { if (!$this->actions) $this->action(null); $this->action["result_success"] = false; $this->action["result_content"] = $content; - $this->printActions(true, $overrideLevel); + $this->flushActions(true, $overrideLevel); } function adone($content=null, ?int $overrideLevel=null): void { if (!$this->actions) $this->action(null); $this->action["result_success"] = null; $this->action["result_content"] = $content; - $this->printActions(true, $overrideLevel); + $this->flushActions(true, $overrideLevel); } function aresult($result=null, ?int $overrideLevel=null): void { diff --git a/php/src/output/std/JsonLogMessenger.php b/php/src/output/std/JsonLogMessenger.php deleted file mode 100644 index f5d5dcf..0000000 --- a/php/src/output/std/JsonLogMessenger.php +++ /dev/null @@ -1,5 +0,0 @@ -_endSection(); if (!$this->checkLevel($level)) return; @@ -113,7 +92,20 @@ class StdLogMessenger implements _IMessenger { } protected int $titleLevel = 0; - + + protected int $actionLevel = 0; + + protected array $actions = []; + + protected ?array $action; + + protected function getIndentLevel(bool $withActions=true): int { + $indentLevel = $this->titleLevel - 1; + if ($indentLevel < 0) $indentLevel = 0; + if ($withActions) $indentLevel += $this->actionLevel; + return $indentLevel; + } + function _getTitleMark(): int { return $this->titleLevel; } @@ -135,6 +127,7 @@ class StdLogMessenger implements _IMessenger { } function desc($content, ?int $level=null): void { + if (!$this->checkLevel($level)) return; $this->_printGeneric( $this->getLinePrefix(), $level, "desc", $content, @@ -147,10 +140,6 @@ class StdLogMessenger implements _IMessenger { $this->titleLevel = $until; } - protected int $actionLevel = 0; - - protected array $actionLevels = []; - function _getActionMark(): int { return $this->actionLevel; } @@ -158,6 +147,8 @@ class StdLogMessenger implements _IMessenger { function action($content, ?callable $func=null, ?int $level=null): void { $this->checkLevel($level); $until = $this->_getActionMark(); + $this->actions[] = ["level" => $level]; + $this->action =& $this->actions[array_key_last($this->actions)]; $this->_printAction( $this->getLinePrefix(), $level, true, $content, @@ -178,12 +169,16 @@ class StdLogMessenger implements _IMessenger { } } + protected function flushActions(bool $endAction=false, ?int $overrideLevel=null): void { + } + function step($content, ?int $level=null): void { $this->_printGenericOrException($level, "step", $content, $this->getIndentLevel(), $this->out); } function asuccess($content=null, ?int $overrideLevel=null): void { if ($this->actionLevel == 0) $this->action(null); + $level = $overrideLevel ?? $this->action["level"]; $this->_printAction( $this->getLinePrefix(), $level, false, null, @@ -194,6 +189,7 @@ class StdLogMessenger implements _IMessenger { function afailure($content=null, ?int $overrideLevel=null): void { if ($this->actionLevel == 0) $this->action(null); + $level = $overrideLevel ?? $this->action["level"]; $this->_printAction( $this->getLinePrefix(), $level, false, null, @@ -204,6 +200,7 @@ class StdLogMessenger implements _IMessenger { function adone($content=null, ?int $overrideLevel=null): void { if ($this->actionLevel == 0) $this->action(null); + $level = $overrideLevel ?? $this->action["level"]; $this->_printAction( $this->getLinePrefix(), $level, false, null, @@ -222,7 +219,16 @@ class StdLogMessenger implements _IMessenger { function _endAction(?int $until=null): void { $until ??= $this->_getActionMark(); - $this->actionLevel = $until; + while ($this->actionLevel > $until) { + $this->actionLevel--; + array_pop($this->actions); + } + if ($this->actions) { + $this->action =& $this->actions[array_key_last($this->actions)]; + } else { + $this->actions = []; + unset($this->action); + } } function print($content, ?int $level=null): void { @@ -230,19 +236,19 @@ class StdLogMessenger implements _IMessenger { } function info($content, ?int $level=null): void { - $this->_printGenericOrException($level, "info", $content, $this->getIndentLevel(), $this->err); + $this->_printGenericOrException($level, "info", $content, $this->getIndentLevel(), $this->out); } function note($content, ?int $level=null): void { - $this->_printGenericOrException($level, "note", $content, $this->getIndentLevel(), $this->err); + $this->_printGenericOrException($level, "note", $content, $this->getIndentLevel(), $this->out); } function warning($content, ?int $level=null): void { - $this->_printGenericOrException($level, "warning", $content, $this->getIndentLevel(), $this->err); + $this->_printGenericOrException($level, "warning", $content, $this->getIndentLevel(), $this->out); } function error($content, ?int $level=null): void { - $this->_printGenericOrException($level, "error", $content, $this->getIndentLevel(), $this->err); + $this->_printGenericOrException($level, "error", $content, $this->getIndentLevel(), $this->out); } function end(bool $all=false): void { diff --git a/php/src/output/std/StdOutput.php b/php/src/output/std/StdOutput.php index 4e6f223..945b9b3 100644 --- a/php/src/output/std/StdOutput.php +++ b/php/src/output/std/StdOutput.php @@ -124,7 +124,7 @@ class StdOutput { protected $outf; /** @var bool faut-il masquer les erreurs d'écriture? */ - protected bool $maskErrors; + protected ?bool $maskErrors; /** @var bool faut-il autoriser la sortie en couleur? */ protected bool $color = false; diff --git a/php/tbin/test-mysql.php b/php/tbin/test-mysql.php index e2eb555..043d924 100644 --- a/php/tbin/test-mysql.php +++ b/php/tbin/test-mysql.php @@ -7,9 +7,9 @@ use nulib\db\CapacitorChannel; use nulib\db\mysql\Mysql; use nulib\db\mysql\MysqlStorage; use nulib\output\msg; -use nulib\output\std\StdMessenger; +use nulib\output\std\ConsoleMessenger; -msg::set_messenger_class(StdMessenger::class); +msg::set_messenger_class(ConsoleMessenger::class); $db = new Mysql([ "type" => "mysql", diff --git a/php/tbin/test-output-forever.php b/php/tbin/test-output-forever.php index 42d014d..6bee7b2 100755 --- a/php/tbin/test-output-forever.php +++ b/php/tbin/test-output-forever.php @@ -3,10 +3,10 @@ require(__DIR__.'/../vendor/autoload.php'); use nulib\UserException; -use nulib\output\std\StdMessenger; +use nulib\output\std\ConsoleMessenger; use nulib\output\msg; -msg::set_messenger(new StdMessenger(), new StdMessenger([ +msg::set_messenger(new ConsoleMessenger(), new ConsoleMessenger([ "output" => "output-forever.log", ])); diff --git a/php/tbin/test-console.php b/php/tbin/test-output.php similarity index 95% rename from php/tbin/test-console.php rename to php/tbin/test-output.php index bc50074..1974482 100755 --- a/php/tbin/test-console.php +++ b/php/tbin/test-output.php @@ -2,13 +2,17 @@ title("title0"); $msg->title("title1"); @@ -43,6 +47,8 @@ $msg->end(); $msg->print("print under title0"); $msg->end(); +exit; + $msg->desc("action avec step"); $msg->action("action avec step"); $msg->step("step"); @@ -264,7 +270,7 @@ $msg->section("multi-line\nsection", function ($msg) { $msg->section("Exceptions", function ($msg) { $e = new Exception("message"); $u1 = new UserException("userMessage"); - $u2 = new UserException("userMessage", "techMessage"); + $u2 = (new UserException("userMessage"))->setTechMessage("techMessage"); $msg->title("avec message", function ($msg) use ($e, $u1, $u2) { $msg->info(["exception", $e]); $msg->info(["userException1", $u1]); diff --git a/php/tests/cache/_TestCase.php b/php/tests/cache/_TestCase.php index f05875c..a8bfa11 100644 --- a/php/tests/cache/_TestCase.php +++ b/php/tests/cache/_TestCase.php @@ -3,7 +3,7 @@ namespace nulib\cache; use nulib\db\sqlite\SqliteStorage; use nulib\output\msg; -use nulib\output\std\StdMessenger; +use nulib\output\std\ConsoleMessenger; use nulib\tests\TestCase; class _TestCase extends TestCase { @@ -11,7 +11,7 @@ class _TestCase extends TestCase { static function setUpBeforeClass(): void { parent::setUpBeforeClass(); - msg::set_messenger_class(StdMessenger::class); + msg::set_messenger_class(ConsoleMessenger::class); self::$storage = new SqliteStorage(__DIR__."/cache.db"); cache::set_storage(self::$storage); } diff --git a/php/tests/db/sqlite/ChannelMigrationTest.php b/php/tests/db/sqlite/ChannelMigrationTest.php index fa48e7c..30a80f4 100644 --- a/php/tests/db/sqlite/ChannelMigrationTest.php +++ b/php/tests/db/sqlite/ChannelMigrationTest.php @@ -7,14 +7,14 @@ use nulib\db\sqlite\impl\MyChannelV2; use nulib\db\sqlite\impl\MyChannelV3; use nulib\db\sqlite\impl\MyIndexChannel; use nulib\output\msg; -use nulib\output\std\StdMessenger; +use nulib\output\std\ConsoleMessenger; use nulib\php\time\DateTime; use nulib\tests\TestCase; class ChannelMigrationTest extends TestCase { static function setUpBeforeClass(): void { parent::setUpBeforeClass(); - msg::set_messenger_class(StdMessenger::class); + msg::set_messenger_class(ConsoleMessenger::class); } protected function addData(MyChannel $channel, array $data): void {