bee4/events

Basic event dispatcher definition, implement basic Mediator pattern

v1.1.0 2016-06-06 09:55 UTC

README

Build Status Scrutinizer Code Quality Code Coverage SensiolabInsight

License

The main goal of this code is to allow using Event Dispatcher pattern with different popular implementations :

This library does not intend to provide the whole possibilities of each adapters but a standard couple of interface which allow to do not depend from one vendor. This way, you can use your preferred event system with one of the bee4/events lib user.

Installing

Latest Stable Version Total Downloads

This project can be installed using Composer. Add the following to your composer.json:

{
    "require": {
        "bee4/events": "~1.1"
    }
}

or run this command:

composer require bee4/events:~1.1

Event System

DispatcherInterface

Define how an object must trigger an event. It contains 4 methods :

  • dispatch to trigger an EventInterface instance
  • on to attach a listener
  • once to attach a listener that will be ran then detached
  • remove to remove a given listener
  • get to retrieve all listeners attached to one event name

###DispatcherAwareInterface Define how an object can rely to a dispatcher to handle events. It contains 4 methods :

  • setDispatcher to initialize the current DispatcherInterface
  • getDispatcher to retrieve the current DispatcherInterface
  • hasDispatcher to check if there is a current DispatcherInterface
  • dispatch to dispatch an EventInterface on the link DispatcherInterface if there is one...

EventInterface

Define an event object which can be triggered. There is no default behaviour for this kind of object because an event can be really specific.

Adapters

I hope you don't want to create your own dispatcher because there are some cool stuff overhere. There are adapters classes located in the Bee4\Events\Adapters namespace :

<?php
$vendor = new \Symfony\Component\EventDispatcher\EventDispatcher;
$adapter = new \Bee4\Events\Adapters\SymfonyEventDispatcherAdapter($vendor);

$adapter->on('name', function(EventInterface $event) {
	echo "I have been triggered: ".PHP_EOL.print_r($event, true);
});

//EventImplementation must be defined in your project to suit your needs
//If must implements the `EventInterface` contract
$adapter->dispatch('name', new EventImplementation);