nulib/php/src_output/msg.php

42 lines
1.2 KiB
PHP
Raw Normal View History

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
*/
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;
}
}