modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-06-11 15:56:54 +04:00
parent b5b901080c
commit 291b478d93
8 changed files with 186 additions and 131 deletions

View File

@ -12,8 +12,8 @@ use nur\path;
use nur\sery\app\app; use nur\sery\app\app;
use nur\sery\output\log as nlog; use nur\sery\output\log as nlog;
use nur\sery\output\msg as nmsg; use nur\sery\output\msg as nmsg;
use nur\sery\output\say as nsay; use nur\sery\output\console as nconsole;
use nur\sery\output\std\StdMessenger as nStdMessenger; use nur\sery\output\std\StdMessenger;
/** /**
* Class Application: application de base * Class Application: application de base
@ -43,6 +43,9 @@ abstract class Application {
const VARDIR = null; const VARDIR = null;
const LOGDIR = null; const LOGDIR = null;
/** @var bool faut-il activer automatiquement l'écriture dans les logs */
const USE_LOGFILE = null;
/** @var bool faut-il maintenir un fichier de suivi du process? */ /** @var bool faut-il maintenir un fichier de suivi du process? */
const USE_RUNFILE = false; const USE_RUNFILE = false;
@ -55,20 +58,16 @@ abstract class Application {
const USE_RUNLOCK = false; const USE_RUNLOCK = false;
protected static function _app_init(): void { protected static function _app_init(): void {
app::init(static::class);
config::set_fact(config::FACT_CLI_APP); config::set_fact(config::FACT_CLI_APP);
nmsg::set_messenger_class(nStdMessenger::class); # avant que l'application soit configurée, configurer le mode debug
nmsg::reset_params(["min_level" => nmsg::DEBUG]);
msg::set_messenger_class(Console::class); msg::set_messenger_class(Console::class);
msg::get()->setParametrableParams([ msg::get()->setParametrableParams([
# En ligne de commande, on peut afficher les messages loggués # En ligne de commande, on peut afficher les messages loggués
"display_log" => true, "display_log" => true,
]); ]);
# avant que l'application soit configurée, rester en mode debug
msg::get()->setLevels(msg::DEBUG_LEVELS, msg::DEBUG_LEVELS, msg::DEBUG); msg::get()->setLevels(msg::DEBUG_LEVELS, msg::DEBUG_LEVELS, msg::DEBUG);
# si un fichier nommé .default-profile-devel existe dans le répertoire de # si un fichier nommé .default-profile-devel existe dans le répertoire de
# l'application ou du projet, alors le profil par défaut est devel # l'application ou du projet, alors le profil par défaut est devel
global $argv; global $argv;
@ -85,6 +84,11 @@ abstract class Application {
$projdir = path::dirname($projdir); $projdir = path::dirname($projdir);
if ($projdir == "/") break; if ($projdir == "/") break;
} }
app::init(static::class);
nmsg::set_messenger(new StdMessenger([
"min_level" => nmsg::DEBUG,
]));
} }
protected static function _app_configure(Application $app): void { protected static function _app_configure(Application $app): void {
@ -92,7 +96,18 @@ abstract class Application {
# revenir à la configuration par défaut une fois que la configuration # revenir à la configuration par défaut une fois que la configuration
# initiale est faite # initiale est faite
msg::get()->setLevels(msg::PRINT_LEVELS, msg::LOG_LEVELS, msg::TYPE_LEVELS); msg::get()->setLevels(msg::PRINT_LEVELS, msg::LOG_LEVELS, msg::TYPE_LEVELS);
nsay::reset_params(["min_level" => nmsg::NORMAL]);
$msgs = ["console" => new StdMessenger([
"min_level" => nmsg::NORMAL,
])];
if (static::USE_LOGFILE) {
$msgs["log"] = new StdMessenger([
"output" => app::get()->getLogfile(),
"min_level" => nmsg::MINOR,
"add_date" => true,
]);
}
nmsg::init($msgs);
$app->parseArgs(); $app->parseArgs();
config::configure(); config::configure();
@ -213,7 +228,7 @@ abstract class Application {
static function set_application_verbosity(string $verbosity): void { static function set_application_verbosity(string $verbosity): void {
$msg = msg::get(); $msg = msg::get();
$nsay = nsay::get(); $nconsole = nconsole::get();
switch ($verbosity) { switch ($verbosity) {
case "s": case "s":
case "silent": case "silent":
@ -222,7 +237,7 @@ abstract class Application {
msg::TECH => msg::NEVER, msg::TECH => msg::NEVER,
msg::EXCEPTION => msg::NEVER, msg::EXCEPTION => msg::NEVER,
]); ]);
$nsay->resetParams([ $nconsole->resetParams([
"min_level" => nmsg::NONE, "min_level" => nmsg::NONE,
]); ]);
break; break;
@ -233,7 +248,7 @@ abstract class Application {
msg::TECH => msg::CRITICAL, msg::TECH => msg::CRITICAL,
msg::EXCEPTION => msg::NEVER, msg::EXCEPTION => msg::NEVER,
]); ]);
$nsay->resetParams([ $nconsole->resetParams([
"min_level" => nmsg::MAJOR, "min_level" => nmsg::MAJOR,
]); ]);
break; break;
@ -244,7 +259,7 @@ abstract class Application {
msg::TECH => msg::MAJOR, msg::TECH => msg::MAJOR,
msg::EXCEPTION => msg::NEVER, msg::EXCEPTION => msg::NEVER,
]); ]);
$nsay->resetParams([ $nconsole->resetParams([
"min_level" => nmsg::MAJOR, "min_level" => nmsg::MAJOR,
]); ]);
break; break;
@ -255,7 +270,7 @@ abstract class Application {
msg::TECH => msg::MINOR, msg::TECH => msg::MINOR,
msg::EXCEPTION => msg::NEVER, msg::EXCEPTION => msg::NEVER,
]); ]);
$nsay->resetParams([ $nconsole->resetParams([
"min_level" => nmsg::MINOR, "min_level" => nmsg::MINOR,
]); ]);
break; break;
@ -267,7 +282,7 @@ abstract class Application {
msg::TECH => msg::MINOR, msg::TECH => msg::MINOR,
msg::EXCEPTION => msg::NORMAL, msg::EXCEPTION => msg::NORMAL,
], null, msg::DEBUG); ], null, msg::DEBUG);
$nsay->resetParams([ $nconsole->resetParams([
"min_level" => nmsg::DEBUG, "min_level" => nmsg::DEBUG,
]); ]);
break; break;
@ -279,7 +294,7 @@ abstract class Application {
msg::TECH => msg::MINOR, msg::TECH => msg::MINOR,
msg::EXCEPTION => msg::MINOR, msg::EXCEPTION => msg::MINOR,
], null, msg::DEBUG); ], null, msg::DEBUG);
$nsay->resetParams([ $nconsole->resetParams([
"min_level" => nmsg::DEBUG, "min_level" => nmsg::DEBUG,
]); ]);
break; break;
@ -296,14 +311,14 @@ abstract class Application {
msg::get()->setParametrableParams(["log_output" => $logfile]); msg::get()->setParametrableParams(["log_output" => $logfile]);
nlog::create_or_reset_params([ nlog::create_or_reset_params([
"output" => $logfile, "output" => $logfile,
], nStdMessenger::class, [ ], StdMessenger::class, [
"add_date" => true, "add_date" => true,
"min_level" => nlog::MINOR, "min_level" => nlog::MINOR,
]); ]);
} }
static function set_application_color(bool $color): void { static function set_application_color(bool $color): void {
msg::get()->setParametrableParams(["color" => $color]); msg::get()->setParametrableParams(["color" => $color]);
nsay::reset_params([ nconsole::reset_params([
"color" => $color, "color" => $color,
]); ]);
} }

