nulib-base/php/src/output/std/ProxyMessenger.php

221 lines
5.2 KiB
PHP

<?php
namespace nulib\output\std;
use nulib\output\IMessenger;
/**
* Class ProxyMessenger: un proxy vers ou un plusieurs instances de IMessenger
*
* NB: si cette classe est instanciée sans argument, elle agit comme
* {@link NullMessenger}: elle envoie tous les messages vers /dev/null
*/
class ProxyMessenger implements _IMessenger {
function __construct(?IMessenger ...$msgs) {
foreach ($msgs as $msg) {
if ($msg !== null) $this->msgs[] = $msg;
}
}
/** @var _IMessenger[] */
protected ?array $msgs = [];
function isEmpty(): bool {
return !$this->msgs;
}
function addMessenger(IMessenger $msg): self {
$this->msgs[] = $msg;
return $this;
}
function resetParams(?array $params=null): void {
foreach ($this->msgs as $msg) {
$msg->resetParams($params);
}
}
function clone(?array $params=null): self {
$clone = clone $this;
foreach ($clone->msgs as &$msg) {
$msg = $msg->clone($params);
}; unset($msg);
return $clone;
}
function section__afterFunc(): void {
foreach ($this->msgs as $msg) {
if ($msg instanceof _IMessenger) {
$msg->section__afterFunc();
}
}
}
function section($content, ?callable $func=null, ?int $level=null): void {
foreach ($this->msgs as $msg) {
$msg->section($content, null, $level);
}
if ($func !== null) {
try {
$func($this);
} finally {
$this->section__afterFunc();
}
}
}
function title__getMarks(): array {
$marks = [];
foreach ($this->msgs as $key => $msg) {
if ($msg instanceof _IMessenger) {
$marks[$key] = $msg->title__getMarks();
}
}
return $marks;
}
function title__beforeFunc(array $marks): void {
foreach ($this->msgs as $key => $msg) {
if ($msg instanceof _IMessenger) {
$msg->title__beforeFunc($marks[$key]);
}
}
}
function title__afterFunc(array $marks): void {
foreach ($this->msgs as $key => $msg) {
if ($msg instanceof _IMessenger) {
$msg->title__afterFunc($marks[$key]);
}
}
}
function title($content, ?callable $func=null, ?int $level=null): void {
$marks = $this->title__getMarks();
foreach ($this->msgs as $msg) {
$msg->title($content, null, $level);
}
if ($func !== null) {
try {
$this->title__beforeFunc($marks);
$func($this);
} finally {
$this->title__afterFunc($marks);
}
}
}
function desc($content, ?int $level=null): void {
foreach ($this->msgs as $msg) {
$msg->desc($content, $level);
}
}
function action__getMarks(): array {
$marks = [];
foreach ($this->msgs as $key => $msg) {
if ($msg instanceof _IMessenger) {
$marks[$key] = $msg->action__getMarks();
}
}
return $marks;
}
function action__beforeFunc(array $marks): void {
foreach ($this->msgs as $key => $msg) {
if ($msg instanceof _IMessenger) {
$msg->action__beforeFunc($marks[$key]);
}
}
}
function action__afterFunc(array $marks, $result): void {
foreach ($this->msgs as $key => $msg) {
if ($msg instanceof _IMessenger) {
$msg->action__afterFunc($marks[$key], $result);
}
}
}
function action($content, ?callable $func=null, ?int $level=null): void {
$marks = $this->action__getMarks();
foreach ($this->msgs as $msg) {
$msg->action($content, null, $level);
}
if ($func !== null) {
try {
$result = null;
$this->action__beforeFunc($marks);
$result = $func($this);
} finally {
$this->action__afterFunc($marks, $result);
}
}
}
function step($content, ?int $level=null): void {
foreach ($this->msgs as $msg) {
$msg->step($content, $level);
}
}
function asuccess($content=null, ?int $overrideLevel=null): void {
foreach ($this->msgs as $msg) {
$msg->asuccess($content, $overrideLevel);
}
}
function afailure($content=null, ?int $overrideLevel=null): void {
foreach ($this->msgs as $msg) {
$msg->afailure($content, $overrideLevel);
}
}
function adone($content=null, ?int $overrideLevel=null): void {
foreach ($this->msgs as $msg) {
$msg->adone($content, $overrideLevel);
}
}
function aresult($result=null, ?int $overrideLevel=null): void {
foreach ($this->msgs as $msg) {
$msg->aresult($result, $overrideLevel);
}
}
function print($content, ?int $level=null): void {
foreach ($this->msgs as $msg) {
$msg->print($content, $level);
}
}
function info($content, ?int $level=null): void {
foreach ($this->msgs as $msg) {
$msg->info($content, $level);
}
}
function note($content, ?int $level=null): void {
foreach ($this->msgs as $msg) {
$msg->note($content, $level);
}
}
function warning($content, ?int $level=null): void {
foreach ($this->msgs as $msg) {
$msg->warning($content, $level);
}
}
function error($content, ?int $level=null): void {
foreach ($this->msgs as $msg) {
$msg->error($content, $level);
}
}
function end(bool $all=false): void {
foreach ($this->msgs as $msg) {
$msg->end($all);
}
}
}