nulib/php/src_output/std/ProxyMessenger.php

101 lines
3.5 KiB
PHP

<?php
namespace nulib\output\std;
use nulib\output\IMessenger;
/**
* Class ProxyMessenger: un proxy vers ou un plusieurs instances de IMessenger
*/
class ProxyMessenger implements IMessenger {
function __construct(?IMessenger ...$msgs) {
$this->msgs = [];
foreach ($msgs as $msg) {
if ($msg !== null) $this->msgs[] = $msg;
}
}
/** @var IMessenger[] */
protected $msgs;
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($content, ?callable $func=null, ?int $level=null): void {
$useFunc = false;
foreach ($this->msgs as $msg) {
$msg->section($content, null, $level);
if ($msg instanceof _IMessenger) $useFunc = true;
}
if ($useFunc && $func !== null) {
try {
$func($this);
} finally {
/** @var _IMessenger $msg */
foreach ($this->msgs as $msg) {
$msg->_endSection();
}
}
}
}
function title($content, ?callable $func=null, ?int $level=null): void {
$useFunc = false;
$untils = [];
foreach ($this->msgs as $msg) {
$msg->title($content, null, $level);
if ($msg instanceof _IMessenger) {
$useFunc = true;
$untils[] = $msg->_getTitleMark();
}
}
if ($useFunc && $func !== null) {
try {
$func($this);
} finally {
/** @var _IMessenger $msg */
$index = 0;
foreach ($this->msgs as $msg) {
$msg->_endTitle($untils[$index++]);
}
}
}
}
function desc($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->desc($content, $level); } }
function action($content, ?callable $func=null, ?int $level=null): void {
$useFunc = false;
$untils = [];
foreach ($this->msgs as $msg) {
$msg->action($content, null, $level);
if ($msg instanceof _IMessenger) {
$useFunc = true;
$untils[] = $msg->_getTitleMark();
}
}
if ($useFunc && $func !== null) {
try {
$func($this);
} finally {
/** @var _IMessenger $msg */
$index = 0;
foreach ($this->msgs as $msg) {
$msg->_endAction($untils[$index++]);
}
}
}
}
function step($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->step($content, $level); } }
function asuccess($content=null): void { foreach ($this->msgs as $msg) { $msg->asuccess($content); } }
function afailure($content=null): void { foreach ($this->msgs as $msg) { $msg->afailure($content); } }
function adone($content=null): void { foreach ($this->msgs as $msg) { $msg->adone($content); } }
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 warn($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->warn($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); } }
}