modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2023-12-28 11:54:15 +04:00
parent 888dd6be1a
commit f6c15f130a
9 changed files with 215 additions and 52 deletions

View File

@ -10,6 +10,11 @@ interface IMessenger {
/** réinitialiser les paramètres de l'objet */
function resetParams(?array $params=null): void;
/**
* créer une copie de cet objet avec éventuellement des paramètres différents
*/
function clone(?array $params=null): self;
/**
* commencer une section.
*

View File

@ -2,11 +2,6 @@
## TOOD
* [ ] ProxyMessenger, vers un ou plusieurs messengers
* [ ] support sous-système (instance locale ou globale)
* [ ] msg::push() --> nouvelle instance globale
* [ ] msg::get() --> nouvelle instance locale
* [ ] support `add_date` et `date_format`
* [ ] affichage des exceptions en message technique.
si pas de message, prendre le message de l'exception par défaut
* [ ] possibilité de paramétrer le nom du fichier destination pour faire une

View File

@ -1,8 +1,8 @@
<?php
namespace nur\sery\output;
use nulib\ValueException;
use nulib\str;
use nulib\ValueException;
/**
* Class _messenger: classe de base pour say, log et msg
@ -32,14 +32,35 @@ abstract class _messenger {
}
}
/** @return IMessenger[] */
abstract static function get_msgs(): array;
abstract static function get(): IMessenger;
/** obtenir une nouvelle instance, avec un nouveau paramétrage */
static function new(?array $params=null): IMessenger {
return static::get()->clone($params);
}
/** @var IMessenger */
protected static $msg;
/** @var IMessenger[] */
protected static $stack;
/** pousser une nouvelle instance avec un nouveau paramétrage sur la pile */
static function push(?array $params=null) {
self::$stack[] = static::get();
self::$msg = self::new($params);
}
/** dépiler la précédente instance */
static function pop(): IMessenger {
if (self::$stack) $msg = self::$msg = array_pop(self::$stack);
else $msg = self::$msg;
return $msg;
}
static final function __callStatic($name, $args) {
$name = str::us2camel($name);
foreach (static::get_msgs() as $msg) {
call_user_func_array([$msg, $name], $args);
}
call_user_func_array([static::get(), $name], $args);
}
#############################################################################
@ -49,24 +70,24 @@ abstract class _messenger {
const MAJOR = IMessenger::LEVEL_MAJOR;
const NONE = IMessenger::LEVEL_NONE;
static function reset_params(?array $params) { foreach (static::get_msgs() as $msg) { $msg->resetParams($params); } }
function section($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->section($content, $level); } }
function title($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->title($content, $level); } }
function desc($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->desc($content, $level); } }
function action($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->action($content, $level); } }
function step($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->step($content, $level); } }
function asuccess($content=null): void { foreach (static::get_msgs() as $msg) { $msg->asuccess($content); } }
function afailure($content=null): void { foreach (static::get_msgs() as $msg) { $msg->afailure($content); } }
function adone($content=null): void { foreach (static::get_msgs() as $msg) { $msg->adone($content); } }
function print($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->print($content, $level); } }
function info($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->info($content, $level); } }
function note($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->note($content, $level); } }
function warn($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->warn($content, $level); } }
function error($content, ?int $level=null): void { foreach (static::get_msgs() as $msg) { $msg->error($content, $level); } }
function debug($content): void { self::info($content, self::DEBUG);}
function important($content): void { self::info($content, self::MAJOR);}
function attention($content): void { self::note($content, self::MAJOR);}
function critical_warn($content): void { self::warn($content, self::MAJOR);}
function critical_error($content): void { self::error($content, self::MAJOR);}
function end(bool $all=false): void { foreach (static::get_msgs() as $msg) { $msg->end($all); } }
static function reset_params(?array $params=null): void { static::get()->resetParams($params); }
static function section($content, ?int $level=null): void { static::get()->section($content, $level); }
static function title($content, ?int $level=null): void { static::get()->title($content, $level); }
static function desc($content, ?int $level=null): void { static::get()->desc($content, $level); }
static function action($content, ?int $level=null): void { static::get()->action($content, $level); }
static function step($content, ?int $level=null): void { static::get()->step($content, $level); }
static function asuccess($content=null): void { static::get()->asuccess($content); }
static function afailure($content=null): void { static::get()->afailure($content); }
static function adone($content=null): void { static::get()->adone($content); }
static function print($content, ?int $level=null): void { static::get()->print($content, $level); }
static function info($content, ?int $level=null): void { static::get()->info($content, $level); }
static function note($content, ?int $level=null): void { static::get()->note($content, $level); }
static function warn($content, ?int $level=null): void { static::get()->warn($content, $level); }
static function error($content, ?int $level=null): void { static::get()->error($content, $level); }
static function debug($content): void { self::info($content, self::DEBUG);}
static function important($content): void { self::info($content, self::MAJOR);}
static function attention($content): void { self::note($content, self::MAJOR);}
static function critwarn($content): void { self::warn($content, self::MAJOR);}
static function criterror($content): void { self::error($content, self::MAJOR);}
static function end(bool $all=false): void { static::get()->end($all); }
}

