kornheiser / events
Eventing, Handling
v1.1.0
2022-01-24 22:41 UTC
Requires
- php: ^8.0
This package is not auto-updated.
Last update: 2024-12-14 09:52:23 UTC
README
Installation
composer require kornheiser/events
Example
Make ur own Event
use Kornheiser\Event\Event; class NewMessageEvent implements Event { public function __construct( public string $text, public int $time, public string $senderMail ): void {} }
Handling
Simple handler
use Kornheiser\Event\Handler; class NewMessageHandler implements Handler { public function onHandle(Event $ev): void { var_dump($ev->text, $ev->time, $ev->senderMail); } }
Callable handler
use Kornheiser\Event\CallableHandler; $callableHandler = new CallableHandler(function(Event $ev) { echo $ev->senderMail . ": " . $ev->text; });
Targeted handler
A targeted handler, unlike a regular handler, only targets a once specific event, see:
use Kornheiser\Event\TargetedHandler; $handler1 = new TargetedHandler(NewMessageEvent::class, new NewMessageHandler()); $handler2 = new TargetedHandler(NewMessageEvent::class, $callableHandler);
Conclusion
use Kornheiser\Event\EventManager; $invoker = new EventManager(); $invoker->addHandler($handler1); $invoker->addHandler($handler2); $invoker->call(new NewMessageEvent(text: "hello world", time: time(), senderMail: "some programmer"));