Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

Add event_class_map for event class aliases #178

Merged
merged 6 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/advanced-usage/using-aliases-for-stored-event-classes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: Using aliases for stored event classes
weight: 8
---

By default we store the `Event`'s FQN in the database when storing the events. This prevents you from changing the name or the namespace of your event classes.

To get around this you can define event class aliases in the `event-projector.php` config file:

```php
/*
* Similar to Relation::morphMap() you can define which alias responds to which
* event class. This allows you to change the namespace or classnames
* of your events but still handle older events correctly.
*/
'event_class_map' => [
'money_added' => MoneyAddedEvent::class,
],
```

With this configuration, instead of saving `\App\Events\MoneyAddedEvent` in the database, we just store `money_added`, now you can change the event classname and namespace. Just make sure to also change the mapping!
10 changes: 5 additions & 5 deletions src/Models/StoredEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function createForEvent(ShouldBeStored $event, string $uuid = null
{
$storedEvent = new static();
$storedEvent->aggregate_uuid = $uuid;
$storedEvent->event_class = self::getEventClass(get_class($event));
$storedEvent->event_class = static::getEventClass(get_class($event));
$storedEvent->attributes['event_properties'] = app(EventSerializer::class)->serialize(clone $event);
$storedEvent->meta_data = [];
$storedEvent->created_at = Carbon::now();
Expand All @@ -38,9 +38,9 @@ public static function createForEvent(ShouldBeStored $event, string $uuid = null
return $storedEvent;
}

public function getEventClassAttribute(string $value): string
public static function getEventClassAttribute(string $value): string
riasvdv marked this conversation as resolved.
Show resolved Hide resolved
{
return self::getActualClassForEvent($value);
return static::getActualClassForEvent($value);
}

public function getEventAttribute(): ShouldBeStored
Expand Down Expand Up @@ -107,7 +107,7 @@ public static function store(ShouldBeStored $event, string $uuid = null): void
static::storeMany([$event], $uuid);
}

private static function getEventClass(string $class): string
protected static function getEventClass(string $class): string
{
$map = config('event-projector.event_class_map', []);

Expand All @@ -118,7 +118,7 @@ private static function getEventClass(string $class): string
return $class;
}

private static function getActualClassForEvent(string $class): string
protected static function getActualClassForEvent(string $class): string
{
return Arr::get(config('event-projector.event_class_map', []), $class, $class);
}
Expand Down