modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2023-12-27 11:38:43 +04:00
parent e62e4ebfa4
commit 331afca9bf
12 changed files with 247 additions and 132 deletions

View File

@ -7,6 +7,9 @@ namespace nur\sery\output;
interface IMessenger { interface IMessenger {
const LEVEL_DEBUG = -1, LEVEL_NORMAL = 0, LEVEL_MAJOR = 1, LEVEL_NONE = 2; const LEVEL_DEBUG = -1, LEVEL_NORMAL = 0, LEVEL_MAJOR = 1, LEVEL_NONE = 2;
/** réinitialiser les paramètres de l'objet */
function resetParams(?array $params=null): void;
/** /**
* commencer une section. * commencer une section.
* *
@ -45,35 +48,35 @@ interface IMessenger {
* *
* démarrer une action le cas échéant (et la terminer aussitôt) * démarrer une action le cas échéant (et la terminer aussitôt)
*/ */
function success($content=null): void; function asuccess($content=null): void;
/** /**
* terminer l'action courante avec le résultat "échec" * terminer l'action courante avec le résultat "échec"
* *
* démarrer une action le cas échéant (et la terminer aussitôt) * démarrer une action le cas échéant (et la terminer aussitôt)
*/ */
function failure($content=null): void; function afailure($content=null): void;
/** /**
* terminer l'action courante avec le résultat "neutre" * terminer l'action courante avec le résultat "neutre"
* *
* démarrer une action le cas échéant (et la terminer aussitôt) * démarrer une action le cas échéant (et la terminer aussitôt)
*/ */
function neutral($content=null): void; function adone($content=null): void;
/** afficher une donnée non structurée */ /** afficher une donnée non structurée */
function print($content, ?int $level=null): void; function print($content, ?int $level=null): void;
/** ajouter un événément "information" */ /** afficher un événément "information" */
function info($content, ?int $level=null): void; function info($content, ?int $level=null): void;
/** ajouter un événément "information importante" */ /** afficher un événément "information importante" */
function note($content, ?int $level=null): void; function note($content, ?int $level=null): void;
/** ajouter un événément "avertissement" */ /** afficher un événément "avertissement" */
function warn($content, ?int $level=null): void; function warn($content, ?int $level=null): void;
/** ajouter un événément "erreur" */ /** afficher un événément "erreur" */
function error($content, ?int $level=null): void; function error($content, ?int $level=null): void;
/** /**
@ -83,5 +86,5 @@ interface IMessenger {
* @param bool $all faut-il terminer *tous* les chapitres ainsi que la section * @param bool $all faut-il terminer *tous* les chapitres ainsi que la section
* en cours? * en cours?
*/ */
function end(bool $all=false); function end(bool $all=false): void;
} }

View File

@ -1,16 +0,0 @@
<?php
namespace nur\sery\output;
/**
* Class Output: une destination pour écrire du contenu
*/
abstract class Output {
/** afficher le contenu spécifié */
abstract function print(?array $contents): void;
/** afficher une ligne de donnée au format CSV */
abstract function printCsv(?array $row): void;
/** afficher un objet */
abstract function printObject($object): void;
}

View File

@ -1,11 +0,0 @@
<?php
namespace nur\sery\output;
trait TOutputObjectAsCsv {
function printObject($object): void {
if ($object === null) return;
if (is_object($object)) $object = (array)$object;
if (is_array($object)) $this->printCsv($object);
else $this->print([$object]);
}
}

View File

@ -1,17 +0,0 @@
<?php
namespace nur\sery\output;
use nulib\str;
class TtyOutput extends Output {
use TOutputObjectAsCsv;
function print(?array $contents): void {
echo str::join("", $contents);
}
function printCsv(?array $row): void {
#XXX rendre paramétrable les options de sortie
fputcsv(STDOUT, $row);
}
}

View File

@ -1,12 +0,0 @@
<?php
namespace nur\sery\output;
use nulib\str;
class WebOutput extends Output {
use TOutputObjectAsCsv;
function print($contents): void {
echo str::join("", $contents);
}
}

