nur-sery/src/output/std/StdMessenger.php

709 lines
24 KiB
PHP
Raw Normal View History

2023-12-26 19:50:59 +04:00
<?php
2023-12-27 11:38:43 +04:00
namespace nur\sery\output\std;
2023-12-26 19:50:59 +04:00
use Exception;
2024-05-21 18:30:26 +04:00
use nur\sery\A;
2024-04-05 08:31:49 +04:00
use nur\sery\cl;
use nur\sery\ExceptionShadow;
2023-12-27 11:38:43 +04:00
use nur\sery\output\IMessenger;
2024-05-22 20:44:20 +04:00
use nur\sery\UserException;
2023-12-28 13:29:39 +04:00
use Throwable;
2023-12-26 19:50:59 +04:00
2023-12-28 15:10:14 +04:00
class StdMessenger implements _IMessenger {
2023-12-26 20:01:41 +04:00
const INDENT = " ";
2023-12-28 11:54:15 +04:00
const DATE_FORMAT = 'Y-m-d\TH:i:s.u';
2023-12-26 20:01:41 +04:00
2023-12-28 15:10:14 +04:00
const VALID_LEVELS = [self::DEBUG, self::MINOR, self::NORMAL, self::MAJOR, self::NONE];
2023-12-26 19:50:59 +04:00
const LEVEL_MAP = [
2023-12-28 15:10:14 +04:00
"debug" => self::DEBUG,
"minor" => self::MINOR, "verbose" => self::MINOR,
2023-12-28 14:16:39 +04:00
"normal" => self::NORMAL,
"major" => self::MAJOR, "quiet" => self::MAJOR,
"none" => self::NONE, "silent" => self::NONE,
2023-12-26 19:50:59 +04:00
];
2023-12-28 13:29:39 +04:00
protected static function verifix_level($level, int $max_level=self::MAX_LEVEL): int {
2023-12-26 23:27:51 +04:00
if (!in_array($level, self::VALID_LEVELS, true)) {
$level = cl::get(self::LEVEL_MAP, $level, $level);
}
if (!in_array($level, self::VALID_LEVELS, true)) {
throw new Exception("$level: invalid level");
}
2023-12-27 01:53:46 +04:00
if ($level > $max_level) {
throw new Exception("$level: level not allowed here");
}
2023-12-26 23:27:51 +04:00
return $level;
}
const GENERIC_PREFIXES = [
2023-12-28 14:16:39 +04:00
self::MAJOR => [
2023-12-26 23:27:51 +04:00
"section" => [true, "SECTION!", "===", "<color @b>=", "=</color>", "==="],
2023-12-27 11:48:28 +04:00
"title" => [false, "TITLE!", null, "<color @b>T", "</color>", "==="],
2023-12-26 23:27:51 +04:00
"desc" => ["DESC!", "<color @b>></color>", ""],
2023-12-27 15:49:59 +04:00
"error" => ["CRIT.ERROR!", "<color @r>E!", "</color>"],
"warning" => ["CRIT.WARNING!", "<color @y>W!", "</color>"],
2023-12-27 01:39:08 +04:00
"note" => ["ATTENTION!", "<color @g>N!", "</color>"],
2023-12-28 15:10:14 +04:00
"info" => ["IMPORTANT!", "<color @b>N!", "</color>"],
2023-12-27 00:34:59 +04:00
"step" => ["*", "<color @w>.</color>", ""],
2023-12-26 23:27:51 +04:00
"print" => [null, null, null],
2023-12-26 19:50:59 +04:00
],
2023-12-28 14:16:39 +04:00
self::NORMAL => [
2023-12-26 23:27:51 +04:00
"section" => [true, "SECTION:", "---", "<color @b>-", "-</color>", "---"],
2023-12-27 01:39:08 +04:00
"title" => [false, "TITLE:", null, "<color @b>T</color><color b>", "</color>", "---"],
2023-12-26 23:27:51 +04:00
"desc" => ["DESC:", "<color @b>></color>", ""],
"error" => ["ERROR:", "<color @r>E</color><color r>", "</color>"],
"warning" => ["WARNING:", "<color @y>W</color><color y>", "</color>"],
2023-12-26 23:27:51 +04:00
"note" => ["NOTE:", "<color @g>N</color>", ""],
"info" => ["INFO:", "<color @b>I</color>", ""],
2023-12-27 00:34:59 +04:00
"step" => ["*", "<color @w>.</color>", ""],
2023-12-26 23:27:51 +04:00
"print" => [null, null, null],
2023-12-26 19:50:59 +04:00
],
2023-12-28 15:10:14 +04:00
self::MINOR => [
"section" => [true, "section", null, "<color @w>>>", "<<</color>", null],
"title" => [false, "title", null, "<color b>t", "</color>", null],
"desc" => ["desc", "<color b>></color>", ""],
"error" => ["error", "<color r>E</color><color -r>", "</color>"],
"warning" => ["warning", "<color y>W</color><color -y>", "</color>"],
2023-12-28 15:10:14 +04:00
"note" => ["note", "<color g>N</color>", ""],
"info" => ["info", "<color b>I</color><color w>", "</color>"],
"step" => ["*", "<color w>.</color>", ""],
"print" => [null, null, null],
],
2023-12-28 14:16:39 +04:00
self::DEBUG => [
2023-12-27 01:39:08 +04:00
"section" => [true, "section", null, "<color @w>>>", "<<</color>", null],
2023-12-26 23:27:51 +04:00
"title" => [false, "title", null, "<color b>t", "</color>", null],
2023-12-27 15:49:59 +04:00
"desc" => ["desc", "<color b>></color>", ""],
"error" => ["debugE", "<color r>e</color><color -r>", "</color>"],
"warning" => ["debugW", "<color y>w</color><color -y>", "</color>"],
2023-12-27 15:49:59 +04:00
"note" => ["debugN", "<color b>i</color>", ""],
2023-12-28 13:29:39 +04:00
"info" => ["debug", "<color @w>D</color><color w>", "</color>"],
2023-12-28 15:10:14 +04:00
"step" => ["*", "<color w>.</color>", ""],
2023-12-26 23:27:51 +04:00
"print" => [null, null, null],
2023-12-26 19:50:59 +04:00
],
];
const RESULT_PREFIXES = [
"failure" => ["(FAILURE)", "<color r>✘</color>"],
"success" => ["(SUCCESS)", "<color @g>✔</color>"],
2023-12-27 11:38:43 +04:00
"done" => [null, null],
2023-12-26 19:50:59 +04:00
];
function __construct(?array $params=null) {
2023-12-27 01:39:08 +04:00
$output = cl::get($params, "output");
2023-12-26 23:27:51 +04:00
$color = cl::get($params, "color");
2023-12-27 11:38:43 +04:00
$indent = cl::get($params, "indent", static::INDENT);
2023-12-27 01:53:46 +04:00
$defaultLevel = cl::get($params, "default_level");
2023-12-28 14:16:39 +04:00
if ($defaultLevel === null) $defaultLevel = self::NORMAL;
2023-12-27 01:53:46 +04:00
$defaultLevel = self::verifix_level($defaultLevel);
2023-12-27 11:38:43 +04:00
$debug = boolval(cl::get($params, "debug"));
2023-12-27 01:53:46 +04:00
$minLevel = cl::get($params, "min_level");
2023-12-28 14:16:39 +04:00
if ($minLevel === null && $debug) $minLevel = self::DEBUG;
2023-12-27 11:38:43 +04:00
if ($minLevel === null) $minLevel = cl::get($params, "verbosity"); # alias
2023-12-28 14:16:39 +04:00
if ($minLevel === null) $minLevel = self::NORMAL;
$minLevel = self::verifix_level($minLevel, self::NONE);
2023-12-26 19:50:59 +04:00
2023-12-28 11:54:15 +04:00
$addDate = boolval(cl::get($params, "add_date"));
$dateFormat = cl::get($params, "date_format", static::DATE_FORMAT);
$id = cl::get($params, "id");
2023-12-26 23:27:51 +04:00
$params = [
"color" => $color,
2023-12-27 11:38:43 +04:00
"indent" => $indent,
2023-12-26 23:27:51 +04:00
];
2023-12-27 01:39:08 +04:00
if ($output !== null) {
$this->err = $this->out = new StdOutput($output, $params);
} else {
$this->out = new StdOutput(STDOUT, $params);
$this->err = new StdOutput(STDERR, $params);
}
$this->defaultLevel = $defaultLevel;
2023-12-27 01:53:46 +04:00
$this->minLevel = $minLevel;
2023-12-28 11:54:15 +04:00
$this->addDate = $addDate;
$this->dateFormat = $dateFormat;
$this->id = $id;
2023-12-26 19:50:59 +04:00
$this->inSection = false;
2023-12-26 23:27:51 +04:00
$this->titles = [];
$this->actions = [];
2023-12-26 19:50:59 +04:00
}
2023-12-27 11:38:43 +04:00
function resetParams(?array $params=null): void {
$output = cl::get($params, "output");
$color = cl::get($params, "color");
$indent = cl::get($params, "indent");
$defaultLevel = cl::get($params, "default_level");
if ($defaultLevel !== null) $defaultLevel = self::verifix_level($defaultLevel);
$debug = cl::get($params, "debug");
$minLevel = cl::get($params, "min_level");
2023-12-28 14:16:39 +04:00
if ($minLevel === null && $debug !== null) $minLevel = $debug? self::DEBUG: self::NORMAL;
2023-12-27 11:38:43 +04:00
if ($minLevel === null) $minLevel = cl::get($params, "verbosity"); # alias
2023-12-28 14:16:39 +04:00
if ($minLevel !== null) $minLevel = self::verifix_level($minLevel, self::NONE);
2023-12-27 11:38:43 +04:00
2023-12-28 11:54:15 +04:00
$addDate = cl::get($params, "add_date");
$dateFormat = cl::get($params, "date_format");
$id = cl::get($params, "id");
2023-12-27 11:38:43 +04:00
$params = [
"output" => $output,
"color" => $color,
"indent" => $indent,
];
2024-01-03 10:47:08 +04:00
if ($this->out === $this->err) {
2023-12-27 11:38:43 +04:00
$this->out->resetParams($params);
} else {
2024-01-03 10:47:08 +04:00
# NB: si initialement [output] était null, et qu'on spécifie une valeur
# [output], alors les deux instances $out et $err sont mis à jour
# séparément avec la même valeur de output
# de plus, on ne peut plus revenir à la situation initiale avec une
# destination différente pour $out et $err
2023-12-27 11:38:43 +04:00
$this->out->resetParams($params);
$this->err->resetParams($params);
}
if ($defaultLevel !== null) $this->defaultLevel = $defaultLevel;
if ($minLevel !== null) $this->minLevel = $minLevel;
2023-12-28 11:54:15 +04:00
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;
2023-12-27 11:38:43 +04:00
}
2023-12-26 19:50:59 +04:00
/** @var StdOutput la sortie standard */
protected $out;
/** @var StdOutput la sortie d'erreur */
protected $err;
2023-12-26 23:27:51 +04:00
/** @var int level par défaut dans lequel les messages sont affichés */
protected $defaultLevel;
2023-12-27 01:53:46 +04:00
/** @var int level minimum que doivent avoir les messages pour être affichés */
protected $minLevel;
2023-12-28 11:54:15 +04:00
/** @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;
}
2023-12-28 15:10:14 +04:00
protected function decrLevel(int $level, int $amount=-1): int {
$level += $amount;
if ($level < self::MIN_LEVEL) $level = self::MIN_LEVEL;
return $level;
}
2023-12-26 23:27:51 +04:00
protected function checkLevel(?int &$level): bool {
if ($level === null) $level = $this->defaultLevel;
2023-12-28 15:10:14 +04:00
elseif ($level < 0) $level = $this->decrLevel($this->defaultLevel, $level);
2023-12-26 23:27:51 +04:00
return $level >= $this->minLevel;
}
2023-12-28 13:29:39 +04:00
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;
}
2023-12-28 11:54:15 +04:00
protected function _printTitle(?string $linePrefix, int $level,
string $type, $content,
int $indentLevel, StdOutput $out): void {
2023-12-26 23:27:51 +04:00
$prefixes = self::GENERIC_PREFIXES[$level][$type];
if ($prefixes[0]) $out->print();
if ($out->isColor()) {
$before = $prefixes[2];
$prefix = $prefixes[3];
$prefix2 = $prefix !== null? "$prefix ": null;
$suffix = $prefixes[4];
$suffix2 = $suffix !== null? " $suffix": null;
$after = $prefixes[5];
$lines = $out->getLines(false, $content);
$maxlen = 0;
foreach ($lines as &$content) {
$line = $out->filterColors($content);
2023-12-27 01:39:08 +04:00
$len = mb_strlen($line);
2023-12-26 23:27:51 +04:00
if ($len > $maxlen) $maxlen = $len;
$content = [$content, $len];
}; unset($content);
if ($before !== null) {
2023-12-28 11:54:15 +04:00
if ($linePrefix !== null) $out->write($linePrefix);
2023-12-26 23:27:51 +04:00
$out->iprint($indentLevel, $prefix, substr($before, 1), str_repeat($before[0], $maxlen), $suffix);
}
foreach ($lines as [$content, $len]) {
2023-12-28 11:54:15 +04:00
if ($linePrefix !== null) $out->write($linePrefix);
2023-12-26 23:27:51 +04:00
$padding = $len < $maxlen? str_repeat(" ", $maxlen - $len): null;
$out->iprint($indentLevel, $prefix2, $content, $padding, $suffix2);
}
if ($after !== null) {
2023-12-28 11:54:15 +04:00
if ($linePrefix !== null) $out->write($linePrefix);
2023-12-26 23:27:51 +04:00
$out->iprint($indentLevel, $prefix, substr($after, 1), str_repeat($after[0], $maxlen), $suffix);
}
} else {
$prefix = $prefixes[1];
if ($prefix !== null) $prefix .= " ";
2023-12-27 01:39:08 +04:00
$prefix2 = str_repeat(" ", mb_strlen($prefix));
2023-12-26 23:27:51 +04:00
$lines = $out->getLines(false, $content);
foreach ($lines as $content) {
2023-12-28 11:54:15 +04:00
if ($linePrefix !== null) $out->write($linePrefix);
2023-12-26 23:27:51 +04:00
$out->iprint($indentLevel, $prefix, $content);
$prefix = $prefix2;
}
}
}
2023-12-28 11:54:15 +04:00
protected function _printAction(?string $linePrefix, int $level,
2023-12-27 00:34:59 +04:00
bool $printContent, $content,
bool $printResult, ?bool $rsuccess, $rcontent,
int $indentLevel, StdOutput $out): void {
2023-12-27 01:39:08 +04:00
$color = $out->isColor();
2023-12-27 00:34:59 +04:00
if ($rsuccess === true) $type = "success";
elseif ($rsuccess === false) $type = "failure";
2023-12-27 11:38:43 +04:00
else $type = "done";
2023-12-27 01:39:08 +04:00
$rprefixes = self::RESULT_PREFIXES[$type];
if ($color) {
$rprefix = $rprefixes[1];
$rprefix2 = null;
if ($rprefix !== null) {
$rprefix .= " ";
$rprefix2 = $out->filterColors($out->filterContent($rprefix));
$rprefix2 = str_repeat(" ", mb_strlen($rprefix2));
}
} else {
$rprefix = $rprefixes[0];
if ($rprefix !== null) $rprefix .= " ";
$rprefix2 = str_repeat(" ", mb_strlen($rprefix));
}
2023-12-27 00:34:59 +04:00
if ($printContent && $printResult) {
if ($rcontent) {
2024-05-21 18:30:26 +04:00
A::ensure_array($content);
2023-12-27 00:34:59 +04:00
$content[] = ": ";
$content[] = $rcontent;
}
2023-12-27 01:39:08 +04:00
$lines = $out->getLines(false, $content);
foreach ($lines as $content) {
2023-12-28 11:54:15 +04:00
if ($linePrefix !== null) $out->write($linePrefix);
2023-12-27 01:39:08 +04:00
$out->iprint($indentLevel, $rprefix, $content);
$rprefix = $rprefix2;
}
2023-12-27 00:34:59 +04:00
} elseif ($printContent) {
$prefixes = self::GENERIC_PREFIXES[$level]["step"];
2023-12-27 01:39:08 +04:00
if ($color) {
2023-12-27 00:34:59 +04:00
$prefix = $prefixes[1];
2023-12-27 01:39:08 +04:00
if ($prefix !== null) $prefix .= " ";
$prefix2 = $out->filterColors($out->filterContent($prefix));
$prefix2 = str_repeat(" ", mb_strlen($prefix2));
2023-12-27 00:34:59 +04:00
$suffix = $prefixes[2];
} else {
$prefix = $prefixes[0];
2023-12-27 01:39:08 +04:00
if ($prefix !== null) $prefix .= " ";
$prefix2 = str_repeat(" ", mb_strlen($prefix));
2023-12-27 00:34:59 +04:00
$suffix = null;
}
2023-12-27 01:39:08 +04:00
$lines = $out->getLines(false, $content, ":");
foreach ($lines as $content) {
2023-12-28 11:54:15 +04:00
if ($linePrefix !== null) $out->write($linePrefix);
2023-12-27 01:39:08 +04:00
$out->iprint($indentLevel, $prefix, $content, $suffix);
$prefix = $prefix2;
}
2023-12-27 00:34:59 +04:00
} elseif ($printResult) {
2023-12-27 11:38:43 +04:00
if (!$rcontent) {
if ($type === "success") $rcontent = $color? "succès": "";
elseif ($type === "failure") $rcontent = $color? "échec": "";
elseif ($type === "done") $rcontent = "fait";
2023-12-27 00:34:59 +04:00
}
2023-12-27 01:39:08 +04:00
$rprefix = " $rprefix";
$rprefix2 = " $rprefix2";
$lines = $out->getLines(false, $rcontent);
foreach ($lines as $rcontent) {
2023-12-28 11:54:15 +04:00
if ($linePrefix !== null) $out->write($linePrefix);
2023-12-27 01:39:08 +04:00
$out->iprint($indentLevel, $rprefix, $rcontent);
$rprefix = $rprefix2;
}
2023-12-27 00:34:59 +04:00
}
}
2023-12-28 13:29:39 +04:00
protected function _printGeneric(?string $linePrefix, int $level,
string $type, $content,
int $indentLevel, StdOutput $out): void {
2023-12-26 23:27:51 +04:00
$prefixes = self::GENERIC_PREFIXES[$level][$type];
if ($out->isColor()) {
$prefix = $prefixes[1];
2023-12-27 01:39:08 +04:00
$prefix2 = null;
if ($prefix !== null) {
$prefix .= " ";
$prefix2 = $out->filterColors($out->filterContent($prefix));
$prefix2 = str_repeat(" ", mb_strlen($prefix2));
}
2023-12-26 23:27:51 +04:00
$suffix = $prefixes[2];
2023-12-27 01:39:08 +04:00
$lines = $out->getLines(false, $content);
foreach ($lines as $content) {
2023-12-28 11:54:15 +04:00
if ($linePrefix !== null) $out->write($linePrefix);
2023-12-27 01:39:08 +04:00
$out->iprint($indentLevel, $prefix, $content, $suffix);
$prefix = $prefix2;
}
2023-12-26 23:27:51 +04:00
} else {
$prefix = $prefixes[0];
2023-12-27 01:39:08 +04:00
if ($prefix !== null) $prefix .= " ";
$prefix2 = str_repeat(" ", mb_strlen($prefix));
$lines = $out->getLines(false, $content);
foreach ($lines as $content) {
2023-12-28 11:54:15 +04:00
if ($linePrefix !== null) $out->write($linePrefix);
2023-12-27 01:39:08 +04:00
$out->iprint($indentLevel, $prefix, $content);
$prefix = $prefix2;
}
2023-12-26 23:27:51 +04:00
}
}
2023-12-26 20:01:41 +04:00
2023-12-28 13:29:39 +04:00
protected function _printGenericOrException(?int $level, string $type, $content, int $indentLevel, StdOutput $out): void {
$linePrefix = $this->getLinePrefix();
# si $content contient des exceptions, les afficher avec un level moindre
$exceptions = null;
if (is_array($content)) {
$valueContent = null;
foreach ($content as $value) {
if ($value instanceof Throwable || $value instanceof ExceptionShadow) {
$exceptions[] = $value;
} else {
$valueContent[] = $value;
}
}
$content = $valueContent;
} elseif ($content instanceof Throwable || $content instanceof ExceptionShadow) {
$exceptions[] = $content;
$content = null;
}
$printActions = true;
$showContent = $this->checkLevel($level);
if ($content !== null && $showContent) {
$this->printActions(); $printActions = false;
$this->_printGeneric($linePrefix, $level, $type, $content, $indentLevel, $out);
}
if ($exceptions !== null) {
2023-12-28 15:10:14 +04:00
$level1 = $this->decrLevel($level);
2023-12-28 13:29:39 +04:00
$showTraceback = $this->checkLevel($level1);
foreach ($exceptions as $exception) {
# tout d'abord userMessage
$userMessage = UserException::get_user_message($exception);
if ($userMessage !== null && $showContent) {
if ($printActions) { $this->printActions(); $printActions = false; }
$this->_printGeneric($linePrefix, $level, $type, $userMessage, $indentLevel, $out);
}
# puis summary et traceback
if ($showTraceback) {
if ($printActions) { $this->printActions(); $printActions = false; }
$summary = UserException::get_summary($exception);
$traceback = UserException::get_traceback($exception);
$this->_printGeneric($linePrefix, $level1, $type, $summary, $indentLevel, $out);
$this->_printGeneric($linePrefix, $level1, $type, $traceback, $indentLevel, $out);
}
}
}
}
2023-12-26 19:50:59 +04:00
/** @var bool est-on dans une section? */
protected $inSection;
2023-12-26 23:27:51 +04:00
/** @var array section qui est en attente d'affichage */
2023-12-26 19:50:59 +04:00
protected $section;
2023-12-28 14:16:39 +04:00
function section($content, ?callable $func=null, ?int $level=null): void {
2023-12-28 15:10:14 +04:00
$this->_endSection();
2023-12-26 19:50:59 +04:00
$this->inSection = true;
2023-12-26 23:27:51 +04:00
if (!$this->checkLevel($level)) return;
$this->section = [
2023-12-28 11:54:15 +04:00
"line_prefix" => $this->getLinePrefix(),
2023-12-26 23:27:51 +04:00
"level" => $level,
"content" => $content,
"print_content" => true,
];
2023-12-28 14:16:39 +04:00
if ($func !== null) {
try {
$func($this);
} finally {
2023-12-28 15:10:14 +04:00
$this->_endSection();
2023-12-28 14:16:39 +04:00
}
}
2023-12-26 19:50:59 +04:00
}
protected function printSection() {
2023-12-26 23:27:51 +04:00
$section =& $this->section;
2023-12-28 13:29:39 +04:00
if ($section !== null && $section["print_content"]) {
2023-12-28 11:54:15 +04:00
$this->_printTitle(
$section["line_prefix"], $section["level"],
"section", $section["content"],
0, $this->err);
2023-12-26 23:27:51 +04:00
$section["print_content"] = false;
2023-12-26 19:50:59 +04:00
}
}
2023-12-28 15:10:14 +04:00
function _endSection(): void {
2023-12-26 19:50:59 +04:00
$this->inSection = false;
$this->section = null;
}
/** @var array */
protected $titles;
/** @var array */
2023-12-26 23:27:51 +04:00
protected $title;
2023-12-26 19:50:59 +04:00
2023-12-28 15:10:14 +04:00
function _getTitleMark(): int {
return count($this->titles);
}
2023-12-28 14:16:39 +04:00
function title($content, ?callable $func=null, ?int $level=null): void {
2023-12-26 23:27:51 +04:00
if (!$this->checkLevel($level)) return;
2023-12-28 15:10:14 +04:00
$until = $this->_getTitleMark();
2023-12-26 19:50:59 +04:00
$this->titles[] = [
2023-12-28 11:54:15 +04:00
"line_prefix" => $this->getLinePrefix(),
2023-12-26 23:27:51 +04:00
"level" => $level,
"content" => $content,
"print_content" => true,
2023-12-26 19:50:59 +04:00
"descs" => [],
2023-12-26 23:27:51 +04:00
"print_descs" => false,
2023-12-26 19:50:59 +04:00
];
2023-12-28 15:26:10 +04:00
$this->title =& $this->titles[$until];
2023-12-28 14:16:39 +04:00
if ($func !== null) {
try {
$func($this);
} finally {
2023-12-28 15:10:14 +04:00
$this->_endTitle($until);
2023-12-28 14:16:39 +04:00
}
}
2023-12-26 19:50:59 +04:00
}
2023-12-26 23:27:51 +04:00
function desc($content, ?int $level=null): void {
if (!$this->checkLevel($level)) return;
$title =& $this->title;
$title["descs"][] = [
2023-12-28 11:54:15 +04:00
"line_prefix" => $this->getLinePrefix(),
2023-12-26 23:27:51 +04:00
"level" => $level,
"content" => $content,
];
$title["print_descs"] = true;
2023-12-26 19:50:59 +04:00
}
protected function printTitles(): void {
$this->printSection();
2023-12-27 00:34:59 +04:00
$err = $this->err;
2023-12-26 23:27:51 +04:00
$indentLevel = 0;
foreach ($this->titles as &$title) {
if ($title["print_content"]) {
2023-12-28 11:54:15 +04:00
$this->_printTitle(
$title["line_prefix"], $title["level"],
"title", $title["content"],
$indentLevel, $err);
2023-12-26 23:27:51 +04:00
$title["print_content"] = false;
}
if ($title["print_descs"]) {
foreach ($title["descs"] as $desc) {
2023-12-28 13:29:39 +04:00
$this->_printGeneric(
$desc["line_prefix"], $desc["level"],
"desc", $desc["content"],
$indentLevel, $err);
2023-12-26 23:27:51 +04:00
}
$title["descs"] = [];
$title["print_descs"] = false;
}
$indentLevel++;
}; unset($title);
2023-12-26 19:50:59 +04:00
}
2023-12-28 15:10:14 +04:00
function _endTitle(?int $until=null): void {
if ($until === null) $until = $this->_getTitleMark() - 1;
2023-12-28 14:16:39 +04:00
while (count($this->titles) > $until) {
array_pop($this->titles);
}
2023-12-26 19:50:59 +04:00
if ($this->titles) {
2023-12-26 23:27:51 +04:00
$this->title =& $this->titles[count($this->titles) - 1];
2023-12-26 19:50:59 +04:00
} else {
2023-12-26 23:27:51 +04:00
$this->titles = [];
unset($this->title);
2023-12-26 19:50:59 +04:00
}
}
/** @var array */
protected $actions;
/** @var array */
2023-12-26 23:27:51 +04:00
protected $action;
2023-12-28 15:10:14 +04:00
function _getActionMark(): int {
return count($this->actions);
}
2023-12-28 14:16:39 +04:00
function action($content, ?callable $func=null, ?int $level=null): void {
2023-12-27 00:34:59 +04:00
$this->checkLevel($level);
2023-12-28 15:10:14 +04:00
$until = $this->_getActionMark();
2023-12-26 19:50:59 +04:00
$this->actions[] = [
2023-12-28 11:54:15 +04:00
"line_prefix" => $this->getLinePrefix(),
2023-12-26 19:50:59 +04:00
"level" => $level,
2023-12-26 20:01:41 +04:00
"content" => $content,
"print_content" => true,
2023-12-27 00:34:59 +04:00
"result_success" => null,
"result_content" => null,
2023-12-26 19:50:59 +04:00
];
2023-12-28 15:26:10 +04:00
$this->action =& $this->actions[$until];
2023-12-28 14:16:39 +04:00
if ($func !== null) {
try {
$result = $func($this);
2024-05-22 15:33:19 +04:00
if ($this->_getActionMark() > $until) {
$this->aresult($result);
2023-12-28 14:16:39 +04:00
}
2024-05-16 17:51:05 +04:00
} catch (Exception $e) {
$this->afailure($e);
throw $e;
2023-12-28 14:16:39 +04:00
} finally {
2023-12-28 15:10:14 +04:00
$this->_endAction($until);
2023-12-28 14:16:39 +04:00
}
}
2023-12-26 19:50:59 +04:00
}
2024-05-02 14:36:53 +04:00
function printActions(bool $endAction=false, ?int $overrideLevel=null): void {
2023-12-26 19:50:59 +04:00
$this->printTitles();
2023-12-27 00:34:59 +04:00
$err = $this->err;
$indentLevel = $this->getIndentLevel(false);
$lastIndex = count($this->actions) - 1;
$index = 0;
foreach ($this->actions as &$action) {
2023-12-28 15:10:14 +04:00
$mergeResult = $index++ == $lastIndex && $endAction;
2023-12-28 11:54:15 +04:00
$linePrefix = $action["line_prefix"];
2024-05-02 14:36:53 +04:00
$level = $overrideLevel?? $action["level"];
2023-12-27 00:34:59 +04:00
$content = $action["content"];
$printContent = $action["print_content"];
$rsuccess = $action["result_success"];
$rcontent = $action["result_content"];
2023-12-28 15:10:14 +04:00
if ($level < $this->minLevel) continue;
2023-12-27 00:34:59 +04:00
if ($mergeResult) {
2023-12-28 11:54:15 +04:00
$this->_printAction(
$linePrefix, $level,
$printContent, $content,
true, $rsuccess, $rcontent,
$indentLevel, $err);
2023-12-27 00:34:59 +04:00
} elseif ($printContent) {
2023-12-28 11:54:15 +04:00
$this->_printAction(
$linePrefix, $level,
$printContent, $content,
false, $rsuccess, $rcontent,
$indentLevel, $err);
2023-12-27 00:34:59 +04:00
$action["print_content"] = false;
}
$indentLevel++;
}; unset($action);
2023-12-28 15:10:14 +04:00
if ($endAction) $this->_endAction();
2023-12-26 19:50:59 +04:00
}
2023-12-26 23:27:51 +04:00
function step($content, ?int $level=null): void {
2023-12-28 13:29:39 +04:00
$this->_printGenericOrException($level, "step", $content, $this->getIndentLevel(), $this->err);
2023-12-26 19:50:59 +04:00
}
2024-05-02 14:36:53 +04:00
function asuccess($content=null, ?int $overrideLevel=null): void {
2023-12-26 20:01:41 +04:00
if (!$this->actions) $this->action(null);
2023-12-27 00:34:59 +04:00
$this->action["result_success"] = true;
$this->action["result_content"] = $content;
2024-05-02 14:36:53 +04:00
$this->printActions(true, $overrideLevel);
2023-12-26 19:50:59 +04:00
}
2024-05-02 14:36:53 +04:00
function afailure($content=null, ?int $overrideLevel=null): void {
2023-12-26 20:01:41 +04:00
if (!$this->actions) $this->action(null);
2023-12-27 00:34:59 +04:00
$this->action["result_success"] = false;
$this->action["result_content"] = $content;
2024-05-02 14:36:53 +04:00
$this->printActions(true, $overrideLevel);
2023-12-26 19:50:59 +04:00
}
2024-05-02 14:36:53 +04:00
function adone($content=null, ?int $overrideLevel=null): void {
2023-12-26 20:01:41 +04:00
if (!$this->actions) $this->action(null);
2023-12-27 00:34:59 +04:00
$this->action["result_success"] = null;
$this->action["result_content"] = $content;
2024-05-02 14:36:53 +04:00
$this->printActions(true, $overrideLevel);
2023-12-26 19:50:59 +04:00
}
2024-05-22 15:33:19 +04:00
function aresult($result=null, ?int $overrideLevel=null): void {
if (!$this->actions) $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);
else $this->adone($result, $overrideLevel);
}
2023-12-28 15:10:14 +04:00
function _endAction(?int $until=null): void {
if ($until === null) $until = $this->_getActionMark() - 1;
2023-12-28 14:16:39 +04:00
while (count($this->actions) > $until) {
array_pop($this->actions);
}
2023-12-26 19:50:59 +04:00
if ($this->actions) {
2023-12-26 23:27:51 +04:00
$this->action =& $this->actions[count($this->actions) - 1];
2023-12-26 19:50:59 +04:00
} else {
2023-12-26 23:27:51 +04:00
$this->actions = [];
unset($this->action);
2023-12-26 19:50:59 +04:00
}
}
2023-12-26 23:27:51 +04:00
function print($content, ?int $level=null): void {
2023-12-28 13:29:39 +04:00
$this->_printGenericOrException($level, "print", $content, $this->getIndentLevel(), $this->out);
2023-12-26 20:01:41 +04:00
}
2023-12-26 23:27:51 +04:00
function info($content, ?int $level=null): void {
2023-12-28 13:29:39 +04:00
$this->_printGenericOrException($level, "info", $content, $this->getIndentLevel(), $this->err);
2023-12-26 19:50:59 +04:00
}
2023-12-26 23:27:51 +04:00
function note($content, ?int $level=null): void {
2023-12-28 13:29:39 +04:00
$this->_printGenericOrException($level, "note", $content, $this->getIndentLevel(), $this->err);
2023-12-26 19:50:59 +04:00
}
function warning($content, ?int $level=null): void {
$this->_printGenericOrException($level, "warning", $content, $this->getIndentLevel(), $this->err);
2023-12-26 19:50:59 +04:00
}
2023-12-26 23:27:51 +04:00
function error($content, ?int $level=null): void {
2023-12-28 13:29:39 +04:00
$this->_printGenericOrException($level, "error", $content, $this->getIndentLevel(), $this->err);
2023-12-26 19:50:59 +04:00
}
2023-12-27 11:38:43 +04:00
function end(bool $all=false): void {
2023-12-26 19:50:59 +04:00
if ($all) {
2023-12-27 11:38:43 +04:00
while ($this->actions) $this->adone();
2023-12-28 15:10:14 +04:00
while ($this->titles) $this->_endTitle();
$this->_endSection();
2023-12-26 19:50:59 +04:00
} elseif ($this->actions) {
2023-12-28 15:10:14 +04:00
$this->_endAction();
2023-12-26 19:50:59 +04:00
} elseif ($this->titles) {
2023-12-28 15:10:14 +04:00
$this->_endTitle();
2023-12-26 19:50:59 +04:00
} else {
2023-12-28 15:10:14 +04:00
$this->_endSection();
2023-12-26 19:50:59 +04:00
}
}
}