ajout configuration de msg dans Application

This commit is contained in:
Jephté Clain 2024-05-02 10:01:48 +04:00
parent 54ce0ee270
commit 02df911059
5 changed files with 323 additions and 14 deletions

View File

@ -9,6 +9,10 @@ use nur\config\ArrayConfig;
use nur\msg;
use nur\os;
use nur\path;
use nur\sery\output\say as nsay;
use nur\sery\output\log as nlog;
use nur\sery\output\msg as nmsg;
use nur\sery\output\std\StdMessenger as nStdMessenger;
/**
* Class Application: application de base
@ -16,7 +20,7 @@ use nur\path;
abstract class Application {
protected static function _app_init(): void {
config::set_fact(config::FACT_CLI_APP);
\nur\sery\output\msg::set_messenger_class(\nur\sery\output\std\StdMessenger::class);
nmsg::set_messenger_class(nStdMessenger::class);
msg::set_messenger_class(Console::class);
msg::get()->setParametrableParams([
@ -98,7 +102,8 @@ abstract class Application {
const PROFILE_SECTION = [
"title" => "PROFILS D'EXECUTION",
["group",
["-p", "--profile", "args" => "value", "argsdesc" => "PROFILE",
["-p", "--profile", "--app-profile",
"args" => 1, "argsdesc" => "PROFILE",
"action" => [null, "set_application_profile"],
"help" => "spécifier le profil d'exécution",
],
@ -116,7 +121,8 @@ abstract class Application {
"title" => "NIVEAU D'INFORMATION",
"show" => false,
["group",
["--verbosity", "args" => "value", "argsdesc" => "silent|very-quiet|quiet|verbose|debug|trace",
["--verbosity",
"args" => 1, "argsdesc" => "silent|very-quiet|quiet|verbose|debug|trace",
"action" => [null, "set_application_verbosity"],
"help" => "spécifier le niveau d'informations affiché",
],
@ -125,7 +131,8 @@ abstract class Application {
["-D", "--debug", "action" => [null, "set_application_verbosity", "debug"]],
["--sql-trace", "action" => [null, "set_application_sql_trace"]],
],
["-L", "--logfile", "args" => "file", "argsdesc" => "OUTPUT",
["-L", "--logfile",
"args" => "file", "argsdesc" => "OUTPUT",
"action" => [null, "set_application_log_output"],
"help" => "Logger les messages de l'application dans le fichier spécifié",
],
@ -134,12 +141,13 @@ abstract class Application {
"action" => [null, "set_application_color", true],
"help" => "Afficher (resp. ne pas afficher) la sortie en couleur par défaut",
],
["--no-color", "action" => [null, "set_application_default_color", false]],
["--no-color", "action" => [null, "set_application_color", false]],
],
];
static function set_application_verbosity(string $verbosity): void {
$msg = msg::get();
$nsay = nsay::get();
switch ($verbosity) {
case "s":
case "silent":
@ -148,6 +156,9 @@ abstract class Application {
msg::TECH => msg::NEVER,
msg::EXCEPTION => msg::NEVER,
]);
$nsay->resetParams([
"min_level" => nmsg::NONE,
]);
break;
case "Q":
case "very-quiet":
@ -156,6 +167,9 @@ abstract class Application {
msg::TECH => msg::CRITICAL,
msg::EXCEPTION => msg::NEVER,
]);
$nsay->resetParams([
"min_level" => nmsg::MAJOR,
]);
break;
case "q":
case "quiet":
@ -164,6 +178,9 @@ abstract class Application {
msg::TECH => msg::MAJOR,
msg::EXCEPTION => msg::NEVER,
]);
$nsay->resetParams([
"min_level" => nmsg::MAJOR,
]);
break;
case "v":
case "verbose":
@ -172,6 +189,9 @@ abstract class Application {
msg::TECH => msg::MINOR,
msg::EXCEPTION => msg::NEVER,
]);
$nsay->resetParams([
"min_level" => nmsg::MINOR,
]);
break;
case "D":
case "debug":
@ -181,6 +201,9 @@ abstract class Application {
msg::TECH => msg::MINOR,
msg::EXCEPTION => msg::NORMAL,
], null, msg::DEBUG);
$nsay->resetParams([
"min_level" => nmsg::DEBUG,
]);
break;
case "T":
case "trace":
@ -190,6 +213,9 @@ abstract class Application {
msg::TECH => msg::MINOR,
msg::EXCEPTION => msg::MINOR,
], null, msg::DEBUG);
$nsay->resetParams([
"min_level" => nmsg::DEBUG,
]);
break;
default:
throw ValueException::invalid_value($verbosity, "verbosity");
@ -202,9 +228,22 @@ abstract class Application {
static function set_application_log_output(string $logfile): void {
msg::get()->setParametrableParams(["log_output" => $logfile]);
if (!nlog::have_log()) {
nlog::set_messenger_class(nStdMessenger::class, [
"output" => $logfile,
"add_date" => true,
]);
} else {
nlog::reset_params([
"output" => $logfile,
]);
}
}
static function set_application_default(bool $color): void {
static function set_application_color(bool $color): void {
msg::get()->setParametrableParams(["color" => $color]);
nsay::reset_params([
"color" => $color,
]);
}
const ARGS = [
"sections" => [

View File

@ -11,15 +11,21 @@ use nur\sery\output\std\ProxyMessenger;
* d'être utilisée
*/
class log extends _messenger {
static function have_log(): bool {
return self::$log !== null;
}
static function set_messenger(IMessenger $log=null) {
self::$log = $log;
// forcer la recréation de l'instance partagée $msg
self::$msg = null;
}
static function set_messenger_class(string $log_class=null, ?array $params=null) {
if (!is_subclass_of($log_class, IMessenger::class)) {
throw ValueException::invalid_class($log_class, IMessenger::class);
}
self::$log = new $log_class($params);
self::set_messenger(new $log_class($params));
}
static function get(): IMessenger {

View File

@ -12,22 +12,32 @@ use nur\sery\output\std\ProxyMessenger;
* les classes {@link say} et {@link log} sont utilisables aussi
*/
class msg extends _messenger {
static function set_messenger(IMessenger $say, ?IMessenger $log=null) {
self::$say = $say;
static function set_messenger(?IMessenger $say, ?IMessenger $log=null) {
if ($say !== null) self::$say = $say;
if ($log !== null) self::$log = $log;
if ($say !== null || $log !== null) {
// forcer la recréation de l'instance partagée $msg
self::$msg = null;
}
}
static function set_messenger_class(string $say_class, ?string $log_class=null) {
if (!is_subclass_of($say_class, IMessenger::class)) {
throw ValueException::invalid_class($say_class, IMessenger::class);
static function set_messenger_class(?string $say_class, ?string $log_class=null) {
if ($say_class !== null) {
if (!is_subclass_of($say_class, IMessenger::class)) {
throw ValueException::invalid_class($say_class, IMessenger::class);
}
self::$say = new $say_class();
}
self::$say = new $say_class();
if ($log_class !== null) {
if (!is_subclass_of($log_class, IMessenger::class)) {
throw ValueException::invalid_class($log_class, IMessenger::class);
}
self::$log = new $log_class();
}
if ($say_class !== null || $log_class !== null) {
// forcer la recréation de l'instance partagée $msg
self::$msg = null;
}
}
static function get(): IMessenger {

View File

@ -13,13 +13,15 @@ use nur\sery\output\std\ProxyMessenger;
class say extends _messenger {
static function set_messenger(IMessenger $say) {
self::$say = $say;
// forcer la recréation de l'instance partagée $msg
self::$msg = null;
}
static function set_messenger_class(string $say_class, ?array $params=null) {
if (!is_subclass_of($say_class, IMessenger::class)) {
throw ValueException::invalid_class($say_class, IMessenger::class);
}
self::$say = new $say_class($params);
self::set_messenger(new $say_class($params));
}
static function get(): IMessenger {

252
tbin/legacy-test-console.php Executable file
View File

@ -0,0 +1,252 @@
#!/usr/bin/php
<?php
require(__DIR__.'/../vendor/autoload.php');
use nur\cli\Application;
use nur\sery\output\msg;
use nur\sery\UserException;
Application::run(new class extends Application {
function main() {
msg::title("title0");
msg::title("title1");
msg::print("print under title1");
msg::end();
msg::print("print under title0");
msg::end();
msg::desc("action avec step");
msg::action("action avec step");
msg::step("step");
msg::asuccess("action success");
msg::action("action avec step");
msg::step("step");
msg::afailure("action failure");
msg::action("action avec step");
msg::step("step");
msg::adone("action neutral");
msg::desc("actions sans step");
msg::action("action sans step");
msg::asuccess("action success");
msg::action("action sans step");
msg::afailure("action failure");
msg::action("action sans step");
msg::adone("action neutral");
msg::desc("actions imbriquées");
msg::action("action0");
msg::action("action1");
msg::action("action2");
msg::asuccess("action2 success");
msg::asuccess("action1 success");
msg::asuccess("action0 success");
msg::desc("action avec step, sans messages");
msg::action("action avec step, sans messages, success");
msg::step("step");
msg::asuccess();
msg::action("action avec step, sans messages, failure");
msg::step("step");
msg::afailure();
msg::action("action avec step, sans messages, done");
msg::step("step");
msg::adone();
msg::desc("action sans step, sans messages");
msg::action("action sans step, sans messages, success");
msg::asuccess();
msg::action("action sans step, sans messages, failure");
msg::afailure();
msg::action("action sans step, sans messages, done");
msg::adone();
msg::desc("actions imbriquées, sans messages");
msg::action("action0");
msg::action("action1");
msg::action("action2");
msg::asuccess();
msg::asuccess();
msg::asuccess();
msg::info("info");
msg::note("note");
msg::warn("warn");
msg::error("error");
msg::section("section", function ($msg) {
$msg->title("title", function ($msg) {
$msg->desc("desc");
$msg->print("print");
$msg->desc("action avec step");
$msg->action("action avec step", function ($msg) {
$msg->step("step");
$msg->asuccess("action success");
});
$msg->action("action avec step", function ($msg) {
$msg->step("step");
$msg->afailure("action failure");
});
$msg->action("action avec step", function ($msg) {
$msg->step("step");
$msg->adone("action done");
});
$msg->desc("actions sans step");
$msg->action("action sans step", function ($msg) {
$msg->asuccess("action success");
});
$msg->action("action sans step", function ($msg) {
$msg->afailure("action failure");
});
$msg->action("action sans step", function ($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->asuccess("action2 success");
});
$msg->asuccess("action1 success");
});
$msg->asuccess("action0 success");
});
$msg->desc("action avec step, sans messages");
$msg->action("action avec step, sans messages, success", function ($msg) {
$msg->step("step");
$msg->asuccess();
});
$msg->action("action avec step, sans messages, failure", function ($msg) {
$msg->step("step");
$msg->afailure();
});
$msg->action("action avec step, sans messages, done", function ($msg) {
$msg->step("step");
$msg->adone();
});
$msg->desc("action sans step, sans messages");
$msg->action("action sans step, sans messages, success", function ($msg) {
$msg->asuccess();
});
$msg->action("action sans step, sans messages, failure", function ($msg) {
$msg->afailure();
});
$msg->action("action sans step, sans messages, done", function ($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->asuccess();
});
$msg->asuccess();
});
$msg->asuccess();
});
$msg->desc("action avec step, avec code de retour");
$msg->action("action avec step, avec code de retour true", function ($msg) {
$msg->step("step");
return true;
});
$msg->action("action avec step, avec code de retour false", function ($msg) {
$msg->step("step");
return false;
});
$msg->action("action avec step, avec code de retour autre", function ($msg) {
$msg->step("step");
return "autre";
});
$msg->action("action avec step, avec code de retour null", function ($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) {
return true;
});
$msg->action("action sans step, avec code de retour false", function ($msg) {
return false;
});
$msg->action("action sans step, avec code de retour autre", function ($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->info("info");
$msg->note("note");
$msg->warn("warn");
$msg->error("error");
});
});
msg::section("multi-line\nsection", function ($msg) {
$msg->title("multi-line\ntitle");
$msg->title("another\ntitle");
$msg->print("multi-line\nprint");
$msg->info("multi-line\ninfo");
$msg->action("multi-line\naction");
$msg->asuccess();
$msg->action("multi-line\naction");
$msg->step("multi-line\nstep");
$msg->afailure();
$msg->action("multi-line\naction");
$msg->step("multi-line\nstep");
$msg->asuccess("multi-line\nsuccess");
$msg->action("multi-line\naction");
$msg->step("multi-line\nstep");
$msg->adone("multi-line\ndone");
$msg->end();
$msg->end();
});
msg::section("Exceptions", function ($msg) {
$e = new Exception("message");
$u1 = new UserException("userMessage");
$u2 = new UserException("userMessage", "techMessage");
$msg->title("avec message", function ($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->info($e);
$msg->info($u1);
$msg->info($u2);
});
});
}
});