67
src/output/_messenger.php Normal file
View File

@ -0,0 +1,67 @@
<?php
namespace nur\sery\output;
use nulib\ValueException;
use nulib\str;
/**
* Class _messenger: classe de base pour say, log et msg
*/
abstract class _messenger {
/** @var IMessenger */
protected static $say;
/** @var IMessenger */
protected static $log;
static final function set_messenger(IMessenger $say, ?IMessenger $log=null) {
self::$say = $say;
if ($log !== null) self::$log = $log;
}
static final function set_messenger_class(string $say_class, ?string $log_class=null) {
if (!is_subclass_of($say_class, IMessenger::class)) {
throw ValueException::unexpected_class($say_class, IMessenger::class);
}
self::$say = new $say_class();
if ($log_class !== null) {
if (!is_subclass_of($log_class, IMessenger::class)) {
throw ValueException::unexpected_class($log_class, IMessenger::class);
}
self::$log = new $log_class();
}
}
/** @return IMessenger[] */
abstract static function get_msgs(): array;
static final function __callStatic($name, $args) {
$name = str::us2camel($name);
foreach (static::get_msgs() as $msg) {
call_user_func_array([$msg, $name], $args);
}
}
#############################################################################
const DEBUG = IMessenger::LEVEL_DEBUG;
const NORMAL = IMessenger::LEVEL_NORMAL;
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 end(bool $all=false): void { foreach (static::get_msgs() as $msg) { $msg->end($all); } }
}

12
src/output/log.php Normal file
View File

