The main goal of this code is to allow using Event Dispatcher pattern with different popular implementations :
- Symfony 2 Event Dispatcher component
- Événment Event Emittter component
- League Event component
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.
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
Define how an object must trigger an event. It contains 4 methods :
dispatch
to trigger an EventInterface instanceon
to attach a listeneronce
to attach a listener that will be ran then detachedremove
to remove a given listenerget
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 currentDispatcherInterface
getDispatcher
to retrieve the currentDispatcherInterface
hasDispatcher
to check if there is a currentDispatcherInterface
dispatch
to dispatch anEventInterface
on the linkDispatcherInterface
if there is one...
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.
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);