Domain Event Dispatcher Bundle
Brings the domain event dispatcher (singleton) to Symfony projects.
For full usage instructions, please see the full documentation
shipped with the library., (*1)
Installation
Install the bundle via Composer:, (*2)
$ composer require ashleydawson/domain-event-dispatcher-bundle
Then, register the bundle with the Symfony kernel app/AppKernel.php
:, (*3)
$bundles = [
// ...
new AshleyDawson\DomainEventDispatcherBundle\AshleyDawsonDomainEventDispatcherBundle(),
];
Configuration
Deferred events are
configured to be dispatched from the Symfony kernel.terminate
kernel event. To change this, add the following to
your app/config/config.yml
file:, (*4)
ashley_dawson_domain_event_dispatcher:
dispatch_deferred_events_from_kernel_event: kernel.terminate
dispatch_deferred_events_from_kernel_event_priority: 0
Usage
Please refer to the full documentation for an in-depth look at how to use the domain event dispatcher. However, please
find a simple example below:, (*5)
Create an event:, (*6)
<?php
namespace AppBundle\DomainEvent;
class MyDomainEvent
{
private $myEntityId;
public function __construct($myEntityId)
{
$this->myEntityId = $myEntityId;
}
public function getMyEntityId()
{
return $this->myEntityId;
}
}
Create a listener:, (*7)
<?php
namespace AppBundle\DomainEventListener;
use AppBundle\DomainEvent\MyDomainEvent;
class MyDomainEventListener
{
public function __invoke(MyDomainEvent $event)
{
// Do something with the event...
}
}
Add the listener to the event dispatcher via the Symfony Dependency Injection Container
using the tag ashley_dawson.domain_event_listener
:, (*8)
# app/config/services.yml
services:
app.my_domain_event_listener:
class: AppBundle\DomainEventListener\MyDomainEventListener
tags:
- { name: ashley_dawson.domain_event_listener }
Dispatch an event from your model:, (*9)
<?php
namespace AppBundle\Entity;
use AshleyDawson\DomainEventDispatcher\DomainEventDispatcher;
use AppBundle\DomainEvent\MyDomainEvent;
class MyEntity
{
private $id;
public function mySpecialCommand()
{
DomainEventDispatcher::getInstance()->dispatch(
new MyDomainEvent($this->id)
);
}
}
Symfony Profiler
The map of events that have been deferred/dispatched during a request can be found in the Symfony Profiler. Simply click
on the domain events icon and the profile screen containing the map will be displayed., (*10)
Toolbar Info:, (*11)
, (*12)
Full Profiler Screen:, (*13)
, (*14)