sbooker / domain-events
Domain events
Installs: 4 504
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.4 || ^8.0
- ramsey/uuid: ^4.0
Requires (Dev)
- phpunit/phpunit: ^9.0
README
Installation
Install via Composer:
composer require sbooker/domain-events
Usage
Publish events
<?php namespace Domain; use Sbooker\DomainEvents\DomainEntity; use Sbooker\DomainEvents\DomainEvent; use Sbooker\DomainEvents\DomainEventCollector; class SomethingOccurred extends DomainEvent {} class SomeAggregateRoot implements DomainEntity { use DomainEventCollector; public function doSomethingOfDomainLogic() { // do $this->publish(new SomethingOccurred()); } }
Dispatch events
For dispatching events use Sbooker\Domain\Events\Publihser implementation
class SomeAggregateRoot implements DomainEntity { use DomainEventCollector; ... } $publisher = new class implements Publihser { ... } $aggregateRoot = new SomeAggregateRoot(); $aggregateRoot->dispatchEvents($publisher)
In more complex cases where Aggregate Root has one or more Domain Entities you must override DomainEventCollector::dispatchEvents as shown bellow:
class Entity implements DomainEntity { ... } class SomeAggregateRoot implements DomainEntity { use DomainEventCollector { dispatchEvents as doDispatchEvents; } private Entity $entity; ... public function dispatchEvents(Publisher $publisher): void { $this->doDispatchEvents($publisher); $this->entity->dispatchEvents($publisher); } }
License
See LICENSE file.