gbprod / domain-event-bundle
Integrates domain event library to a Symfony application
Installs: 129
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Type:bundle
Requires
- gbprod/domain-event: ^0.1
- symfony/framework-bundle: ^2.3|^3.0
Requires (Dev)
- phpunit/phpunit: 4.*
This package is auto-updated.
Last update: 2020-06-07 07:56:55 UTC
README
Integrates domain event library to a Symfony application
Installation
With composer :
composer require gbprod/domain-event-bundle
Update your app/AppKernel.php
file:
public function registerBundles() { $bundles = array( new GBProd\DomainEventBundle\DomainEventBundle(), ); }
Setup your entity
Use domain event library to raise events :
<?php namespace GBProd\Acme\Entity; use GBProd\DomainEvent\EventProvider; use GBProd\DomainEvent\EventProviderTrait; final class MyEntity implements EventProvider { use EventProviderTrait; public function doSomething() { $this->raise( new SomethingHappenedEvent($this->id) ); } }
Dispatch events from your repository
Example with Doctrine repository :
<?php namespace GBProd\AcmeBundle\Repository; use GBProd\DomainEvent\EventProvider; use GBProd\DomainEvent\Dispatcher; class MyEntityRepository { public function __construct(EntityManager $em, DomainEventDispatcher $dispatcher) { $this->em = $em; $this->dispatcher = $dispatcher; } public function save(MyEntity $entity) { $this->em->persist($entity); $this->em->flush(); $this->dispatcher->dispatch($entity); } }
# src/GBProd/AcmeBundle/Resourses/config/services.yml services: gbprod_acme.my_entity_repository: class: GBProd\AcmeBundle\Repository\MyEntityRepository arguments: - "@doctrine.entity_manager" - "@gbprod.domain_event_dispatcher"
This will dispatch events using Symfony Event dispatcher.
The name of the event will be the classname of the aggregate and the event (MyEntity.SomethingHappenedEvent
in this example).
Create your listener
<?php namespace GBProd\AcmeBundle\Listener; use Symfony\Component\EventDispatcher\Event; class MyListener { public function onSomethingHappened(Event $event) { $domainEvent = $event->getDomainEvent(); // Use it now } }
Register your listener
gbprod_acme.event_listener.my_listener: class: GBProd\AcmeBundle\Listener\MyListener tags: - { name: kernel.event_listener, event: MyEntity.SomethingHappenedEvent, method: 'onSomethingHappened' }