azurre / php-event-dispatcher
EventDispatcher Component
0.2
2020-06-25 16:21 UTC
Requires
- php: >=7.2.0
- psr/event-dispatcher: ^1.0
This package is auto-updated.
Last update: 2024-11-26 02:25:39 UTC
README
Simple implementation of Event Dispatcher
Installation
Install composer in your project:
curl -s https://getcomposer.org/installer | php
Require the package with composer:
composer require azurre/php-event-dispatcher
Usage
The Dispatcher
$loader = require_once __DIR__ . '/vendor/autoload.php'; use Azurre\EventDispatcher\Dispatcher; use Azurre\EventDispatcher\Event; $dispatcher = new Dispatcher(); $dispatcher->attachListener(new Logger()); // ... // Do something // ... $event = new Event('my.event', new MyObject()); $dispatcher->dispatch($event); $someData = $event->getData('some_data');
Listener
class Logger implements \Azurre\EventDispatcher\ListenerInterface { public function getKey(): string { return 'my.event.logger'; } public function getListenedEvents(): array { return ['my.event', 'my.another.event']; } public function execute(\Azurre\EventDispatcher\EventInterface $event): void { switch ($event->getKey()) { case 'my.event': // Modify data $data = $event->getData(); $data->setIsLogged(true); $event->setData('some_data', [1,2,3]); break; case 'my.another.event': // Do another things break; } } }