View File

@ -1,12 +1,18 @@
<?php
namespace nur\sery\output;
use nur\sery\output\std\ProxyMessenger;
/**
* Class log: inscrire un message dans les logs uniquement
*/
class log extends _messenger {
static function get_msgs(): array {
$log = self::$log;
return $log !== null? [$log]: [];
static function get(): IMessenger {
if (self::$msg === null) {
$msg = self::$log;
if ($msg === null) $msg = new ProxyMessenger();
self::$msg = $msg;
}
return self::$msg;
}
}

View File

@ -1,16 +1,22 @@
<?php
namespace nur\sery\output;
use nur\sery\output\std\ProxyMessenger;
/**
* Class msg: inscrire un message dans les logs ET l'afficher sur la console
*/
class msg extends _messenger {
static function get_msgs(): array {
$msgs = [];
$log = self::$log;
if ($log !== null) $msgs[] = $log;
$say = self::$say;
if ($say !== null) $msgs[] = $say;
return $msgs;
static function get(): IMessenger {
if (self::$msg === null) {
$log = self::$log;
$say = self::$say;
if ($log !== null && $say !== null) $msg = new ProxyMessenger($log, $say);
elseif ($log !== null) $msg = $log;
elseif ($say !== null) $msg = $say;
else $msg = new ProxyMessenger();
self::$msg = $msg;
}
return self::$msg;
}
}

View File

@ -1,12 +1,18 @@
<?php
namespace nur\sery\output;
use nur\sery\output\std\ProxyMessenger;
/**
* Class say: afficher un message sur la console uniquement
*/
class say extends _messenger {
static function get_msgs(): array {
$say = self::$say;
return $say !== null? [$say]: [];
static function get(): IMessenger {
if (self::$msg === null) {
$msg = self::$say;
if ($msg === null) $msg = new ProxyMessenger();
self::$msg = $msg;
}
return self::$msg;
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace nur\sery\output\std;
use nur\sery\output\IMessenger;
/**
* Class ProxyMessenger: un proxy vers ou un plusieurs instances de IMessenger
*/
class ProxyMessenger implements IMessenger {
function __construct(?IMessenger ...$msgs) {
$this->msgs = [];
foreach ($msgs as $msg) {
if ($msg !== null) $this->msgs[] = $msg;
}
}
/** @var IMessenger[] */
protected $msgs;
function resetParams(?array $params=null): void { foreach ($this->msgs as $msg) { $msg->resetParams($params); } }
function clone(?array $params=null): self {
$clone = clone $this;
foreach ($clone->msgs as &$msg) {
$msg = $msg->clone($params);
}; unset($msg);
return $clone;
}
function section($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->section($content, $level); } }
function title($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->title($content, $level); } }
function desc($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->desc($content, $level); } }
function action($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->action($content, $level); } }
function step($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->step($content, $level); } }
function asuccess($content=null): void { foreach ($this->msgs as $msg) { $msg->asuccess($content); } }
function afailure($content=null): void { foreach ($this->msgs as $msg) { $msg->afailure($content); } }
function adone($content=null): void { foreach ($this->msgs as $msg) { $msg->adone($content); } }
function print($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->print($content, $level); } }
function info($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->info($content, $level); } }
function note($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->note($content, $level); } }
function warn($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->warn($content, $level); } }
function error($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->error($content, $level); } }
function end(bool $all=false): void { foreach ($this->msgs as $msg) { $msg->end($all); } }
}

View File

