itmedia / domain-events
Domain events implementation
Installs: 2 536
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7.2.5 || ^8.0
- psr/log: *
- symfony/event-dispatcher-contracts: *
Requires (Dev)
- doctrine/orm: ^2.4
- phpunit/phpunit: ^6.0 || ^7.0
This package is auto-updated.
Last update: 2025-01-10 23:20:09 UTC
README
Domain Events Implementation
Install
composer require itmedia/domain-events
How to use
DomainEvent:
<?php use Itmedia\DomainEvents\Event\DomainEvent; class AccountRegistrationEvent implements DomainEvent { /** * @var Account */ private $account; public function __construct(Account $account) { $this->account = $account; } public function getName():string { return 'account_register'; } // ... }
Entity implement interface DomainEventPublisher
:
<?php use Itmedia\DomainEvents\Publisher\DomainEventPublisher; use Itmedia\DomainEvents\Publisher\DomainEventPublisherTrait; class Account implements DomainEventPublisher { use DomainEventPublisherTrait; // Helper trait public static function register($email) { $account = new self(); //... $account->pushEvent(new AccountRegistrationEvent($account)); // Checked single event $account->pushSingleEvent(new MyEvent($account)); $account->pushSingleEvent(new MyEvent($account)); return $account; } }
Use Doctrine ORM and Symfony events
Translation domain-event to symfony events on Doctrine postFlush() action:
# services.yml services: # Translate domain event to symfony events Itmedia\DomainEvents\Bridge\SymfonyDomainEventTranslatorDispatcher: arguments: ['@event_dispatcher', '@logger', '%kernel.debug%'] # Handle domain-events Itmedia\DomainEvents\Bridge\DoctrineDomainEventsHandler: arguments: ['@Itmedia\DomainEvents\Bridge\SymfonyDomainEventTranslatorDispatcher'] tags: - { name: doctrine.event_listener, event: postFlush } - { name: doctrine.event_listener, event: preFlush }