2023-12-28 17:32:27 +04:00
|
|
|
<?php
|
|
|
|
namespace nulib\output;
|
|
|
|
|
|
|
|
use nulib\ValueException;
|
|
|
|
use nulib\output\std\ProxyMessenger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class msg: inscrire un message dans les logs ET l'afficher sur la console
|
2024-04-25 17:46:18 +04:00
|
|
|
*
|
|
|
|
* Cette classe DOIT être initialisée avec {@link set_messenger()} ou
|
|
|
|
* {@link set_messenger_class()} avant d'être utilisée. Une fois initialisée,
|
|
|
|
* les classes {@link say} et {@link log} sont utilisables aussi
|
2023-12-28 17:32:27 +04:00
|
|
|
*/
|
|
|
|
class msg extends _messenger {
|
|
|
|
static function set_messenger(IMessenger $say, ?IMessenger $log=null) {
|
|
|
|
self::$say = $say;
|
|
|
|
if ($log !== null) self::$log = $log;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|