@ -0,0 +1,12 @@
<?php
namespace nur\sery\output;
/**
* 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]: [];
}
}

16
src/output/msg.php Normal file
View File

@ -0,0 +1,16 @@
<?php
namespace nur\sery\output;
/**
* 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;
}
}

12
src/output/say.php Normal file
View File

@ -0,0 +1,12 @@
<?php
namespace nur\sery\output;
/**
* 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]: [];
}
}

View File

@ -1,8 +1,9 @@
<?php <?php
namespace nur\sery\output; namespace nur\sery\output\std;
use Exception; use Exception;
use nulib\cl; use nulib\cl;
use nur\sery\output\IMessenger;
class StdMessenger implements IMessenger { class StdMessenger implements IMessenger {
const INDENT = " "; const INDENT = " ";
@ -14,13 +15,10 @@ class StdMessenger implements IMessenger {
self::LEVEL_NONE, self::LEVEL_NONE,
]; ];
const LEVEL_MAP = [ const LEVEL_MAP = [
"debug" => self::LEVEL_DEBUG, "debug" => self::LEVEL_DEBUG, "verbose" => self::LEVEL_DEBUG,
"d" => self::LEVEL_DEBUG,
"normal" => self::LEVEL_NORMAL, "normal" => self::LEVEL_NORMAL,
"n" => self::LEVEL_NORMAL, "major" => self::LEVEL_MAJOR, "quiet" => self::LEVEL_MAJOR,
"major" => self::LEVEL_MAJOR, "none" => self::LEVEL_NONE, "silent" => self::LEVEL_NONE,
"m" => self::LEVEL_MAJOR,
"none" => self::LEVEL_NONE,
]; ];
protected static function verifix_level($level, int $max_level=self::LEVEL_MAJOR): int { protected static function verifix_level($level, int $max_level=self::LEVEL_MAJOR): int {
@ -75,23 +73,28 @@ class StdMessenger implements IMessenger {
const RESULT_PREFIXES = [ const RESULT_PREFIXES = [
"failure" => ["(FAILURE)", "<color r>✘</color>"], "failure" => ["(FAILURE)", "<color r>✘</color>"],
"success" => ["(SUCCESS)", "<color @g>✔</color>"], "success" => ["(SUCCESS)", "<color @g>✔</color>"],
"neutral" => [null, null], "done" => [null, null],
]; ];
function __construct(?array $params=null) { function __construct(?array $params=null) {
$output = cl::get($params, "output"); $output = cl::get($params, "output");
$color = cl::get($params, "color"); $color = cl::get($params, "color");
$debug = boolval(cl::get($params, "debug")); $indent = cl::get($params, "indent", static::INDENT);
$defaultLevel = cl::get($params, "default_level"); $defaultLevel = cl::get($params, "default_level");
if ($defaultLevel === null) $defaultLevel = self::LEVEL_NORMAL; if ($defaultLevel === null) $defaultLevel = self::LEVEL_NORMAL;
$defaultLevel = self::verifix_level($defaultLevel); $defaultLevel = self::verifix_level($defaultLevel);
$debug = boolval(cl::get($params, "debug"));
$minLevel = cl::get($params, "min_level"); $minLevel = cl::get($params, "min_level");
if ($minLevel === null) $minLevel = $debug? self::LEVEL_DEBUG: self::LEVEL_NORMAL; if ($minLevel === null && $debug) $minLevel = self::LEVEL_DEBUG;
if ($minLevel === null) $minLevel = cl::get($params, "verbosity"); # alias
if ($minLevel === null) $minLevel = self::LEVEL_NORMAL;
$minLevel = self::verifix_level($minLevel, self::LEVEL_NONE); $minLevel = self::verifix_level($minLevel, self::LEVEL_NONE);
$params = [ $params = [
"color" => $color, "color" => $color,
"indent" => static::INDENT, "indent" => $indent,
]; ];
if ($output !== null) { if ($output !== null) {
$this->err = $this->out = new StdOutput($output, $params); $this->err = $this->out = new StdOutput($output, $params);
@ -108,6 +111,35 @@ class StdMessenger implements IMessenger {
$this->action = null; $this->action = null;
} }
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");
if ($minLevel === null && $debug !== null) $minLevel = $debug? self::LEVEL_DEBUG: self::LEVEL_NORMAL;
if ($minLevel === null) $minLevel = cl::get($params, "verbosity"); # alias
if ($minLevel !== null) $minLevel = self::verifix_level($minLevel, self::LEVEL_NONE);
$params = [
"output" => $output,
"color" => $color,
"indent" => $indent,
];
if ($output !== null) {
$this->out->resetParams($params);
} else {
$this->out->resetParams($params);
$this->err->resetParams($params);
}
if ($defaultLevel !== null) $this->defaultLevel = $defaultLevel;
if ($minLevel !== null) $this->minLevel = $minLevel;
}
/** @var StdOutput la sortie standard */ /** @var StdOutput la sortie standard */
protected $out; protected $out;
@ -173,7 +205,7 @@ class StdMessenger implements IMessenger {
$color = $out->isColor(); $color = $out->isColor();
if ($rsuccess === true) $type = "success"; if ($rsuccess === true) $type = "success";
elseif ($rsuccess === false) $type = "failure"; elseif ($rsuccess === false) $type = "failure";
else $type = "neutral"; else $type = "done";
$rprefixes = self::RESULT_PREFIXES[$type]; $rprefixes = self::RESULT_PREFIXES[$type];
if ($color) { if ($color) {
$rprefix = $rprefixes[1]; $rprefix = $rprefixes[1];
@ -219,9 +251,10 @@ class StdMessenger implements IMessenger {
$prefix = $prefix2; $prefix = $prefix2;
} }
} elseif ($printResult) { } elseif ($printResult) {
if (!$rcontent && $color) { if (!$rcontent) {
if ($rsuccess === true) $rcontent = "succès"; if ($type === "success") $rcontent = $color? "succès": "";
elseif ($rsuccess === false) $rcontent = "échec"; elseif ($type === "failure") $rcontent = $color? "échec": "";
elseif ($type === "done") $rcontent = "fait";
} }
$rprefix = " $rprefix"; $rprefix = " $rprefix";
$rprefix2 = " $rprefix2"; $rprefix2 = " $rprefix2";
@ -409,7 +442,7 @@ class StdMessenger implements IMessenger {
$this->_printGeneric($level, "step", $content, $this->getIndentLevel(), $this->err); $this->_printGeneric($level, "step", $content, $this->getIndentLevel(), $this->err);
} }
function success($content=null): void { function asuccess($content=null): void {
if (!$this->actions) $this->action(null); if (!$this->actions) $this->action(null);
$this->action["result_success"] = true; $this->action["result_success"] = true;
$this->action["result_content"] = $content; $this->action["result_content"] = $content;
@ -417,7 +450,7 @@ class StdMessenger implements IMessenger {
$this->endAction(); $this->endAction();
} }
function failure($content=null): void { function afailure($content=null): void {
if (!$this->actions) $this->action(null); if (!$this->actions) $this->action(null);
$this->action["result_success"] = false; $this->action["result_success"] = false;
$this->action["result_content"] = $content; $this->action["result_content"] = $content;
@ -425,7 +458,7 @@ class StdMessenger implements IMessenger {
$this->endAction(); $this->endAction();
} }
function neutral($content=null): void { function adone($content=null): void {
if (!$this->actions) $this->action(null); if (!$this->actions) $this->action(null);
$this->action["result_success"] = null; $this->action["result_success"] = null;
$this->action["result_content"] = $content; $this->action["result_content"] = $content;
@ -473,9 +506,9 @@ class StdMessenger implements IMessenger {
$this->_printGeneric($level, "error", $content, $this->getIndentLevel(), $this->err); $this->_printGeneric($level, "error", $content, $this->getIndentLevel(), $this->err);
} }
function end(bool $all=false) { function end(bool $all=false): void {
if ($all) { if ($all) {
while ($this->actions) $this->neutral(); while ($this->actions) $this->adone();
while ($this->titles) $this->endTitle(); while ($this->titles) $this->endTitle();
$this->endSection(); $this->endSection();
} elseif ($this->actions) { } elseif ($this->actions) {

View File

@ -1,8 +1,10 @@
<?php <?php
namespace nur\sery\output; namespace nur\sery\output\std;
use Exception; use Exception;
use nulib\cl; use nulib\cl;
use nur\sery\output\IContent;
use nur\sery\output\IPrintable;
/** /**
* Class StdOutput: affichage sur STDOUT, STDERR ou dans un fichier quelconque * Class StdOutput: affichage sur STDOUT, STDERR ou dans un fichier quelconque
@ -69,15 +71,22 @@ class StdOutput {
* échoué * échoué
*/ */
function __construct($output=null, ?array $params=null) { function __construct($output=null, ?array $params=null) {
if ($output === null) $output = cl::get($params, "output"); if ($output !== null) $params["output"] = $output;
elseif (!isset($params["output"])) $params["output"] = STDOUT;
if (!isset($params["filter_tags"])) $params["filter_tags"] = true;
if (!isset($params["indent"])) $params["indent"] = " ";
$this->resetParams($params);
}
function resetParams(?array $params=null): void {
$output = cl::get($params, "output");
$color = cl::get($params, "color"); $color = cl::get($params, "color");
$filterTags = cl::get($params, "filter_tags", true); $filterTags = cl::get($params, "filter_tags");
$indent = cl::get($params, "indent", " "); $indent = cl::get($params, "indent");
$flush = cl::get($params, "flush"); $flush = cl::get($params, "flush");
if ($output === null) { if ($output !== null) {
$outf = STDOUT; if ($output === "php://stdout") {
} elseif ($output === "php://stdout") {
$outf = STDOUT; $outf = STDOUT;
} elseif ($output === "php://stderr") { } elseif ($output === "php://stderr") {
$outf = STDERR; $outf = STDERR;
@ -95,14 +104,14 @@ class StdOutput {
} else { } else {
$outf = $output; $outf = $output;
} }
$this->outf = $outf;
if ($color === null) $color = stream_isatty($outf); if ($color === null) $color = stream_isatty($outf);
if ($flush === null) $flush = false; if ($flush === null) $flush = false;
}
$this->outf = $outf; if ($color !== null) $this->color = boolval($color);
$this->color = boolval($color); if ($filterTags !== null) $this->filterTags = boolval($filterTags);
$this->filterTags = boolval($filterTags); if ($indent !== null) $this->indent = strval($indent);
$this->indent = $indent; if ($flush !== null) $this->flush = boolval($flush);
$this->flush = boolval($flush);
} }
/** @var resource */ /** @var resource */
@ -195,7 +204,9 @@ class StdOutput {
function getLines(bool $withNl, ...$values): array { function getLines(bool $withNl, ...$values): array {
$values = self::flatten($values); $values = self::flatten($values);
if (!$values) return [];
$text = implode("", $values); $text = implode("", $values);
if ($text === "") return [""];
$text = $this->filterContent($text); $text = $this->filterContent($text);
if (!$this->color) $text = $this->filterColors($text); if (!$this->color) $text = $this->filterColors($text);
$lines = explode("\n", $text); $lines = explode("\n", $text);

View File

@ -2,7 +2,7 @@
<?php <?php
require(__DIR__.'/../vendor/autoload.php'); require(__DIR__.'/../vendor/autoload.php');
use nur\sery\output\StdMessenger; use nur\sery\output\std\StdMessenger;
$params = []; $params = [];
$count = count($argv) - 1; $count = count($argv) - 1;
@ -46,45 +46,59 @@ $c->print("print");
$c->action("action"); $c->action("action");
$c->step("step"); $c->step("step");
$c->success("action success"); $c->asuccess("action success");
$c->action("action"); $c->action("action");
$c->step("step"); $c->step("step");
$c->failure("action failure"); $c->afailure("action failure");
$c->action("action"); $c->action("action");
$c->success("action success"); $c->step("step");
$c->adone("action neutral");
$c->action("action"); $c->action("action");
$c->failure("action failure"); $c->asuccess("action success");
$c->action("action");
$c->afailure("action failure");
$c->action("action");
$c->adone("action neutral");
$c->action("action0"); $c->action("action0");
$c->action("action1"); $c->action("action1");
$c->action("action2"); $c->action("action2");
$c->success("action2 success"); $c->asuccess("action2 success");
$c->success("action1 success"); $c->asuccess("action1 success");
$c->success("action0 success"); $c->asuccess("action0 success");
$c->action("action"); $c->action("action");
$c->step("step"); $c->step("step");
$c->success(); $c->asuccess();
$c->action("action"); $c->action("action");
$c->step("step"); $c->step("step");
$c->failure(); $c->afailure();
$c->action("action"); $c->action("action");
$c->success(); $c->step("step");
$c->adone();
$c->action("action"); $c->action("action");
$c->failure(); $c->asuccess();
$c->action("action");
$c->afailure();
$c->action("action");
$c->adone();
$c->action("action0"); $c->action("action0");
$c->action("action1"); $c->action("action1");
$c->action("action2"); $c->action("action2");
$c->success(); $c->asuccess();
$c->success(); $c->asuccess();
$c->success(); $c->asuccess();
$c->info("info"); $c->info("info");
$c->note("note"); $c->note("note");
@ -108,11 +122,14 @@ $c->title("another\ntitle");
$c->print("multi-line\nprint"); $c->print("multi-line\nprint");
$c->info("multi-line\ninfo"); $c->info("multi-line\ninfo");
$c->action("multi-line\naction"); $c->action("multi-line\naction");
$c->success(); $c->asuccess();
$c->action("multi-line\naction"); $c->action("multi-line\naction");
$c->step("multi-line\nstep"); $c->step("multi-line\nstep");
$c->failure(); $c->afailure();
$c->action("multi-line\naction"); $c->action("multi-line\naction");
$c->step("multi-line\nstep"); $c->step("multi-line\nstep");
$c->success("multi-line\nsuccess"); $c->asuccess("multi-line\nsuccess");
$c->action("multi-line\naction");
$c->step("multi-line\nstep");
$c->adone("multi-line\ndone");
$c->end(true); $c->end(true);