View File

@ -2,8 +2,8 @@
namespace nur\sery\app; namespace nur\sery\app;
use nur\cli\Application; use nur\cli\Application;
use nur\os;
use nur\sery\os\path; use nur\sery\os\path;
use nur\sery\os\sh;
use nur\sery\ValueException; use nur\sery\ValueException;
class app { class app {
@ -27,6 +27,8 @@ class app {
$etcdir = constant("$app::ETCDIR"); $etcdir = constant("$app::ETCDIR");
$vardir = constant("$app::VARDIR"); $vardir = constant("$app::VARDIR");
$logdir = constant("$app::LOGDIR"); $logdir = constant("$app::LOGDIR");
} elseif (is_array($app)) {
return new static($app);
} else { } else {
throw ValueException::invalid_type($app, Application::class); throw ValueException::invalid_type($app, Application::class);
} }
@ -202,7 +204,9 @@ class app {
} }
function getVarfile(string $name, $profile=null): string { function getVarfile(string $name, $profile=null): string {
return $this->withProfile($this->fencedJoin($this->vardir, $name), $profile); $file = $this->withProfile($this->fencedJoin($this->vardir, $name), $profile);
sh::mkdirof($file);
return $file;
} }
protected string $logdir; protected string $logdir;
@ -211,8 +215,11 @@ class app {
return $this->logdir; return $this->logdir;
} }
function getLogfile(string $name, $profile=null): string { function getLogfile(?string $name=null, $profile=null): string {
return $this->withProfile($this->fencedJoin($this->logdir, $name), $profile); if ($name === null) $name = "{$this->name}.log";
$file = $this->withProfile($this->fencedJoin($this->logdir, $name), $profile);
sh::mkdirof($file);
return $file;
} }
/** /**
@ -233,7 +240,7 @@ class app {
$file = path::reljoin($this->datadir, $file); $file = path::reljoin($this->datadir, $file);
} }
$file = $this->withProfile($file, $profile); $file = $this->withProfile($file, $profile);
if ($ensureDir) os::mkdirof($file); if ($ensureDir) sh::mkdirof($file);
return $file; return $file;
} }

View File

@ -1,44 +1,39 @@
<?php <?php
namespace nur\sery\output; namespace nur\sery\output;
use nur\sery\cl;
use nur\sery\str; use nur\sery\str;
use nur\sery\ValueException;
/** /**
* Class _messenger: classe de base pour say, log et msg * Class _messenger: classe de base pour say, log et msg
*/ */
abstract class _messenger { abstract class _messenger {
/** @var IMessenger */ abstract static function is_setup(): bool;
protected static $say; abstract static function set_messenger(IMessenger $msg);
/** @var IMessenger */
protected static $log;
abstract static function get(): IMessenger; abstract static function get(): IMessenger;
static function set_messenger_class(string $msg_class, ?array $params=null) {
if (!is_subclass_of($msg_class, IMessenger::class)) {
throw ValueException::invalid_class($msg_class, IMessenger::class);
}
static::set_messenger(new $msg_class($params));
}
static function create_or_reset_params(?array $params=null, string $log_class=null, ?array $create_params=null) {
if (static::is_setup()) {
self::reset_params($params);
} else {
$params = cl::merge($params, $create_params);
self::set_messenger_class($log_class, $params);
}
}
/** obtenir une nouvelle instance, avec un nouveau paramétrage */ /** obtenir une nouvelle instance, avec un nouveau paramétrage */
static function new(?array $params=null): IMessenger { static function new(?array $params=null): IMessenger {
return static::get()->clone($params); 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) { static final function __callStatic($name, $args) {
$name = str::us2camel($name); $name = str::us2camel($name);
call_user_func_array([static::get(), $name], $args); call_user_func_array([static::get(), $name], $args);

29
src/output/console.php Normal file
View File

@ -0,0 +1,29 @@
<?php
namespace nur\sery\output;
use nur\sery\output\std\ProxyMessenger;
use nur\sery\ValueException;
/**
* Class console: afficher un message sur la console
*
* Cette classe DOIT être initialisée avant d'être utilisée
*/
class console extends _messenger {
private static ?IMessenger $msg = null;
private static bool $setup = false;
static function is_setup(): bool {
return self::$setup;
}
static function set_messenger(IMessenger $msg) {
self::$msg = $msg;
self::$setup = true;
}
static function get(): IMessenger {
return self::$msg ??= new ProxyMessenger();
}
}

View File

@ -8,42 +8,23 @@ use nur\sery\ValueException;
/** /**
* Class log: inscrire un message dans les logs uniquement * Class log: inscrire un message dans les logs uniquement
* *
* Cette classe (ou la classe parallèle {@link msg} DOIT être initialisée avant * Cette classe DOIT être initialisée avant d'être utilisée
* d'être utilisée
*/ */
class log extends _messenger { class log extends _messenger {
static function set_messenger(IMessenger $log=null) { private static ?IMessenger $msg = null;
self::$log = $log;
// forcer la recréation de l'instance partagée $msg private static bool $setup = false;
self::$msg = null;
static function is_setup(): bool {
return self::$setup;
} }
static function set_messenger_class(string $log_class=null, ?array $params=null) { static function set_messenger(IMessenger $msg) {
if (!is_subclass_of($log_class, IMessenger::class)) { self::$msg = $msg;
throw ValueException::invalid_class($log_class, IMessenger::class); self::$setup = true;
}
self::set_messenger(new $log_class($params));
} }
static function get(): IMessenger { static function get(): IMessenger {
if (self::$msg === null) { return self::$msg ??= new ProxyMessenger();
$msg = self::$log;
if ($msg === null) $msg = new ProxyMessenger();
self::$msg = $msg;
}
return self::$msg;
}
static function have_log(): bool {
return self::$log !== null;
}
static function create_or_reset_params(?array $params=null, string $log_class=null, ?array $create_params=null) {
if (self::$log === null) {
$params = cl::merge($params, $create_params);
self::set_messenger_class($log_class, $params);
} else {
self::reset_params($params);
}
} }
} }

View File

@ -2,54 +2,76 @@
namespace nur\sery\output; namespace nur\sery\output;
use nur\sery\output\std\ProxyMessenger; use nur\sery\output\std\ProxyMessenger;
use nur\sery\php\func;
use nur\sery\ValueException; use nur\sery\ValueException;
/** /**
* Class msg: inscrire un message dans les logs ET l'afficher sur la console * Class msg: inscrire un message dans les logs ET l'afficher sur la console
* *
* Cette classe DOIT être initialisée avec {@link set_messenger()} ou * Cette classe DOIT être initialisée avec {@link set_messenger()} ou
* {@link set_messenger_class()} avant d'être utilisée. Une fois initialisée, * {@link set_messenger_class()} avant d'être utilisée.
* les classes {@link say} et {@link log} sont utilisables aussi
*/ */
class msg extends _messenger { class msg extends _messenger {
static function set_messenger(?IMessenger $say, ?IMessenger $log=null) { private static ?IMessenger $msg = null;
if ($say !== null) self::$say = $say;
if ($log !== null) self::$log = $log; private static bool $setup = false;
if ($say !== null || $log !== null) {
// forcer la recréation de l'instance partagée $msg static function is_setup(): bool {
self::$msg = null; return self::$setup;
}
} }
static function set_messenger_class(?string $say_class, ?string $log_class=null) { static function set_messenger(IMessenger $msg) {
if ($say_class !== null) { self::$msg = $msg;
if (!is_subclass_of($say_class, IMessenger::class)) { self::$setup = true;
throw ValueException::invalid_class($say_class, IMessenger::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 { static function get(): IMessenger {
if (self::$msg === null) { return self::$msg ??= new ProxyMessenger();
$log = self::$log; }
$say = self::$say;
if ($log !== null && $say !== null) $msg = new ProxyMessenger($log, $say); /**
elseif ($log !== null) $msg = $log; * initialiser les instances say, console, log.
elseif ($say !== null) $msg = $say; */
else $msg = new ProxyMessenger(); static function init(array $msgs) {
self::$msg = $msg; $say = $msgs["say"] ?? null;
$console = $msgs["console"] ?? null;
$log = $msgs["log"] ?? null;
$msgs = [];
if ($log !== null && $log !== false) {
if ($log instanceof IMessenger) log::set_messenger($log);
elseif (is_string($log)) log::set_messenger_class($log);
elseif (is_array($log)) {
func::ensure_class($log, $args);
$log = func::cons($log, $args);
}
log::set_messenger($log);
$msgs[] = $log;
} }
return self::$msg; if ($console !== null && $console !== false) {
if ($console instanceof IMessenger) console::set_messenger($console);
elseif (is_string($console)) console::set_messenger_class($console);
elseif (is_array($console)) {
func::ensure_class($console, $args);
$console = func::cons($console, $args);
}
console::set_messenger($console);
$msgs[] = $console;
}
if ($say !== null && $say !== false) {
if ($say instanceof IMessenger) say::set_messenger($say);
elseif (is_string($say)) say::set_messenger_class($say);
elseif (is_array($say)) {
func::ensure_class($say, $args);
$say = func::cons($say, $args);
}
say::set_messenger($say);
$msgs[] = $say;
}
if ($say === null && $console !== null) {
say::set_messenger($console);
} elseif ($console === null && $say !== null) {
console::set_messenger($say);
}
self::set_messenger(new ProxyMessenger(...$msgs));
} }
} }

View File

@ -5,31 +5,25 @@ use nur\sery\output\std\ProxyMessenger;
use nur\sery\ValueException; use nur\sery\ValueException;
/** /**
* Class say: afficher un message sur la console uniquement * Class say: afficher un message pour l'utilisateur
* *
* Cette classe (ou la classe parallèle {@link msg} DOIT être initialisée avant * Cette classe DOIT être initialisée avant d'être utilisée
* d'être utilisée
*/ */
class say extends _messenger { class say extends _messenger {
static function set_messenger(IMessenger $say) { private static ?IMessenger $msg = null;
self::$say = $say;
// forcer la recréation de l'instance partagée $msg private static bool $setup = false;
self::$msg = null;
static function is_setup(): bool {
return self::$setup;
} }
static function set_messenger_class(string $say_class, ?array $params=null) { static function set_messenger(IMessenger $msg) {
if (!is_subclass_of($say_class, IMessenger::class)) { self::$msg = $msg;
throw ValueException::invalid_class($say_class, IMessenger::class); self::$setup = true;
}
self::set_messenger(new $say_class($params));
} }
static function get(): IMessenger { static function get(): IMessenger {
if (self::$msg === null) { return self::$msg ??= new ProxyMessenger();
$msg = self::$say;
if ($msg === null) $msg = new ProxyMessenger();
self::$msg = $msg;
}
return self::$msg;
} }
} }

View File

@ -418,6 +418,18 @@ class func {
return false; return false;
} }
/**
* Comme {@link check_class()} mais lance une exception si la classe est
* invalide
*
* @throws ValueException si $class n'est pas une classe valide
*/
static final function ensure_class(&$class, &$args=null): void {
if (!self::check_class($class, $args)) {
throw ValueException::invalid_type($class, "class");
}
}
/** /**
* Instancier la classe avec les arguments spécifiés. * Instancier la classe avec les arguments spécifiés.
* Adapter $args en fonction du nombre réel d'arguments du constructeur * Adapter $args en fonction du nombre réel d'arguments du constructeur