modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2025-10-09 12:33:42 +04:00
parent 54de648f10
commit 0e3c53dd98
5 changed files with 245 additions and 181 deletions

View File

@ -9,6 +9,10 @@ use nulib\ExceptionShadow;
use Throwable; use Throwable;
abstract class AbstractMessenger implements _IMessenger { abstract class AbstractMessenger implements _IMessenger {
const ADD_DATE = false;
const SHOW_IDS = false;
protected static function verifix_level($level, int $max_level=self::MAX_LEVEL): int { protected static function verifix_level($level, int $max_level=self::MAX_LEVEL): int {
if (!in_array($level, self::VALID_LEVELS, true)) { if (!in_array($level, self::VALID_LEVELS, true)) {
$level = cl::get(self::LEVEL_MAP, $level, $level); $level = cl::get(self::LEVEL_MAP, $level, $level);
@ -37,17 +41,28 @@ abstract class AbstractMessenger implements _IMessenger {
/** @var string format de la date */ /** @var string format de la date */
protected string $dateFormat; protected string $dateFormat;
/** @var bool faut-il afficher les ids (p=id t=id a=id) */
protected bool $showIds;
/** @var ?string identifiant de ce messenger, à ajouter à chaque ligne */ /** @var ?string identifiant de ce messenger, à ajouter à chaque ligne */
protected ?string $id; protected ?string $id;
protected int $lastTitleId = 1;
protected int $lastActionId = 1;
protected function getLinePrefix(): ?string { protected function getLinePrefix(): ?string {
$linePrefix = null; $linePrefix = null;
if ($this->addDate) { if ($this->addDate) {
$date = date_create()->format($this->dateFormat); $date = date_create()->format($this->dateFormat);
$linePrefix .= "$date "; $linePrefix .= "$date ";
} }
if ($this->id !== null) { if ($this->showIds) {
$linePrefix .= "$this->id "; if ($this->id !== null) $linePrefix .= "p=$this->id ";
$titleId = $this->_getTitleId();
if ($titleId !== null) $linePrefix .= "t=$titleId ";
$actionId = $this->_getActionId();
if ($actionId !== null) $linePrefix .= "a=$actionId ";
} }
return $linePrefix; return $linePrefix;
} }

View File

@ -11,24 +11,23 @@ use Throwable;
class ConsoleMessenger extends AbstractMessenger { class ConsoleMessenger extends AbstractMessenger {
function __construct(?array $params=null) { function __construct(?array $params=null) {
$output = cl::get($params, "output"); $output = $params["output"] ?? null;
$color = cl::get($params, "color"); $color = $params["color"] ?? null;
$indent = cl::get($params, "indent", static::INDENT); $indent = $params["indent"] ?? static::INDENT;
$defaultLevel = cl::get($params, "default_level"); $defaultLevel = $params["default_level"] ?? null;
if ($defaultLevel === null) $defaultLevel = self::NORMAL; $defaultLevel = self::verifix_level($defaultLevel ?? self::NORMAL);
$defaultLevel = self::verifix_level($defaultLevel);
$debug = boolval(cl::get($params, "debug")); $debug = boolval($params["debug"] ?? null);
$minLevel = cl::get($params, "min_level"); $minLevel = $params["min_level"] ?? null;
if ($minLevel === null && $debug) $minLevel = self::DEBUG; if ($debug) $minLevel ??= self::DEBUG;
if ($minLevel === null) $minLevel = cl::get($params, "verbosity"); # alias $minLevel ??= $params["verbosity"] ?? null; # alias
if ($minLevel === null) $minLevel = self::NORMAL; $minLevel = self::verifix_level($minLevel ?? self::NORMAL, self::NONE);
$minLevel = self::verifix_level($minLevel, self::NONE);
$addDate = boolval(cl::get($params, "add_date")); $addDate = boolval($params["add_date"] ?? static::ADD_DATE);
$dateFormat = cl::get($params, "date_format", static::DATE_FORMAT); $dateFormat = cl::get($params, "date_format", static::DATE_FORMAT);
$id = cl::get($params, "id"); $id = $params["id"] ?? null;
$showIds = $params["show_ids"] ?? static::SHOW_IDS;
$params = [ $params = [
"color" => $color, "color" => $color,
@ -45,7 +44,9 @@ class ConsoleMessenger extends AbstractMessenger {
$this->addDate = $addDate; $this->addDate = $addDate;
$this->dateFormat = $dateFormat; $this->dateFormat = $dateFormat;
$this->id = $id; $this->id = $id;
$this->showIds = $showIds;
$this->inSection = false; $this->inSection = false;
$this->section = null;
$this->titles = []; $this->titles = [];
$this->actions = []; $this->actions = [];
} }
@ -102,13 +103,13 @@ class ConsoleMessenger extends AbstractMessenger {
} }
/** @var StdOutput la sortie d'erreur */ /** @var StdOutput la sortie d'erreur */
protected $err; protected StdOutput $err;
/** @var bool est-on dans une section? */ /** @var bool est-on dans une section? */
protected $inSection; protected bool $inSection;
/** @var array section qui est en attente d'affichage */ /** @var array section qui est en attente d'affichage */
protected $section; protected ?array $section;
function section($content, ?callable $func=null, ?int $level=null): void { function section($content, ?callable $func=null, ?int $level=null): void {
$this->_endSection(); $this->_endSection();
@ -141,22 +142,12 @@ class ConsoleMessenger extends AbstractMessenger {
} }
function _endSection(): void { function _endSection(): void {
while ($this->actions) $this->adone();
while ($this->titles) $this->_endTitle();
$this->inSection = false; $this->inSection = false;
$this->section = null; $this->section = null;
} }
/** @var array */
protected $titles;
/** @var array */
protected $title;
/** @var array */
protected $actions;
/** @var array */
protected $action;
protected function getIndentLevel(bool $withActions=true): int { protected function getIndentLevel(bool $withActions=true): int {
$indentLevel = count($this->titles) - 1; $indentLevel = count($this->titles) - 1;
if ($indentLevel < 0) $indentLevel = 0; if ($indentLevel < 0) $indentLevel = 0;
@ -169,43 +160,61 @@ class ConsoleMessenger extends AbstractMessenger {
return $indentLevel; return $indentLevel;
} }
protected array $titles;
function _getTitleMark(): int { function _getTitleMark(): int {
return count($this->titles); return count($this->titles);
} }
function _getTitleId(): ?int {
return end($this->titles)["id"] ?? null;
}
function title($content, ?callable $func=null, ?int $level=null): void { function title($content, ?callable $func=null, ?int $level=null): void {
if (!$this->checkLevel($level)) return; if (!$this->checkLevel($level)) return;
$until = $this->_getTitleMark(); $titleLevel = $this->_getTitleMark();
$this->titles[] = [ // faire en deux temps pour linePrefix soit à jour
$this->titles[] = ["id" => $this->lastTitleId++];
A::merge($this->titles[array_key_last($this->titles)], [
"title_level" => $titleLevel,
"line_prefix" => $this->getLinePrefix(), "line_prefix" => $this->getLinePrefix(),
"level" => $level, "level" => $level,
"content" => $content, "content" => $content,
"print_content" => true, "print_content" => true,
"descs" => [], "descs" => [],
"print_descs" => false, "print_descs" => false,
]; ]);
$this->title =& $this->titles[$until];
if ($func !== null) { if ($func !== null) {
try { try {
$func($this); $func($this);
} finally { } finally {
$this->_endTitle($until); $this->_endTitle($titleLevel);
} }
} }
} }
function desc($content, ?int $level=null): void { function desc($content, ?int $level=null): void {
if (!$this->checkLevel($level)) return; if (!$this->checkLevel($level)) return;
$title =& $this->title; $desc = [
$title["descs"][] = [
"line_prefix" => $this->getLinePrefix(), "line_prefix" => $this->getLinePrefix(),
"level" => $level, "level" => $level,
"content" => $content, "content" => $content,
]; ];
$title["print_descs"] = true; $key = array_key_last($this->titles);
if (array_key_exists($key, $this->titles)) {
$title =& $this->titles[$key];
$title["descs"][] = $desc;
$title["print_descs"] = true;
} else {
# pas de titre en cours
$this->_printGeneric(
$desc["line_prefix"], $desc["level"],
"desc", $desc["content"],
0, $this->err);
}
} }
protected function printTitles(): void { protected function flushTitles(): void {
$this->printSection(); $this->printSection();
$err = $this->err; $err = $this->err;
$indentLevel = 0; $indentLevel = 0;
@ -232,23 +241,27 @@ class ConsoleMessenger extends AbstractMessenger {
} }
function _endTitle(?int $until=null): void { function _endTitle(?int $until=null): void {
if ($until === null) $until = $this->_getTitleMark() - 1; $until ??= $this->_getTitleMark() - 1;
while (count($this->titles) > $until) { while (count($this->titles) > $until) {
array_pop($this->titles); array_pop($this->titles);
} }
if ($this->titles) { }
$this->title =& $this->titles[count($this->titles) - 1];
} else { protected array $actions;
$this->titles = [];
unset($this->title); function _getActionMark(): int {
} return count($this->actions);
}
function _getActionId(): ?int {
return end($this->actions)["id"] ?? null;
} }
protected function flushActions(bool $endAction=false, ?int $overrideLevel=null): void { protected function flushActions(bool $endAction=false, ?int $overrideLevel=null): void {
$this->printTitles(); $this->flushTitles();
$err = $this->err; $err = $this->err;
$indentLevel = $this->getIndentLevel(false); $indentLevel = $this->getIndentLevel(false);
$lastIndex = count($this->actions) - 1; $lastIndex = array_key_last($this->actions);
$index = 0; $index = 0;
foreach ($this->actions as &$action) { foreach ($this->actions as &$action) {
$mergeResult = $index++ == $lastIndex && $endAction; $mergeResult = $index++ == $lastIndex && $endAction;
@ -278,59 +291,62 @@ class ConsoleMessenger extends AbstractMessenger {
if ($endAction) $this->_endAction(); if ($endAction) $this->_endAction();
} }
function _getActionMark(): int {
return count($this->actions);
}
function action($content, ?callable $func=null, ?int $level=null): void { function action($content, ?callable $func=null, ?int $level=null): void {
$this->checkLevel($level); $this->checkLevel($level);
$until = $this->_getActionMark(); $actionLevel = $this->_getActionMark();
$this->actions[] = [ // faire en deux temps pour linePrefix soit à jour
$this->actions[] = ["id" => $this->lastActionId++];
A::merge($this->actions[array_key_last($this->actions)], [
"action_level" => $actionLevel,
"line_prefix" => $this->getLinePrefix(), "line_prefix" => $this->getLinePrefix(),
"level" => $level, "level" => $level,
"content" => $content, "content" => $content,
"print_content" => true, "print_content" => true,
"result_success" => null, "result_success" => null,
"result_content" => null, "result_content" => null,
]; ]);
$this->action =& $this->actions[$until];
if ($func !== null) { if ($func !== null) {
try { try {
$result = $func($this); $result = $func($this);
if ($this->_getActionMark() > $until) { if ($this->_getActionMark() > $actionLevel) {
$this->aresult($result); $this->aresult($result);
} }
} catch (Exception $e) { } catch (Exception $e) {
$this->afailure($e); $this->afailure($e);
throw $e; throw $e;
} finally { } finally {
$this->_endAction($until); $this->_endAction($actionLevel);
} }
} }
} }
function step($content, ?int $level=null): void { function step($content, ?int $level=null): void {
$this->_printGenericOrException($level, "step", $content, $this->getIndentLevel(), $this->err); $this->_printGenericOrException(
$level, "step", $content,
$this->getIndentLevel(), $this->err);
} }
function asuccess($content=null, ?int $overrideLevel=null): void { function asuccess($content=null, ?int $overrideLevel=null): void {
if (!$this->actions) $this->action(null); if (!$this->actions) $this->action(null);
$this->action["result_success"] = true; $action =& $this->actions[array_key_last($this->actions)];
$this->action["result_content"] = $content; $action["result_success"] = true;
$action["result_content"] = $content;
$this->flushActions(true, $overrideLevel); $this->flushActions(true, $overrideLevel);
} }
function afailure($content=null, ?int $overrideLevel=null): void { function afailure($content=null, ?int $overrideLevel=null): void {
if (!$this->actions) $this->action(null); if (!$this->actions) $this->action(null);
$this->action["result_success"] = false; $action =& $this->actions[array_key_last($this->actions)];
$this->action["result_content"] = $content; $action["result_success"] = false;
$action["result_content"] = $content;
$this->flushActions(true, $overrideLevel); $this->flushActions(true, $overrideLevel);
} }
function adone($content=null, ?int $overrideLevel=null): void { function adone($content=null, ?int $overrideLevel=null): void {
if (!$this->actions) $this->action(null); if (!$this->actions) $this->action(null);
$this->action["result_success"] = null; $action =& $this->actions[array_key_last($this->actions)];
$this->action["result_content"] = $content; $action["result_success"] = null;
$action["result_content"] = $content;
$this->flushActions(true, $overrideLevel); $this->flushActions(true, $overrideLevel);
} }
@ -347,45 +363,42 @@ class ConsoleMessenger extends AbstractMessenger {
while (count($this->actions) > $until) { while (count($this->actions) > $until) {
array_pop($this->actions); array_pop($this->actions);
} }
if ($this->actions) {
$this->action =& $this->actions[count($this->actions) - 1];
} else {
$this->actions = [];
unset($this->action);
}
} }
function print($content, ?int $level=null): void { function print($content, ?int $level=null): void {
$this->_printGenericOrException($level, "print", $content, $this->getIndentLevel(), $this->out); $this->_printGenericOrException(
$level, "print", $content,
$this->getIndentLevel(), $this->out);
} }
function info($content, ?int $level=null): void { function info($content, ?int $level=null): void {
$this->_printGenericOrException($level, "info", $content, $this->getIndentLevel(), $this->err); $this->_printGenericOrException(
$level, "info", $content,
$this->getIndentLevel(), $this->err);
} }
function note($content, ?int $level=null): void { function note($content, ?int $level=null): void {
$this->_printGenericOrException($level, "note", $content, $this->getIndentLevel(), $this->err); $this->_printGenericOrException(
$level, "note", $content,
$this->getIndentLevel(), $this->err);
} }
function warning($content, ?int $level=null): void { function warning($content, ?int $level=null): void {
$this->_printGenericOrException($level, "warning", $content, $this->getIndentLevel(), $this->err); $this->_printGenericOrException(
$level, "warning", $content,
$this->getIndentLevel(), $this->err);
} }
function error($content, ?int $level=null): void { function error($content, ?int $level=null): void {
$this->_printGenericOrException($level, "error", $content, $this->getIndentLevel(), $this->err); $this->_printGenericOrException(
$level, "error", $content,
$this->getIndentLevel(), $this->err);
} }
function end(bool $all=false): void { function end(bool $all=false): void {
if ($all) { if ($all) $this->_endSection();
while ($this->actions) $this->adone(); elseif ($this->actions) $this->_endAction();
while ($this->titles) $this->_endTitle(); elseif ($this->titles) $this->_endTitle();
$this->_endSection(); else $this->_endSection();
} elseif ($this->actions) {
$this->_endAction();
} elseif ($this->titles) {
$this->_endTitle();
} else {
$this->_endSection();
}
} }
} }

View File

@ -6,6 +6,10 @@ use nulib\cl;
use nulib\output\IMessenger; use nulib\output\IMessenger;
class LogMessenger extends AbstractMessenger { class LogMessenger extends AbstractMessenger {
const ADD_DATE = true;
const SHOW_IDS = true;
function __construct(?array $params=null) { function __construct(?array $params=null) {
$output = $params["output"] ?? null; $output = $params["output"] ?? null;
$color = $params["color"] ?? false; $color = $params["color"] ?? false;
@ -20,9 +24,10 @@ class LogMessenger extends AbstractMessenger {
$minLevel ??= $params["verbosity"] ?? null; # alias $minLevel ??= $params["verbosity"] ?? null; # alias
$minLevel = self::verifix_level($minLevel ?? self::NORMAL, self::NONE); $minLevel = self::verifix_level($minLevel ?? self::NORMAL, self::NONE);
$addDate = boolval($params["add_date"] ?? true); $addDate = boolval($params["add_date"] ?? static::ADD_DATE);
$dateFormat = cl::get($params, "date_format", static::DATE_FORMAT); $dateFormat = cl::get($params, "date_format", static::DATE_FORMAT);
$id = $params["id"] ?? null; $id = $params["id"] ?? null;
$showIds = $params["show_ids"] ?? static::SHOW_IDS;
$this->out = new StdOutput($output ?? STDERR, [ $this->out = new StdOutput($output ?? STDERR, [
"color" => $color, "color" => $color,
@ -33,6 +38,7 @@ class LogMessenger extends AbstractMessenger {
$this->addDate = $addDate; $this->addDate = $addDate;
$this->dateFormat = $dateFormat; $this->dateFormat = $dateFormat;
$this->id = $id; $this->id = $id;
$this->showIds = $showIds;
} }
function resetParams(?array $params=null): void { function resetParams(?array $params=null): void {
@ -91,80 +97,87 @@ class LogMessenger extends AbstractMessenger {
$this->end(true); $this->end(true);
} }
protected int $titleLevel = 0; protected array $titles = [];
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 { function _getTitleMark(): int {
return $this->titleLevel; return count($this->titles);
}
function _getTitleId(): ?int {
return end($this->titles)["id"] ?? null;
} }
function title($content, ?callable $func=null, ?int $level=null): void { function title($content, ?callable $func=null, ?int $level=null): void {
if (!$this->checkLevel($level)) return; if (!$this->checkLevel($level)) return;
$until = $this->_getTitleMark(); $titleLevel = $this->_getTitleMark();
$this->titles[] = [
"id" => $this->lastTitleId++,
"title_level" => $titleLevel,
];
$this->_printTitle( $this->_printTitle(
$this->getLinePrefix(), $level, $this->getLinePrefix(), $level,
"title", $content, "title", $content,
$this->titleLevel++, $this->out); $titleLevel, $this->out);
if ($func !== null) { if ($func !== null) {
try { try {
$func($this); $func($this);
} finally { } finally {
$this->_endTitle($until); $this->_endTitle($titleLevel);
} }
} }
} }
function desc($content, ?int $level=null): void { function desc($content, ?int $level=null): void {
if (!$this->checkLevel($level)) return; if (!$this->checkLevel($level)) return;
$titleLevel = end($this->titles)["title_level"] ?? 0;
$this->_printGeneric( $this->_printGeneric(
$this->getLinePrefix(), $level, $this->getLinePrefix(), $level,
"desc", $content, "desc", $content,
$this->titleLevel - 1, $this->out); $titleLevel, $this->out);
} }
function _endTitle(?int $until=null): void { function _endTitle(?int $until=null): void {
$until ??= $this->_getTitleMark(); $until ??= $this->_getTitleMark() - 1;
$this->titleLevel = $until; while (count($this->titles) > $until) {
array_pop($this->titles);
}
} }
protected array $actions = [];
function _getActionMark(): int { function _getActionMark(): int {
return $this->actionLevel; return count($this->actions);
}
function _getActionId(): ?int {
return end($this->actions)["id"] ?? null;
} }
function action($content, ?callable $func=null, ?int $level=null): void { function action($content, ?callable $func=null, ?int $level=null): void {
$this->checkLevel($level); $this->checkLevel($level);
$until = $this->_getActionMark(); $actionLevel = $this->_getActionMark();
$this->actions[] = ["level" => $level]; $this->actions[] = [
$this->action =& $this->actions[array_key_last($this->actions)]; "id" => $this->lastActionId++,
"action_level" => $actionLevel,
"level" => $level
];
$this->_printAction( $this->_printAction(
$this->getLinePrefix(), $level, $this->getLinePrefix(), $level,
true, $content, true, $content,
false, null, null, false, null, null,
$this->actionLevel++, $this->out); $actionLevel, $this->out);
if ($func !== null) { if ($func !== null) {
try { try {
$result = $func($this); $result = $func($this);
if ($this->_getActionMark() > $until) { if ($this->_getActionMark() > $actionLevel) {
$this->aresult($result); $this->aresult($result);
} }
} catch (Exception $e) { } catch (Exception $e) {
$this->afailure($e); $this->afailure($e);
throw $e; throw $e;
} finally { } finally {
$this->_endAction($until); $this->_endAction($actionLevel);
} }
} }
} }
@ -173,44 +186,49 @@ class LogMessenger extends AbstractMessenger {
} }
function step($content, ?int $level=null): void { function step($content, ?int $level=null): void {
$this->_printGenericOrException($level, "step", $content, $this->getIndentLevel(), $this->out); $this->_printGenericOrException(
$level, "step", $content,
$this->getIndentLevel(), $this->out);
} }
function asuccess($content=null, ?int $overrideLevel=null): void { function asuccess($content=null, ?int $overrideLevel=null): void {
if ($this->actionLevel == 0) $this->action(null); if ($this->_getActionMark() == 0) $this->action(null);
$level = $overrideLevel ?? $this->action["level"]; $action = end($this->actions);
$level = $overrideLevel ?? $action["level"];
$this->_printAction( $this->_printAction(
$this->getLinePrefix(), $level, $this->getLinePrefix(), $level,
false, null, false, null,
true, true, $content, true, true, $content,
$this->actionLevel, $this->out); $action["action_level"], $this->out);
$this->_endAction(); $this->_endAction();
} }
function afailure($content=null, ?int $overrideLevel=null): void { function afailure($content=null, ?int $overrideLevel=null): void {
if ($this->actionLevel == 0) $this->action(null); if ($this->_getActionMark() == 0) $this->action(null);
$level = $overrideLevel ?? $this->action["level"]; $action = end($this->actions);
$level = $overrideLevel ?? $action["level"];
$this->_printAction( $this->_printAction(
$this->getLinePrefix(), $level, $this->getLinePrefix(), $level,
false, null, false, null,
true, false, $content, true, false, $content,
$this->actionLevel, $this->out); $action["action_level"], $this->out);
$this->_endAction(); $this->_endAction();
} }
function adone($content=null, ?int $overrideLevel=null): void { function adone($content=null, ?int $overrideLevel=null): void {
if ($this->actionLevel == 0) $this->action(null); if ($this->_getActionMark() == 0) $this->action(null);
$level = $overrideLevel ?? $this->action["level"]; $action = end($this->actions);
$level = $overrideLevel ?? $action["level"];
$this->_printAction( $this->_printAction(
$this->getLinePrefix(), $level, $this->getLinePrefix(), $level,
false, null, false, null,
true, null, $content, true, null, $content,
$this->actionLevel, $this->out); $action["action_level"], $this->out);
$this->_endAction(); $this->_endAction();
} }
function aresult($result=null, ?int $overrideLevel=null): void { function aresult($result=null, ?int $overrideLevel=null): void {
if ($this->actionLevel == 0) $this->action(null); if ($this->_getActionMark() == 0) $this->action(null);
if ($result === true) $this->asuccess(null, $overrideLevel); if ($result === true) $this->asuccess(null, $overrideLevel);
elseif ($result === false) $this->afailure(null, $overrideLevel); elseif ($result === false) $this->afailure(null, $overrideLevel);
elseif ($result instanceof Exception) $this->afailure($result, $overrideLevel); elseif ($result instanceof Exception) $this->afailure($result, $overrideLevel);
@ -218,47 +236,56 @@ class LogMessenger extends AbstractMessenger {
} }
function _endAction(?int $until=null): void { function _endAction(?int $until=null): void {
$until ??= $this->_getActionMark(); $until ??= $this->_getActionMark() - 1;
while ($this->actionLevel > $until) { while (count($this->actions) > $until) {
$this->actionLevel--;
array_pop($this->actions); array_pop($this->actions);
} }
if ($this->actions) { }
$this->action =& $this->actions[array_key_last($this->actions)];
} else { protected function getIndentLevel(bool $withActions=true): int {
$this->actions = []; $indentLevel = count($this->titles) - 1;
unset($this->action); if ($indentLevel < 0) $indentLevel = 0;
} if ($withActions) $indentLevel += count($this->actions);
return $indentLevel;
} }
function print($content, ?int $level=null): void { function print($content, ?int $level=null): void {
$this->_printGenericOrException($level, "print", $content, $this->getIndentLevel(), $this->out); $this->_printGenericOrException(
$level, "print", $content,
$this->getIndentLevel(), $this->out);
} }
function info($content, ?int $level=null): void { function info($content, ?int $level=null): void {
$this->_printGenericOrException($level, "info", $content, $this->getIndentLevel(), $this->out); $this->_printGenericOrException(
$level, "info", $content,
$this->getIndentLevel(), $this->out);
} }
function note($content, ?int $level=null): void { function note($content, ?int $level=null): void {
$this->_printGenericOrException($level, "note", $content, $this->getIndentLevel(), $this->out); $this->_printGenericOrException(
$level, "note", $content,
$this->getIndentLevel(), $this->out);
} }
function warning($content, ?int $level=null): void { function warning($content, ?int $level=null): void {
$this->_printGenericOrException($level, "warning", $content, $this->getIndentLevel(), $this->out); $this->_printGenericOrException(
$level, "warning", $content,
$this->getIndentLevel(), $this->out);
} }
function error($content, ?int $level=null): void { function error($content, ?int $level=null): void {
$this->_printGenericOrException($level, "error", $content, $this->getIndentLevel(), $this->out); $this->_printGenericOrException(
$level, "error", $content,
$this->getIndentLevel(), $this->out);
} }
function end(bool $all=false): void { function end(bool $all=false): void {
if ($all) { if ($all) {
while ($this->actionLevel > 0) $this->adone(); while ($this->actions) $this->adone();
while ($this->titleLevel > 0) $this->_endTitle(); while ($this->titles) $this->_endTitle();
$this->_endSection(); } elseif ($this->actions) {
} elseif ($this->actionLevel > 0) {
$this->_endAction(); $this->_endAction();
} elseif ($this->titleLevel > 0) { } elseif ($this->titles) {
$this->_endTitle(); $this->_endTitle();
} }
} }

View File

@ -78,9 +78,13 @@ interface _IMessenger extends IMessenger {
function _getTitleMark(): int; function _getTitleMark(): int;
function _getTitleId(): ?int;
function _endTitle(?int $until=null): void; function _endTitle(?int $until=null): void;
function _getActionMark(): int; function _getActionMark(): int;
function _getActionId(): ?int;
function _endAction(?int $until=null): void; function _endAction(?int $until=null): void;
} }

View File

@ -2,6 +2,7 @@
<?php <?php
require(__DIR__.'/../vendor/autoload.php'); require(__DIR__.'/../vendor/autoload.php');
use nulib\output\IMessenger;
use nulib\output\std\ConsoleMessenger; use nulib\output\std\ConsoleMessenger;
use nulib\output\std\LogMessenger; use nulib\output\std\LogMessenger;
use nulib\UserException; use nulib\UserException;
@ -13,8 +14,10 @@ for ($i = 1; $i <= $count; $i++) {
switch ($argv[$i]) { switch ($argv[$i]) {
case "--console": $class = ConsoleMessenger::class; break; case "--console": $class = ConsoleMessenger::class; break;
case "--log": $class = LogMessenger::class; break; case "--log": $class = LogMessenger::class; break;
case "--id": $i++; $params["id"] = $argv[$i]; break;
case "-L": $i++; $params["output"] = $argv[$i]; break; case "-L": $i++; $params["output"] = $argv[$i]; break;
case "-i": $i++; $params["id"] = $argv[$i]; break; case "-i": $i++; $params["id"] = $argv[$i]; break;
case "--show-ids": $params["show_ids"] = true; break;
case "-t": $params["add_date"] = true; break; case "-t": $params["add_date"] = true; break;
case "-n": $params["color"] = false; break; case "-n": $params["color"] = false; break;
case "+n": $params["color"] = true; break; case "+n": $params["color"] = true; break;
@ -40,15 +43,17 @@ for ($i = 1; $i <= $count; $i++) {
} }
$msg = new $class($params); $msg = new $class($params);
###############################################################################
$msg->title("title0"); $msg->title("title0");
$msg->desc("desc0");
$msg->title("title1"); $msg->title("title1");
$msg->desc("desc1");
$msg->print("print under title1"); $msg->print("print under title1");
$msg->end(); $msg->end();
$msg->print("print under title0"); $msg->print("print under title0");
$msg->end(); $msg->end();
exit;
$msg->desc("action avec step"); $msg->desc("action avec step");
$msg->action("action avec step"); $msg->action("action avec step");
$msg->step("step"); $msg->step("step");
@ -116,44 +121,44 @@ $msg->note("note");
$msg->warning("warning"); $msg->warning("warning");
$msg->error("error"); $msg->error("error");
$msg->section("section", function ($msg) { $msg->section("section", function (IMessenger $msg) {
$msg->title("title", function ($msg) { $msg->title("title", function (IMessenger $msg) {
$msg->desc("desc"); $msg->desc("desc");
$msg->print("print"); $msg->print("print");
$msg->desc("action avec step"); $msg->desc("action avec step");
$msg->action("action avec step", function ($msg) { $msg->action("action avec step", function (IMessenger $msg) {
$msg->step("step"); $msg->step("step");
$msg->asuccess("action success"); $msg->asuccess("action success");
}); });
$msg->action("action avec step", function ($msg) { $msg->action("action avec step", function (IMessenger $msg) {
$msg->step("step"); $msg->step("step");
$msg->afailure("action failure"); $msg->afailure("action failure");
}); });
$msg->action("action avec step", function ($msg) { $msg->action("action avec step", function (IMessenger $msg) {
$msg->step("step"); $msg->step("step");
$msg->adone("action done"); $msg->adone("action done");
}); });
$msg->desc("actions sans step"); $msg->desc("actions sans step");
$msg->action("action sans step", function ($msg) { $msg->action("action sans step", function (IMessenger $msg) {
$msg->asuccess("action success"); $msg->asuccess("action success");
}); });
$msg->action("action sans step", function ($msg) { $msg->action("action sans step", function (IMessenger $msg) {
$msg->afailure("action failure"); $msg->afailure("action failure");
}); });
$msg->action("action sans step", function ($msg) { $msg->action("action sans step", function (IMessenger $msg) {
$msg->adone("action done"); $msg->adone("action done");
}); });
$msg->desc("actions imbriquées"); $msg->desc("actions imbriquées");
$msg->action("action0", function ($msg) { $msg->action("action0", function (IMessenger $msg) {
$msg->action("action1", function ($msg) { $msg->action("action1", function (IMessenger $msg) {
$msg->action("action2", function ($msg) { $msg->action("action2", function (IMessenger $msg) {
$msg->asuccess("action2 success"); $msg->asuccess("action2 success");
}); });
$msg->asuccess("action1 success"); $msg->asuccess("action1 success");
@ -162,38 +167,38 @@ $msg->section("section", function ($msg) {
}); });
$msg->desc("action avec step, sans messages"); $msg->desc("action avec step, sans messages");
$msg->action("action avec step, sans messages, success", function ($msg) { $msg->action("action avec step, sans messages, success", function (IMessenger $msg) {
$msg->step("step"); $msg->step("step");
$msg->asuccess(); $msg->asuccess();
}); });
$msg->action("action avec step, sans messages, failure", function ($msg) { $msg->action("action avec step, sans messages, failure", function (IMessenger $msg) {
$msg->step("step"); $msg->step("step");
$msg->afailure(); $msg->afailure();
}); });
$msg->action("action avec step, sans messages, done", function ($msg) { $msg->action("action avec step, sans messages, done", function (IMessenger $msg) {
$msg->step("step"); $msg->step("step");
$msg->adone(); $msg->adone();
}); });
$msg->desc("action sans step, sans messages"); $msg->desc("action sans step, sans messages");
$msg->action("action sans step, sans messages, success", function ($msg) { $msg->action("action sans step, sans messages, success", function (IMessenger $msg) {
$msg->asuccess(); $msg->asuccess();
}); });
$msg->action("action sans step, sans messages, failure", function ($msg) { $msg->action("action sans step, sans messages, failure", function (IMessenger $msg) {
$msg->afailure(); $msg->afailure();
}); });
$msg->action("action sans step, sans messages, done", function ($msg) { $msg->action("action sans step, sans messages, done", function (IMessenger $msg) {
$msg->adone(); $msg->adone();
}); });
$msg->desc("actions imbriquées, sans messages"); $msg->desc("actions imbriquées, sans messages");
$msg->action("action0", function ($msg) { $msg->action("action0", function (IMessenger $msg) {
$msg->action("action1", function ($msg) { $msg->action("action1", function (IMessenger $msg) {
$msg->action("action2", function ($msg) { $msg->action("action2", function (IMessenger $msg) {
$msg->asuccess(); $msg->asuccess();
}); });
$msg->asuccess(); $msg->asuccess();
@ -202,40 +207,40 @@ $msg->section("section", function ($msg) {
}); });
$msg->desc("action avec step, avec code de retour"); $msg->desc("action avec step, avec code de retour");
$msg->action("action avec step, avec code de retour true", function ($msg) { $msg->action("action avec step, avec code de retour true", function (IMessenger $msg) {
$msg->step("step"); $msg->step("step");
return true; return true;
}); });
$msg->action("action avec step, avec code de retour false", function ($msg) { $msg->action("action avec step, avec code de retour false", function (IMessenger $msg) {
$msg->step("step"); $msg->step("step");
return false; return false;
}); });
$msg->action("action avec step, avec code de retour autre", function ($msg) { $msg->action("action avec step, avec code de retour autre", function (IMessenger $msg) {
$msg->step("step"); $msg->step("step");
return "autre"; return "autre";
}); });
$msg->action("action avec step, avec code de retour null", function ($msg) { $msg->action("action avec step, avec code de retour null", function (IMessenger $msg) {
$msg->step("step"); $msg->step("step");
}); });
$msg->desc("action sans step, avec code de retour"); $msg->desc("action sans step, avec code de retour");
$msg->action("action sans step, avec code de retour true", function ($msg) { $msg->action("action sans step, avec code de retour true", function (IMessenger $msg) {
return true; return true;
}); });
$msg->action("action sans step, avec code de retour false", function ($msg) { $msg->action("action sans step, avec code de retour false", function (IMessenger $msg) {
return false; return false;
}); });
$msg->action("action sans step, avec code de retour autre", function ($msg) { $msg->action("action sans step, avec code de retour autre", function (IMessenger $msg) {
return "autre"; return "autre";
}); });
# ici, il n'y aura pas de message du tout # ici, il n'y aura pas de message du tout
$msg->action("action sans step, avec code de retour null", function ($msg) { $msg->action("action sans step, avec code de retour null", function (IMessenger $msg) {
}); });
$msg->info("info"); $msg->info("info");
@ -245,7 +250,7 @@ $msg->section("section", function ($msg) {
}); });
}); });
$msg->section("multi-line\nsection", function ($msg) { $msg->section("multi-line\nsection", function (IMessenger $msg) {
$msg->title("multi-line\ntitle"); $msg->title("multi-line\ntitle");
$msg->title("another\ntitle"); $msg->title("another\ntitle");
@ -267,16 +272,16 @@ $msg->section("multi-line\nsection", function ($msg) {
$msg->end(); $msg->end();
}); });
$msg->section("Exceptions", function ($msg) { $msg->section("Exceptions", function (IMessenger $msg) {
$e = new Exception("message"); $e = new Exception("message");
$u1 = new UserException("userMessage"); $u1 = new UserException("userMessage");
$u2 = (new UserException("userMessage"))->setTechMessage("techMessage"); $u2 = (new UserException("userMessage"))->setTechMessage("techMessage");
$msg->title("avec message", function ($msg) use ($e, $u1, $u2) { $msg->title("avec message", function (IMessenger $msg) use ($e, $u1, $u2) {
$msg->info(["exception", $e]); $msg->info(["exception", $e]);
$msg->info(["userException1", $u1]); $msg->info(["userException1", $u1]);
$msg->info(["userException2", $u2]); $msg->info(["userException2", $u2]);
}); });
$msg->title("sans message", function ($msg) use ($e, $u1, $u2) { $msg->title("sans message", function (IMessenger $msg) use ($e, $u1, $u2) {
$msg->info($e); $msg->info($e);
$msg->info($u1); $msg->info($u1);
$msg->info($u2); $msg->info($u2);