composer require open-solid/domainDomain events are significant occurrences within a software system that reflect changes in the state of the domain. These events are used to capture and communicate changes, allowing different parts of the system to react accordingly.
For example, in an e-commerce application, a domain event might be "OrderPlaced" when a customer completes a purchase. This event can then trigger other actions such as updating inventory, sending a confirmation email, or processing payment. Domain events help to decouple systems, making them more modular and easier to maintain, as each component can independently respond to changes without being tightly integrated.
<?phpuseOpenSolid\Bus\Handler\MessageHandlersLocator;
useOpenSolid\Bus\Middleware\HandlingMiddleware;
useOpenSolid\Bus\NativeLazyMessageBus;
useOpenSolid\Bus\NativeMessageBus;
useOpenSolid\DomainEvent\Bus\NativeDomainEventBus;
useOpenSolid\DomainEvent\DomainEvent;
finalreadonlyclass UserRegistered extends DomainEvent
{
}
finalreadonlyclass UserRegisteredSubscriber
{
publicfunction__invoke(UserRegistered$event): void
{
// Handle the event
}
}
$nativeMessageBus = newNativeMessageBus([
newHandlingMiddleware(
newMessageHandlersLocator([
UserRegistered::class => [newUserRegisteredSubscriber()],
]),
),
]);
$bus = newNativeDomainEventBus(newNativeLazyMessageBus($nativeMessageBus));
$bus->publish(newEntityUpdated('uuid'));
// do something in between ...$bus->flush();This software is published under the MIT License