Skip to content

Event Bus

The Event Bus plugin dispatches domain events to registered listeners, enabling loose coupling between bounded contexts through event-driven architecture.

One adapter is available:

  • SyncEventBus dispatches events synchronously using the Dispatcher plugin.

Why Use It

Domain events represent facts that have occurred in the system. The Event Bus decouples the event publisher (typically a command handler) from the listeners that react to these events. This enables cross-module communication, logging, notifications, and read model updates without tight coupling.

Usage

The bus dispatches domain events from handlers:

$this->eventBus->dispatch(new BookCreatedEvent($occurredOn, $book));

Events are immutable objects representing facts:

final readonly class BookCreatedEvent extends DomainEventAbstract
{
    public function __construct(
        private DateTimeImmutable $occurredOn,
        private Book $book
    ) {}
}

Listeners are registered in config/events.php:

return [
    BookCreatedEvent::class => [
        BookEventHandler::class,
        NotificationHandler::class,  // Multiple listeners supported
    ],
];

Testing

Events and listeners can be tested independently. The event bus can be mocked to verify that handlers dispatch the expected events, or listeners can be tested by invoking them directly with test events.

See Also