Small and simple Event Bus library.
Narrator allows communication between components without requiring the component to explicitly depend on each other.
Using Composer:
$ composer require narrator/narrator
// Simple event object
class UserRegistered {
private $userId;
private $userName;
// ...event data, constructor, getters
}
// Sample listener
class UserRegisteredListener implements Listener {
public function handle($event, Meta $meta){
// send email, update model, etc
}
}
// create EventBus which will be responsible for managing events and listeners
$eventBus = new BasicEventBus(new NameBasedResolver(new ClassNameExtractor()));
// create listener instance
$listener = new UserRegisteredListener(...);
// and register it in bus
$eventBus->subscribe(UserRegistered::class, $listener);
// create event
$event = new UserRegistered(...);
// and `emit` it to listeners
$eventBus->emit($event);
To run unit tests use PHPUnit
$ ./vendor/bin/phpunit