bee4 / events
Basic event dispatcher definition, implement basic Mediator pattern
Installs: 2 971
Dependents: 3
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: >=5.4.0
Requires (Dev)
- atoum/atoum: ~2.5
- evenement/evenement: ~2.0
- league/event: ~2.0
- squizlabs/php_codesniffer: ~2.5
- symfony/event-dispatcher: ~2.5
Suggests
- evenement/evenement: Required to use Événement dispatcher adapter
- league/event: Required to use PHP League Event dispatcher adapter
- symfony/event-dispatcher: Required to use Symfony dispatcher adapter
README
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.
Installing
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 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...
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);