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

Commit

Permalink
make model customizable
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed May 18, 2018
1 parent 7af9c61 commit cbc15dd
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
8 changes: 5 additions & 3 deletions config/event-projector.php
@@ -1,7 +1,9 @@
<?php

return [
'projectors' => [
\Spatie\EventProjector\Tests\BalanceProjector::class,
],

/*
* The class name of the model responsible for storing events.
*/
'stored_event_model' => \Spatie\EventProjector\StoredEvent::class
];
2 changes: 1 addition & 1 deletion database/migrations/create_stored_events_table.php.stub
Expand Up @@ -11,7 +11,7 @@ class CreateStoredEventsTable extends Migration
Schema::create('stored_events', function (Blueprint $table) {
$table->increments('id');
$table->string('event_class');
$table->text('serialized_event');
$table->text('event_properties');
$table->timestamps();
});
}
Expand Down
7 changes: 6 additions & 1 deletion src/Console/ReplayEventsCommand.php
Expand Up @@ -18,11 +18,16 @@ class ReplayEventsCommand extends Command
/** @var \Spatie\EventProjector\EventProjectionist */
protected $eventSorcerer;

public function __construct(EventProjectionist $eventSorcerer)
/** @var string */
protected $storedEventModelClass;

public function __construct(EventProjectionist $eventSorcerer, string $storedEventModelClass)
{
parent::__construct();

$this->eventSorcerer = $eventSorcerer;

$this->storedEventModelClass = $storedEventModelClass;
}

public function handle()
Expand Down
12 changes: 12 additions & 0 deletions src/EventProjectorServiceProvider.php
Expand Up @@ -32,13 +32,25 @@ public function boot()
return new EventProjectionist();
});



$this->app->alias(EventProjectionist::class, 'event-projector');
}

public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/event-projector.php', 'event-projector');

$this->app
->when(EventSubscriber::class)
->needs('$storedEventModelClass')
->give(config('event-projector.stored_event_model'));

$this->app
->when(ReplayEventsCommand::class)
->needs('$storedEventModelClass')
->give(config('event-projector.stored_event_model'));

Event::subscribe(EventSubscriber::class);
}
}
9 changes: 7 additions & 2 deletions src/EventSubscriber.php
Expand Up @@ -7,9 +7,14 @@ class EventSubscriber
/** @var \Spatie\EventProjector\EventProjectionist */
protected $evenSorcerer;

public function __construct(EventProjectionist $evenSorcerer)
/** @var string */
protected $storedEventModelClass;

public function __construct(EventProjectionist $evenSorcerer, string $storedEventModelClass)
{
$this->evenSorcerer = $evenSorcerer;

$this->storedEventModelClass = $storedEventModelClass;
}

public function subscribe($events)
Expand All @@ -28,7 +33,7 @@ public function handleEvent(string $eventName, $payload)

public function storeEvent(ShouldBeStored $event)
{
StoredEvent::createForEvent($event);
$this->storedEventModelClass::createForEvent($event);

$this->evenSorcerer
->callEventHandlers($this->evenSorcerer->projectors, $event)
Expand Down
13 changes: 7 additions & 6 deletions src/StoredEvent.php
Expand Up @@ -9,23 +9,24 @@ class StoredEvent extends Model
public $guarded = [];

public $casts = [
'serialized_event' => 'array',
'event_properties' => 'array',
];

public static function createForEvent(ShouldBeStored $event): self
{
$storedEvent = new StoredEvent();
$storedEvent = new static();
$storedEvent->event_class = get_class($event);
$storedEvent->attributes['serialized_event'] = (new EventSerializer())->serialize(clone $event);
$storedEvent->attributes['event_properties'] = (new EventSerializer())->serialize(clone $event);
$storedEvent->save();

return $storedEvent;
}

public function getEventAttribute(): ShouldBeStored
{
return (new EventSerializer())->deserialize($this->event_class, $this->getOriginal('serialized_event'));


return (new EventSerializer())->deserialize(
$this->event_class,
$this->getOriginal('event_properties')
);
}
}

0 comments on commit cbc15dd

Please sign in to comment.