Event Dispatcher
This repository has been moved to Big Name. There won't be any support here. Visit Big Name's repository for the newest updates., (*1)
An Event Dispatcher built with a focus on Domain Events., (*2)
Installation
Begin by installing the package through Composer. Edit your project's composer.json
file to require mitch/event-dispatcher
., (*3)
php
"require": {
"mitch/event-dispatcher": "0.1.x"
}
, (*4)
Next use Composer to update your project from the the Terminal:, (*5)
php
php composer.phar update
, (*6)
Once the package has been installed you'll need to add the service provider. Open your app/config/app.php
configuration file, and add a new item to the providers
array., (*7)
php
'Mitch\EventDispatcher\Laravel\EventDispatcherServiceProvider'
, (*8)
How It Works
Event
In your domain you'll create an event, for let's say when a new user has been added.
Lets call this event UserCreatedEvent
. This event will hold the necessary information for the listener to fulfill it's job.
You have complete freedom about which arguments it takes, since you'll be the one passing them in.
In some ways this event is a Date Transfer Object
(DTO)., (*9)
For example:, (*10)
<?php namespace Domain\Accounts\Events;
use Mitch\EventDispatcher\Event;
class UserCreatedEvent implements Event
{
public $user;
public function __construct($user)
{
$this->user = $user;
}
public function getName()
{
return 'UserCreated';
}
}
Listener
An event without a listener does no good for us, so lets create an email listener MailNewlyCreatedUserListener
for the event UserCreatedEvent
., (*11)
<?php namespace Domain\Accounts\EventListeners;
use Mitch\EventDispatcher\Listener;
class MailNewlyCreatedUserListener implements Listener
{
private $mailer
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
public function handle(Event $event)
{
// Do something with the event
}
}
Same rule with the listeners as the events, you have complete freedom with the arguments.
When an event is dispatched the handle
method on the correct listeners will be called., (*12)
Listening
Now we got the building blocks ready lets start listening for some new users, shall we.
For the sake of this example, the code is kept as simple as possible., (*13)
use Mitch\EventDispatcher\Dispatcher;
use Domain\Accounts\Events\UserCreatedEvent;
use Domain\Accounts\EventListeners\MailNewlyCreatedUserListener;
// Listening for event
$mailer = // Some mail package...
$listener = new MailNewlyCreatedUserListener($mailer);
$dispatcher = new Dispatcher;
$dispatcher->addListener('UserCreated', $listener);
// Dispatching event
$user = // A wild user appeared..
$event = new UserCreatedEvent($user);
$dispatcher->dispatch($event);
Dispatching multiple
For extra hipster points you can dispatch multiple events in 1 call., (*14)
use Mitch\EventDispatcher\Dispatcher;
use Domain\Accounts\UserAddedEvent;
use Domain\Achievements\UserGotAchievementEvent;
$user = ...;
$achievement = ...;
$events = [
new UserAddedEvent($user),
new UserGotAchievementEvent($user, $achievement)
];
$dispatcher = new Dispatcher;
$dispatcher->dispatch($events);
That's it!
Later tater, (*15)