/
jagepard
/
PhpDesignPatterns-Mediator
Обзор
Документация
Войти
/
jagepard
/
PhpDesignPatterns-Mediator
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/Mediator.php
50 строк
2 KB
Jagepard
fix
06 май 2026, 16:48
06 май 2026, 16:48
e136d47
Код
Авторство
О чём код?
<?php declare(strict_types=1); /** * @author : Jagepard <jagepard@yandex.ru> * @license https://mit-license.org/ MIT */ namespace Behavioral\Mediator; class Mediator implements MediatorInterface { private array $listeners = []; /** * Adds the name of the listener class and the method to be called when dispatch is called * --------------------------------------------------------------------------------------- * Добавляет имя класса слушателя и метод, который будет вызван при обращении dispatch * * @param string $listenerName * @param string $methodName * @return void */ public function addListener(string $listenerName, string $methodName): void { if (array_key_exists($listenerName, $this->listeners)) { throw new \InvalidArgumentException("Listener already exists"); } $this->listeners[$listenerName] = [$listenerName, $methodName]; } /** * Calls the installed addListener listener method * -------------------------------------------------- * Вызывает установленный addListener метод слушателя * * @param string $listenerName * @return void */ public function dispatch(string $listenerName) { if (array_key_exists($listenerName, $this->listeners)) { return (new $this->listeners[$listenerName][0])->{$this->listeners[$listenerName][1]}(); } throw new \InvalidArgumentException("Listener " . $listenerName . " does not exist"); } }