bornfreee / tactician-domain-events-bundle
Bundle to integrate Tactician Domain Events library with Symfony project
Installs: 135 431
Dependents: 0
Suggesters: 0
Security: 0
Stars: 10
Watchers: 5
Forks: 9
Open Issues: 3
Requires
- php: >=5.5
- bornfree/tactician-doctrine-domain-events: ^0.4.0
- symfony/config: ^2.8|^3.0|^4.0|^5.0
- symfony/dependency-injection: ^2.8|^3.0|^4.0|^5.0
- symfony/http-kernel: ^2.8|^3.0|^4.0|^5.0
Requires (Dev)
- phpunit/phpunit: ^5.7
README
Symfony Bundle to integrate Tactician Domain Events library with Symfony project
Installation
Install via composer
composer require bornfreee/tactician-domain-events-bundle
Add bundle to AppKernel.php
:
$bundles = [ // ... new \BornFree\TacticianDomainEventBundle\TacticianDomainEventBundle(), ];
Configuration
On default event collector CollectsEventsFromEntities
will be used, but sometimes you might record an event when entity doesn't change so the event is not collected.
It's also true when an aggregate root is recording events for its child entities. To collect those events you need to use CollectsEventsFromAllEntitiesManagedByUnitOfWork
That one collects events from all entities which are managed by Unit of Work. To use it you need to set collect_from_all_managed_entities
on true:
tactician_domain_event: collect_from_all_managed_entities: true # it's false on default
Usage
This bundle allows you to automatically have Domain Events dispatched by EventDispatcher
. It also allows to register event listeners and subscribers as the Symfony Services.
You can register as many listeners as you want for each Domain Event.
First, we need to install the Tactician official Bundle to integrate the command bus library:
composer require league/tactician-bundle
Then we need to configure Middleware to automatically record the Domain Events and dispatch them. We only want to handle the events themselves after the command has completely and successfully been handled. So we add the middleware that records the Domain Events before the Transaction middleware.
It means that as soon as transaction is completed, the Domain Events will be recorded:
tactician: commandbus: default: middleware: # other middlewares... - tactician_domain_events.middleware.release_recorded_events # make sure to add it before `tactician.middleware.doctrine` - tactician.middleware.doctrine - tactician.middleware.command_handler
Configuring Event Listeners
In order to add event listeners for dispatched Domain Events, we need to define services and the corresponded commands for them:
app.listener.send_email: class: AppBundle\EventListener\SendEmailAfterUserIsCreatedListener tags: - { name: tactician.event_listener, event: App\Domain\Events\UserWasCreated }
Notice the tag tactician.event_listener
. The bundle automatically finds all services tagged with this tag and adds the listener to EventDispatcher
.
By default, event listener shold have public __invoke
function. If you want to have regular method name, it's possible to it to the service configuration:
app.listener.send_email: class: AppBundle\EventListener\SendEmailAfterUserIsCreatedListener tags: - { name: tactician.event_listener, event: App\Domain\Events\UserWasCreated, method: send }
This is all configuration you need to start using the Tactician command bus with Domain Events.
Let's have an example where we create a new user and a UserWasCreated
domain event is dispatched:
class User implements ContainsRecordedEvents { use EventRecorderCapabilities; public function __construct($name) { $this->name = $name; $this->record(new UserWasCreated($name)); } // ... }
As soon as this Entity
is successfully created, the SendEmailAfterUserIsCreatedListener
will be triggered.
Debugging
You can run the debug:tactician-domain-events
command to get a list of all events with mapped listeners.
License
Copyright (c) 2017, Maks Rafalko
Under MIT license, read LICENSE file.