modifs.mineures sans commentaires
This commit is contained in:
parent
54de648f10
commit
0e3c53dd98
@ -9,6 +9,10 @@ use nulib\ExceptionShadow;
|
||||
use Throwable;
|
||||
|
||||
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 {
|
||||
if (!in_array($level, self::VALID_LEVELS, true)) {
|
||||
$level = cl::get(self::LEVEL_MAP, $level, $level);
|
||||
@ -37,17 +41,28 @@ abstract class AbstractMessenger implements _IMessenger {
|
||||
/** @var string format de la date */
|
||||
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 */
|
||||
protected ?string $id;
|
||||
|
||||
protected int $lastTitleId = 1;
|
||||
|
||||
protected int $lastActionId = 1;
|
||||
|
||||
protected function getLinePrefix(): ?string {
|
||||
$linePrefix = null;
|
||||
if ($this->addDate) {
|
||||
$date = date_create()->format($this->dateFormat);
|
||||
$linePrefix .= "$date ";
|
||||
}
|
||||
if ($this->id !== null) {
|
||||
$linePrefix .= "$this->id ";
|
||||
if ($this->showIds) {
|
||||
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;
|
||||
}
|
||||
|
@ -11,24 +11,23 @@ use Throwable;
|
||||
|
||||
class ConsoleMessenger extends AbstractMessenger {
|
||||
function __construct(?array $params=null) {
|
||||
$output = cl::get($params, "output");
|
||||
$color = cl::get($params, "color");
|
||||
$indent = cl::get($params, "indent", static::INDENT);
|
||||
$output = $params["output"] ?? null;
|
||||
$color = $params["color"] ?? null;
|
||||
$indent = $params["indent"] ?? static::INDENT;
|
||||
|
||||
$defaultLevel = cl::get($params, "default_level");
|
||||
if ($defaultLevel === null) $defaultLevel = self::NORMAL;
|
||||
$defaultLevel = self::verifix_level($defaultLevel);
|
||||
$defaultLevel = $params["default_level"] ?? null;
|
||||
$defaultLevel = self::verifix_level($defaultLevel ?? self::NORMAL);
|
||||
|
||||
$debug = boolval(cl::get($params, "debug"));
|
||||
$minLevel = cl::get($params, "min_level");
|
||||
if ($minLevel === null && $debug) $minLevel = self::DEBUG;
|
||||
if ($minLevel === null) $minLevel = cl::get($params, "verbosity"); # alias
|
||||
if ($minLevel === null) $minLevel = self::NORMAL;
|
||||
$minLevel = self::verifix_level($minLevel, self::NONE);
|
||||
$debug = boolval($params["debug"] ?? null);
|
||||
$minLevel = $params["min_level"] ?? null;
|
||||
if ($debug) $minLevel ??= self::DEBUG;
|
||||
$minLevel ??= $params["verbosity"] ?? null; # alias
|
||||
$minLevel = self::verifix_level($minLevel ?? self::NORMAL, 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);
|
||||
$id = cl::get($params, "id");
|
||||
$id = $params["id"] ?? null;
|
||||
$showIds = $params["show_ids"] ?? static::SHOW_IDS;
|
||||
|
||||
$params = [
|
||||
"color" => $color,
|
||||
@ -45,7 +44,9 @@ class ConsoleMessenger extends AbstractMessenger {
|
||||
$this->addDate = $addDate;
|
||||
$this->dateFormat = $dateFormat;
|
||||
$this->id = $id;
|
||||
$this->showIds = $showIds;
|
||||
$this->inSection = false;
|
||||
$this->section = null;
|
||||
$this->titles = [];
|
||||
$this->actions = [];
|
||||
}
|
||||
@ -102,13 +103,13 @@ class ConsoleMessenger extends AbstractMessenger {
|
||||
}
|
||||
|
||||
/** @var StdOutput la sortie d'erreur */
|
||||
protected $err;
|
||||
protected StdOutput $err;
|
||||
|
||||
/** @var bool est-on dans une section? */
|
||||
protected $inSection;
|
||||
protected bool $inSection;
|
||||
|
||||
/** @var array section qui est en attente d'affichage */
|
||||
protected $section;
|
||||
protected ?array $section;
|
||||
|
||||
function section($content, ?callable $func=null, ?int $level=null): void {
|
||||
$this->_endSection();
|
||||
@ -141,22 +142,12 @@ class ConsoleMessenger extends AbstractMessenger {
|
||||
}
|
||||
|
||||
function _endSection(): void {
|
||||
while ($this->actions) $this->adone();
|
||||
while ($this->titles) $this->_endTitle();
|
||||
$this->inSection = false;
|
||||
$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 {
|
||||
$indentLevel = count($this->titles) - 1;
|
||||
if ($indentLevel < 0) $indentLevel = 0;
|
||||
@ -169,43 +160,61 @@ class ConsoleMessenger extends AbstractMessenger {
|
||||
return $indentLevel;
|
||||
}
|
||||
|
||||
protected array $titles;
|
||||
|
||||
function _getTitleMark(): int {
|
||||
return count($this->titles);
|
||||
}
|
||||
|
||||
function _getTitleId(): ?int {
|
||||
return end($this->titles)["id"] ?? null;
|
||||
}
|
||||
|
||||
function title($content, ?callable $func=null, ?int $level=null): void {
|
||||
if (!$this->checkLevel($level)) return;
|
||||
$until = $this->_getTitleMark();
|
||||
$this->titles[] = [
|
||||
$titleLevel = $this->_getTitleMark();
|
||||
// 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(),
|
||||
"level" => $level,
|
||||
"content" => $content,
|
||||
"print_content" => true,
|
||||
"descs" => [],
|
||||
"print_descs" => false,
|
||||
];
|
||||
$this->title =& $this->titles[$until];
|
||||
]);
|
||||
if ($func !== null) {
|
||||
try {
|
||||
$func($this);
|
||||
} finally {
|
||||
$this->_endTitle($until);
|
||||
$this->_endTitle($titleLevel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function desc($content, ?int $level=null): void {
|
||||
if (!$this->checkLevel($level)) return;
|
||||
$title =& $this->title;
|
||||
$title["descs"][] = [
|
||||
$desc = [
|
||||
"line_prefix" => $this->getLinePrefix(),
|
||||
"level" => $level,
|
||||
"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();
|
||||
$err = $this->err;
|
||||
$indentLevel = 0;
|
||||
@ -232,23 +241,27 @@ class ConsoleMessenger extends AbstractMessenger {
|
||||
}
|
||||
|
||||
function _endTitle(?int $until=null): void {
|
||||
if ($until === null) $until = $this->_getTitleMark() - 1;
|
||||
$until ??= $this->_getTitleMark() - 1;
|
||||
while (count($this->titles) > $until) {
|
||||
array_pop($this->titles);
|
||||
}
|
||||
if ($this->titles) {
|
||||
$this->title =& $this->titles[count($this->titles) - 1];
|
||||
} else {
|
||||
$this->titles = [];
|
||||
unset($this->title);
|
||||
}
|
||||
}
|
||||
|
||||
protected array $actions;
|
||||
|
||||
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 {
|
||||
$this->printTitles();
|
||||
$this->flushTitles();
|
||||
$err = $this->err;
|
||||
$indentLevel = $this->getIndentLevel(false);
|
||||
$lastIndex = count($this->actions) - 1;
|
||||
$lastIndex = array_key_last($this->actions);
|
||||
$index = 0;
|
||||
foreach ($this->actions as &$action) {
|
||||
$mergeResult = $index++ == $lastIndex && $endAction;
|
||||
@ -278,59 +291,62 @@ class ConsoleMessenger extends AbstractMessenger {
|
||||
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[] = [
|
||||
$actionLevel = $this->_getActionMark();
|
||||
// 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(),
|
||||
"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) {
|
||||
if ($this->_getActionMark() > $actionLevel) {
|
||||
$this->aresult($result);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$this->afailure($e);
|
||||
throw $e;
|
||||
} finally {
|
||||
$this->_endAction($until);
|
||||
$this->_endAction($actionLevel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
if (!$this->actions) $this->action(null);
|
||||
$this->action["result_success"] = true;
|
||||
$this->action["result_content"] = $content;
|
||||
$action =& $this->actions[array_key_last($this->actions)];
|
||||
$action["result_success"] = true;
|
||||
$action["result_content"] = $content;
|
||||
$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;
|
||||
$action =& $this->actions[array_key_last($this->actions)];
|
||||
$action["result_success"] = false;
|
||||
$action["result_content"] = $content;
|
||||
$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;
|
||||
$action =& $this->actions[array_key_last($this->actions)];
|
||||
$action["result_success"] = null;
|
||||
$action["result_content"] = $content;
|
||||
$this->flushActions(true, $overrideLevel);
|
||||
}
|
||||
|
||||
@ -347,45 +363,42 @@ class ConsoleMessenger extends AbstractMessenger {
|
||||
while (count($this->actions) > $until) {
|
||||
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 {
|
||||
$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 {
|
||||
$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 {
|
||||
$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 {
|
||||
$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 {
|
||||
$this->_printGenericOrException($level, "error", $content, $this->getIndentLevel(), $this->err);
|
||||
$this->_printGenericOrException(
|
||||
$level, "error", $content,
|
||||
$this->getIndentLevel(), $this->err);
|
||||
}
|
||||
|
||||
function end(bool $all=false): void {
|
||||
if ($all) {
|
||||
while ($this->actions) $this->adone();
|
||||
while ($this->titles) $this->_endTitle();
|
||||
$this->_endSection();
|
||||
} elseif ($this->actions) {
|
||||
$this->_endAction();
|
||||
} elseif ($this->titles) {
|
||||
$this->_endTitle();
|
||||
} else {
|
||||
$this->_endSection();
|
||||
}
|
||||
if ($all) $this->_endSection();
|
||||
elseif ($this->actions) $this->_endAction();
|
||||
elseif ($this->titles) $this->_endTitle();
|
||||
else $this->_endSection();
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,10 @@ use nulib\cl;
|
||||
use nulib\output\IMessenger;
|
||||
|
||||
class LogMessenger extends AbstractMessenger {
|
||||
const ADD_DATE = true;
|
||||
|
||||
const SHOW_IDS = true;
|
||||
|
||||
function __construct(?array $params=null) {
|
||||
$output = $params["output"] ?? null;
|
||||
$color = $params["color"] ?? false;
|
||||
@ -20,9 +24,10 @@ class LogMessenger extends AbstractMessenger {
|
||||
$minLevel ??= $params["verbosity"] ?? null; # alias
|
||||
$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);
|
||||
$id = $params["id"] ?? null;
|
||||
$showIds = $params["show_ids"] ?? static::SHOW_IDS;
|
||||
|
||||
$this->out = new StdOutput($output ?? STDERR, [
|
||||
"color" => $color,
|
||||
@ -33,6 +38,7 @@ class LogMessenger extends AbstractMessenger {
|
||||
$this->addDate = $addDate;
|
||||
$this->dateFormat = $dateFormat;
|
||||
$this->id = $id;
|
||||
$this->showIds = $showIds;
|
||||
}
|
||||
|
||||
function resetParams(?array $params=null): void {
|
||||
@ -91,80 +97,87 @@ class LogMessenger extends AbstractMessenger {
|
||||
$this->end(true);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
protected array $titles = [];
|
||||
|
||||
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 {
|
||||
if (!$this->checkLevel($level)) return;
|
||||
$until = $this->_getTitleMark();
|
||||
$titleLevel = $this->_getTitleMark();
|
||||
$this->titles[] = [
|
||||
"id" => $this->lastTitleId++,
|
||||
"title_level" => $titleLevel,
|
||||
];
|
||||
$this->_printTitle(
|
||||
$this->getLinePrefix(), $level,
|
||||
"title", $content,
|
||||
$this->titleLevel++, $this->out);
|
||||
$titleLevel, $this->out);
|
||||
if ($func !== null) {
|
||||
try {
|
||||
$func($this);
|
||||
} finally {
|
||||
$this->_endTitle($until);
|
||||
$this->_endTitle($titleLevel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function desc($content, ?int $level=null): void {
|
||||
if (!$this->checkLevel($level)) return;
|
||||
$titleLevel = end($this->titles)["title_level"] ?? 0;
|
||||
$this->_printGeneric(
|
||||
$this->getLinePrefix(), $level,
|
||||
"desc", $content,
|
||||
$this->titleLevel - 1, $this->out);
|
||||
$titleLevel, $this->out);
|
||||
|
||||
}
|
||||
|
||||
function _endTitle(?int $until=null): void {
|
||||
$until ??= $this->_getTitleMark();
|
||||
$this->titleLevel = $until;
|
||||
$until ??= $this->_getTitleMark() - 1;
|
||||
while (count($this->titles) > $until) {
|
||||
array_pop($this->titles);
|
||||
}
|
||||
}
|
||||
|
||||
protected array $actions = [];
|
||||
|
||||
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 {
|
||||
$this->checkLevel($level);
|
||||
$until = $this->_getActionMark();
|
||||
$this->actions[] = ["level" => $level];
|
||||
$this->action =& $this->actions[array_key_last($this->actions)];
|
||||
$actionLevel = $this->_getActionMark();
|
||||
$this->actions[] = [
|
||||
"id" => $this->lastActionId++,
|
||||
"action_level" => $actionLevel,
|
||||
"level" => $level
|
||||
];
|
||||
$this->_printAction(
|
||||
$this->getLinePrefix(), $level,
|
||||
true, $content,
|
||||
false, null, null,
|
||||
$this->actionLevel++, $this->out);
|
||||
$actionLevel, $this->out);
|
||||
if ($func !== null) {
|
||||
try {
|
||||
$result = $func($this);
|
||||
if ($this->_getActionMark() > $until) {
|
||||
if ($this->_getActionMark() > $actionLevel) {
|
||||
$this->aresult($result);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$this->afailure($e);
|
||||
throw $e;
|
||||
} finally {
|
||||
$this->_endAction($until);
|
||||
$this->_endAction($actionLevel);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -173,44 +186,49 @@ class LogMessenger extends AbstractMessenger {
|
||||
}
|
||||
|
||||
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 {
|
||||
if ($this->actionLevel == 0) $this->action(null);
|
||||
$level = $overrideLevel ?? $this->action["level"];
|
||||
if ($this->_getActionMark() == 0) $this->action(null);
|
||||
$action = end($this->actions);
|
||||
$level = $overrideLevel ?? $action["level"];
|
||||
$this->_printAction(
|
||||
$this->getLinePrefix(), $level,
|
||||
false, null,
|
||||
true, true, $content,
|
||||
$this->actionLevel, $this->out);
|
||||
$action["action_level"], $this->out);
|
||||
$this->_endAction();
|
||||
}
|
||||
|
||||
function afailure($content=null, ?int $overrideLevel=null): void {
|
||||
if ($this->actionLevel == 0) $this->action(null);
|
||||
$level = $overrideLevel ?? $this->action["level"];
|
||||
if ($this->_getActionMark() == 0) $this->action(null);
|
||||
$action = end($this->actions);
|
||||
$level = $overrideLevel ?? $action["level"];
|
||||
$this->_printAction(
|
||||
$this->getLinePrefix(), $level,
|
||||
false, null,
|
||||
true, false, $content,
|
||||
$this->actionLevel, $this->out);
|
||||
$action["action_level"], $this->out);
|
||||
$this->_endAction();
|
||||
}
|
||||
|
||||
function adone($content=null, ?int $overrideLevel=null): void {
|
||||
if ($this->actionLevel == 0) $this->action(null);
|
||||
$level = $overrideLevel ?? $this->action["level"];
|
||||
if ($this->_getActionMark() == 0) $this->action(null);
|
||||
$action = end($this->actions);
|
||||
$level = $overrideLevel ?? $action["level"];
|
||||
$this->_printAction(
|
||||
$this->getLinePrefix(), $level,
|
||||
false, null,
|
||||
true, null, $content,
|
||||
$this->actionLevel, $this->out);
|
||||
$action["action_level"], $this->out);
|
||||
$this->_endAction();
|
||||
}
|
||||
|
||||
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);
|
||||
elseif ($result === false) $this->afailure(null, $overrideLevel);
|
||||
elseif ($result instanceof Exception) $this->afailure($result, $overrideLevel);
|
||||
@ -218,47 +236,56 @@ class LogMessenger extends AbstractMessenger {
|
||||
}
|
||||
|
||||
function _endAction(?int $until=null): void {
|
||||
$until ??= $this->_getActionMark();
|
||||
while ($this->actionLevel > $until) {
|
||||
$this->actionLevel--;
|
||||
$until ??= $this->_getActionMark() - 1;
|
||||
while (count($this->actions) > $until) {
|
||||
array_pop($this->actions);
|
||||
}
|
||||
if ($this->actions) {
|
||||
$this->action =& $this->actions[array_key_last($this->actions)];
|
||||
} else {
|
||||
$this->actions = [];
|
||||
unset($this->action);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getIndentLevel(bool $withActions=true): int {
|
||||
$indentLevel = count($this->titles) - 1;
|
||||
if ($indentLevel < 0) $indentLevel = 0;
|
||||
if ($withActions) $indentLevel += count($this->actions);
|
||||
return $indentLevel;
|
||||
}
|
||||
|
||||
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 {
|
||||
$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 {
|
||||
$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 {
|
||||
$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 {
|
||||
$this->_printGenericOrException($level, "error", $content, $this->getIndentLevel(), $this->out);
|
||||
$this->_printGenericOrException(
|
||||
$level, "error", $content,
|
||||
$this->getIndentLevel(), $this->out);
|
||||
}
|
||||
|
||||
function end(bool $all=false): void {
|
||||
if ($all) {
|
||||
while ($this->actionLevel > 0) $this->adone();
|
||||
while ($this->titleLevel > 0) $this->_endTitle();
|
||||
$this->_endSection();
|
||||
} elseif ($this->actionLevel > 0) {
|
||||
while ($this->actions) $this->adone();
|
||||
while ($this->titles) $this->_endTitle();
|
||||
} elseif ($this->actions) {
|
||||
$this->_endAction();
|
||||
} elseif ($this->titleLevel > 0) {
|
||||
} elseif ($this->titles) {
|
||||
$this->_endTitle();
|
||||
}
|
||||
}
|
||||
|
@ -78,9 +78,13 @@ interface _IMessenger extends IMessenger {
|
||||
|
||||
function _getTitleMark(): int;
|
||||
|
||||
function _getTitleId(): ?int;
|
||||
|
||||
function _endTitle(?int $until=null): void;
|
||||
|
||||
function _getActionMark(): int;
|
||||
|
||||
function _getActionId(): ?int;
|
||||
|
||||
function _endAction(?int $until=null): void;
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
<?php
|
||||
require(__DIR__.'/../vendor/autoload.php');
|
||||
|
||||
use nulib\output\IMessenger;
|
||||
use nulib\output\std\ConsoleMessenger;
|
||||
use nulib\output\std\LogMessenger;
|
||||
use nulib\UserException;
|
||||
@ -13,8 +14,10 @@ for ($i = 1; $i <= $count; $i++) {
|
||||
switch ($argv[$i]) {
|
||||
case "--console": $class = ConsoleMessenger::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 "-i": $i++; $params["id"] = $argv[$i]; break;
|
||||
case "--show-ids": $params["show_ids"] = true; break;
|
||||
case "-t": $params["add_date"] = true; break;
|
||||
case "-n": $params["color"] = false; break;
|
||||
case "+n": $params["color"] = true; break;
|
||||
@ -40,15 +43,17 @@ for ($i = 1; $i <= $count; $i++) {
|
||||
}
|
||||
$msg = new $class($params);
|
||||
|
||||
###############################################################################
|
||||
|
||||
$msg->title("title0");
|
||||
$msg->desc("desc0");
|
||||
$msg->title("title1");
|
||||
$msg->desc("desc1");
|
||||
$msg->print("print under title1");
|
||||
$msg->end();
|
||||
$msg->print("print under title0");
|
||||
$msg->end();
|
||||
|
||||
exit;
|
||||
|
||||
$msg->desc("action avec step");
|
||||
$msg->action("action avec step");
|
||||
$msg->step("step");
|
||||
@ -116,44 +121,44 @@ $msg->note("note");
|
||||
$msg->warning("warning");
|
||||
$msg->error("error");
|
||||
|
||||
$msg->section("section", function ($msg) {
|
||||
$msg->title("title", function ($msg) {
|
||||
$msg->section("section", function (IMessenger $msg) {
|
||||
$msg->title("title", function (IMessenger $msg) {
|
||||
$msg->desc("desc");
|
||||
$msg->print("print");
|
||||
|
||||
$msg->desc("action avec step");
|
||||
$msg->action("action avec step", function ($msg) {
|
||||
$msg->action("action avec step", function (IMessenger $msg) {
|
||||
$msg->step("step");
|
||||
$msg->asuccess("action success");
|
||||
});
|
||||
|
||||
$msg->action("action avec step", function ($msg) {
|
||||
$msg->action("action avec step", function (IMessenger $msg) {
|
||||
$msg->step("step");
|
||||
$msg->afailure("action failure");
|
||||
});
|
||||
|
||||
$msg->action("action avec step", function ($msg) {
|
||||
$msg->action("action avec step", function (IMessenger $msg) {
|
||||
$msg->step("step");
|
||||
$msg->adone("action done");
|
||||
});
|
||||
|
||||
$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->action("action sans step", function ($msg) {
|
||||
$msg->action("action sans step", function (IMessenger $msg) {
|
||||
$msg->afailure("action failure");
|
||||
});
|
||||
|
||||
$msg->action("action sans step", function ($msg) {
|
||||
$msg->action("action sans step", function (IMessenger $msg) {
|
||||
$msg->adone("action done");
|
||||
});
|
||||
|
||||
$msg->desc("actions imbriquées");
|
||||
$msg->action("action0", function ($msg) {
|
||||
$msg->action("action1", function ($msg) {
|
||||
$msg->action("action2", function ($msg) {
|
||||
$msg->action("action0", function (IMessenger $msg) {
|
||||
$msg->action("action1", function (IMessenger $msg) {
|
||||
$msg->action("action2", function (IMessenger $msg) {
|
||||
$msg->asuccess("action2 success");
|
||||
});
|
||||
$msg->asuccess("action1 success");
|
||||
@ -162,38 +167,38 @@ $msg->section("section", function ($msg) {
|
||||
});
|
||||
|
||||
$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->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->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->adone();
|
||||
});
|
||||
|
||||
$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->action("action sans step, sans messages, failure", function ($msg) {
|
||||
$msg->action("action sans step, sans messages, failure", function (IMessenger $msg) {
|
||||
$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->desc("actions imbriquées, sans messages");
|
||||
$msg->action("action0", function ($msg) {
|
||||
$msg->action("action1", function ($msg) {
|
||||
$msg->action("action2", function ($msg) {
|
||||
$msg->action("action0", function (IMessenger $msg) {
|
||||
$msg->action("action1", function (IMessenger $msg) {
|
||||
$msg->action("action2", function (IMessenger $msg) {
|
||||
$msg->asuccess();
|
||||
});
|
||||
$msg->asuccess();
|
||||
@ -202,40 +207,40 @@ $msg->section("section", function ($msg) {
|
||||
});
|
||||
|
||||
$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");
|
||||
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");
|
||||
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");
|
||||
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->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;
|
||||
});
|
||||
|
||||
$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;
|
||||
});
|
||||
|
||||
$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";
|
||||
});
|
||||
|
||||
# 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");
|
||||
@ -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("another\ntitle");
|
||||
|
||||
@ -267,16 +272,16 @@ $msg->section("multi-line\nsection", function ($msg) {
|
||||
$msg->end();
|
||||
});
|
||||
|
||||
$msg->section("Exceptions", function ($msg) {
|
||||
$msg->section("Exceptions", function (IMessenger $msg) {
|
||||
$e = new Exception("message");
|
||||
$u1 = new UserException("userMessage");
|
||||
$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(["userException1", $u1]);
|
||||
$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($u1);
|
||||
$msg->info($u2);
|
||||
|
Loading…
x
Reference in New Issue
Block a user