Skip to content

Commit

Permalink
update message decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Feb 1, 2024
1 parent 17dbead commit c4abcc2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
40 changes: 40 additions & 0 deletions docs/pages/event_bus.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions docs/pages/message_decorator.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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;
Expand All @@ -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([
Expand Down Expand Up @@ -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)

0 comments on commit c4abcc2

Please sign in to comment.