@ -7,6 +7,7 @@ use nur\sery\output\IMessenger;
class StdMessenger implements IMessenger {
const INDENT = " ";
const DATE_FORMAT = 'Y-m-d\TH:i:s.u';
const VALID_LEVELS = [
self::LEVEL_DEBUG,
@ -92,6 +93,10 @@ class StdMessenger implements IMessenger {
if ($minLevel === null) $minLevel = self::LEVEL_NORMAL;
$minLevel = self::verifix_level($minLevel, self::LEVEL_NONE);
$addDate = boolval(cl::get($params, "add_date"));
$dateFormat = cl::get($params, "date_format", static::DATE_FORMAT);
$id = cl::get($params, "id");
$params = [
"color" => $color,
"indent" => $indent,
@ -104,11 +109,12 @@ class StdMessenger implements IMessenger {
}
$this->defaultLevel = $defaultLevel;
$this->minLevel = $minLevel;
$this->addDate = $addDate;
$this->dateFormat = $dateFormat;
$this->id = $id;
$this->inSection = false;
$this->titles = [];
$this->title = null;
$this->actions = [];
$this->action = null;
}
function resetParams(?array $params=null): void {
@ -125,6 +131,10 @@ class StdMessenger implements IMessenger {
if ($minLevel === null) $minLevel = cl::get($params, "verbosity"); # alias
if ($minLevel !== null) $minLevel = self::verifix_level($minLevel, self::LEVEL_NONE);
$addDate = cl::get($params, "add_date");
$dateFormat = cl::get($params, "date_format");
$id = cl::get($params, "id");
$params = [
"output" => $output,
"color" => $color,
@ -138,6 +148,19 @@ class StdMessenger implements IMessenger {
}
if ($defaultLevel !== null) $this->defaultLevel = $defaultLevel;
if ($minLevel !== null) $this->minLevel = $minLevel;
if ($addDate !== null) $this->addDate = boolval($addDate);
if ($dateFormat !== null) $this->dateFormat = $dateFormat;
if ($id !== null) $this->id = $id;
}
function clone(?array $params=null): IMessenger {
$clone = clone $this;
if ($params !== null) $clone->resetParams($params);
#XXX faut-il marquer la section et les titres du clone à "print" => false?
# ou en faire des références au parent?
# dans tous les cas, on considère qu'il n'y a pas d'actions en cours, et on
# ne doit pas dépiler avec end() plus que l'état que l'on a eu lors du clone
return $clone;
}
/** @var StdOutput la sortie standard */
@ -152,12 +175,35 @@ class StdMessenger implements IMessenger {
/** @var int level minimum que doivent avoir les messages pour être affichés */
protected $minLevel;
/** @var bool faut-il ajouter la date à chaque ligne? */
protected $addDate;
/** @var string format de la date */
protected $dateFormat;
/** @var ?string identifiant de ce messenger, à ajouter à chaque ligne */
protected $id;
protected function getLinePrefix(): ?string {
$linePrefix = null;
if ($this->addDate) {
$date = date_create()->format($this->dateFormat);
$linePrefix .= "$date ";
}
if ($this->id !== null) {
$linePrefix .= "$this->id ";
}
return $linePrefix;
}
protected function checkLevel(?int &$level): bool {
if ($level === null) $level = $this->defaultLevel;
return $level >= $this->minLevel;
}
protected function _printTitle(int $level, string $type, $content, int $indentLevel, StdOutput $out): void {
protected function _printTitle(?string $linePrefix, int $level,
string $type, $content,
int $indentLevel, StdOutput $out): void {
$prefixes = self::GENERIC_PREFIXES[$level][$type];
if ($prefixes[0]) $out->print();
if ($out->isColor()) {
@ -177,13 +223,16 @@ class StdMessenger implements IMessenger {
$content = [$content, $len];
}; unset($content);
if ($before !== null) {
if ($linePrefix !== null) $out->write($linePrefix);
$out->iprint($indentLevel, $prefix, substr($before, 1), str_repeat($before[0], $maxlen), $suffix);
}
foreach ($lines as [$content, $len]) {
if ($linePrefix !== null) $out->write($linePrefix);
$padding = $len < $maxlen? str_repeat(" ", $maxlen - $len): null;
$out->iprint($indentLevel, $prefix2, $content, $padding, $suffix2);
}
if ($after !== null) {
if ($linePrefix !== null) $out->write($linePrefix);
$out->iprint($indentLevel, $prefix, substr($after, 1), str_repeat($after[0], $maxlen), $suffix);
}
} else {
@ -192,13 +241,14 @@ class StdMessenger implements IMessenger {
$prefix2 = str_repeat(" ", mb_strlen($prefix));
$lines = $out->getLines(false, $content);
foreach ($lines as $content) {
if ($linePrefix !== null) $out->write($linePrefix);
$out->iprint($indentLevel, $prefix, $content);
$prefix = $prefix2;
}
}
}
protected function _printAction(int $level,
protected function _printAction(?string $linePrefix, int $level,
bool $printContent, $content,
bool $printResult, ?bool $rsuccess, $rcontent,
int $indentLevel, StdOutput $out): void {
@ -228,6 +278,7 @@ class StdMessenger implements IMessenger {
}
$lines = $out->getLines(false, $content);
foreach ($lines as $content) {
if ($linePrefix !== null) $out->write($linePrefix);
$out->iprint($indentLevel, $rprefix, $content);
$rprefix = $rprefix2;
}
@ -247,6 +298,7 @@ class StdMessenger implements IMessenger {
}
$lines = $out->getLines(false, $content, ":");
foreach ($lines as $content) {
if ($linePrefix !== null) $out->write($linePrefix);
$out->iprint($indentLevel, $prefix, $content, $suffix);
$prefix = $prefix2;
}
@ -260,6 +312,7 @@ class StdMessenger implements IMessenger {
$rprefix2 = " $rprefix2";
$lines = $out->getLines(false, $rcontent);
foreach ($lines as $rcontent) {
if ($linePrefix !== null) $out->write($linePrefix);
$out->iprint($indentLevel, $rprefix, $rcontent);
$rprefix = $rprefix2;
}
@ -267,6 +320,7 @@ class StdMessenger implements IMessenger {
}
protected function _printGeneric(int $level, string $type, $content, int $indentLevel, StdOutput $out): void {
$linePrefix = $this->getLinePrefix();
$prefixes = self::GENERIC_PREFIXES[$level][$type];
if ($out->isColor()) {
$prefix = $prefixes[1];
@ -279,6 +333,7 @@ class StdMessenger implements IMessenger {
$suffix = $prefixes[2];
$lines = $out->getLines(false, $content);
foreach ($lines as $content) {
if ($linePrefix !== null) $out->write($linePrefix);
$out->iprint($indentLevel, $prefix, $content, $suffix);
$prefix = $prefix2;
}
@ -288,6 +343,7 @@ class StdMessenger implements IMessenger {
$prefix2 = str_repeat(" ", mb_strlen($prefix));
$lines = $out->getLines(false, $content);
foreach ($lines as $content) {
if ($linePrefix !== null) $out->write($linePrefix);
$out->iprint($indentLevel, $prefix, $content);
$prefix = $prefix2;
}
@ -305,6 +361,7 @@ class StdMessenger implements IMessenger {
$this->inSection = true;
if (!$this->checkLevel($level)) return;
$this->section = [
"line_prefix" => $this->getLinePrefix(),
"level" => $level,
"content" => $content,
"print_content" => true,
@ -314,7 +371,10 @@ class StdMessenger implements IMessenger {
protected function printSection() {
$section =& $this->section;
if ($section["print_content"]) {
$this->_printTitle($section["level"], "section", $section["content"], 0, $this->err);
$this->_printTitle(
$section["line_prefix"], $section["level"],
"section", $section["content"],
0, $this->err);
$section["print_content"] = false;
}
}
@ -345,6 +405,7 @@ class StdMessenger implements IMessenger {
function title($content, ?int $level=null): void {
if (!$this->checkLevel($level)) return;
$this->titles[] = [
"line_prefix" => $this->getLinePrefix(),
"level" => $level,
"content" => $content,
"print_content" => true,
@ -358,6 +419,7 @@ class StdMessenger implements IMessenger {
if (!$this->checkLevel($level)) return;
$title =& $this->title;
$title["descs"][] = [
"line_prefix" => $this->getLinePrefix(),
"level" => $level,
"content" => $content,
];
@ -370,7 +432,10 @@ class StdMessenger implements IMessenger {
$indentLevel = 0;
foreach ($this->titles as &$title) {
if ($title["print_content"]) {
$this->_printTitle($title["level"], "title", $title["content"], $indentLevel, $err);
$this->_printTitle(
$title["line_prefix"], $title["level"],
"title", $title["content"],
$indentLevel, $err);
$title["print_content"] = false;
}
if ($title["print_descs"]) {
@ -403,6 +468,7 @@ class StdMessenger implements IMessenger {
function action($content, ?int $level=null): void {
$this->checkLevel($level);
$this->actions[] = [
"line_prefix" => $this->getLinePrefix(),
"level" => $level,
"content" => $content,
"print_content" => true,
@ -420,15 +486,24 @@ class StdMessenger implements IMessenger {
$index = 0;
foreach ($this->actions as &$action) {
$mergeResult = $index++ == $lastIndex && $willEnd;
$linePrefix = $action["line_prefix"];
$level = $action["level"];
$content = $action["content"];
$printContent = $action["print_content"];
$rsuccess = $action["result_success"];
$rcontent = $action["result_content"];
if ($mergeResult) {
$this->_printAction($level, $printContent, $content, true, $rsuccess, $rcontent, $indentLevel, $err);
$this->_printAction(
$linePrefix, $level,
$printContent, $content,
true, $rsuccess, $rcontent,
$indentLevel, $err);
} elseif ($printContent) {
$this->_printAction($level, $printContent, $content, false, $rsuccess, $rcontent, $indentLevel, $err);
$this->_printAction(
$linePrefix, $level,
$printContent, $content,
false, $rsuccess, $rcontent,
$indentLevel, $err);
$action["print_content"] = false;
}
$indentLevel++;

View File

@ -34,6 +34,13 @@ for ($i = 1; $i <= $count; $i++) {
case "-M":
$params["default_level"] = "major";
break;
case "-i":
$i++;
$params["id"] = $argv[$i];
break;
case "-t":
$params["add_date"] = true;
break;
}
}
$c = new StdMessenger($params);