From c4abcc21bb0185a1768a287bf5a9c43cbaec5d2a Mon Sep 17 00:00:00 2001 From: David Badura Date: Thu, 1 Feb 2024 21:26:41 +0100 Subject: [PATCH] update message decorator --- docs/pages/event_bus.md | 40 +++++++++++++++++++++++++++++++++ docs/pages/message_decorator.md | 15 ++++++++++--- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/docs/pages/event_bus.md b/docs/pages/event_bus.md index ba03cd3d1..bd3178430 100644 --- a/docs/pages/event_bus.md +++ b/docs/pages/event_bus.md @@ -104,6 +104,30 @@ $eventBus = new DefaultEventBus($listenerProvider); The `DefaultEventBus::create` method uses the `AttributeListenerProvider` by default. +### Custom listener provider + +You can also use your own listener provider. + +```php +use Patchlevel\EventSourcing\EventBus\DefaultEventBus; +use Patchlevel\EventSourcing\EventBus\ListenerProvider; +use Patchlevel\EventSourcing\EventBus\ListenerDescriptor; + +$listenerProvider = new class implements ListenerProvider { + public function listenersForEvent(string $eventClass): iterable + { + return [ + new ListenerDescriptor( + (new WelcomeSubscriber())->onProfileCreated(...), + ), + ]; + } +}; +``` + +!!! tip + + You can use `$listenerDiscriptor->name()` to get the name of the listener. ## Listener @@ -148,6 +172,22 @@ final class WelcomeSubscriber } ``` +## Psr-14 Event Bus + +You can also use a [psr-14](https://www.php-fig.org/psr/psr-14/) compatible event bus. +In this case, you can't use the `Subscribe` attribute. +You need to use the system of the psr-14 event bus. + +```php +use Patchlevel\EventSourcing\EventBus\Psr14EventBus; + +$eventBus = new Psr14EventBus($psr14EventDispatcher); +``` + +!!! warning + + You can't use the `Subscribe` attribute with the psr-14 event bus. + ## Learn more * [How to decorate messages](message_decorator.md) diff --git a/docs/pages/message_decorator.md b/docs/pages/message_decorator.md index cdaa5c78a..c8b799043 100644 --- a/docs/pages/message_decorator.md +++ b/docs/pages/message_decorator.md @@ -1,6 +1,6 @@ # Message Decorator -There are usecases where you want to add some extra context to your events like metadata which is not directly relevant +There are use-cases where you want to add some extra context to your events like metadata which is not directly relevant for your domain. With `MessageDecorator` we are providing a solution to add this metadata to your events. The metadata will also be persisted in the database and can be retrieved later on. @@ -22,7 +22,7 @@ $decorator = new SplitStreamDecorator($eventMetadataFactory); ### ChainMessageDecorator -To use multiple decorators at the same time, one can use the `ChainMessageDecorator`. +To use multiple decorators at the same time, you can use the `ChainMessageDecorator`. ```php use Patchlevel\EventSourcing\EventBus\Decorator\ChainMessageDecorator; @@ -35,10 +35,12 @@ $decorator = new ChainMessageDecorator([ ## Use decorator -To use the message decorator, you have to pass it to the `DefaultRepositoryManager`. +To use the message decorator, you have to pass it to the `DefaultRepositoryManager`, +which will then pass it to all Repositories. ```php use Patchlevel\EventSourcing\EventBus\Decorator\ChainMessageDecorator; +use Patchlevel\EventSourcing\EventBus\Decorator\SplitStreamDecorator; use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager; $decorator = new ChainMessageDecorator([ @@ -84,3 +86,10 @@ final class OnSystemRecordedDecorator implements MessageDecorator !!! tip You can also set multiple headers with `withCustomHeaders` which expects an hashmap. + +## Learn more + +* [How to define events](events.md) +* [How to use the event bus](event_bus.md) +* [How to configure repositories](repository.md) +* [How to upcast events](upcasting.md)