narrator/narrator

Event library

v0.4 2017-03-20 18:44 UTC

README

Travis CI Scrutinizer Code Quality Code Coverage

Small and simple Event Bus library.

Narrator allows communication between components without requiring the component to explicitly depend on each other.

Installation

Using Composer:

$ composer require narrator/narrator

Basic usage

// 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);

Testing

To run unit tests use PHPUnit

$ ./vendor/bin/phpunit