PHP code example of albertoarena / filament-event-sourcing
1. Go to this page and download the library: Download albertoarena/filament-event-sourcing library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
albertoarena / filament-event-sourcing example snippets
use Albertoarena\FilamentEventSourcing\Actions\EventHistoryAction;
EventHistoryAction::make(); // a "History" action for a row or page header
use Albertoarena\FilamentEventSourcing\FilamentEventSourcingPlugin;
$panel->plugin(
FilamentEventSourcingPlugin::make()
->storedEventsResource() // the read-only Stored Events browser, off by default
->replayPage() // the projector replay page, off by default and also config gated
);
use Albertoarena\FilamentEventSourcing\Concerns\CreatesEventSourcedRecord;
use Filament\Resources\Pages\CreateRecord;
class CreatePost extends CreateRecord
{
use CreatesEventSourcedRecord;
protected static string $resource = PostResource::class;
protected function handleAggregateCreation(string $uuid, array $data): void
{
PostAggregate::retrieve($uuid)
->createPost($data['title'], $data['body'])
->persist();
}
}
use Albertoarena\FilamentEventSourcing\Concerns\EditsEventSourcedRecord;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Database\Eloquent\Model;
class EditPost extends EditRecord
{
use EditsEventSourcedRecord;
protected static string $resource = PostResource::class;
protected function handleAggregateUpdate(Model $record, array $data): void
{
PostAggregate::retrieve($record->uuid)
->changeTitle($data['title'])
->persist();
}
}
use Albertoarena\FilamentEventSourcing\Actions\EventSourcedDeleteAction;
EventSourcedDeleteAction::make()
->using(fn (Post $record) => PostAggregate::retrieve($record->uuid)->deletePost()->persist());
use Albertoarena\FilamentEventSourcing\Concerns\HasStoredEvents;
use Spatie\EventSourcing\Projections\Projection;
class Post extends Projection
{
use HasStoredEvents;
}
use Albertoarena\FilamentEventSourcing\RelationManagers\StoredEventsRelationManager;
// On a resource:
public static function getRelations(): array
{
return [StoredEventsRelationManager::class];
}
use Albertoarena\FilamentEventSourcing\Actions\EventHistoryAction;
// As a table row action or page header action:
EventHistoryAction::make();
return [
// Column on projection models holding the aggregate uuid. Independent of the primary key.
'aggregate_uuid_column' => 'uuid',
'stored_events_resource' => [
'navigation_group' => null,
'navigation_sort' => null,
'per_page' => 25,
],
'replay' => [
// Master switch. The plugin option alone is not enough; this must also be true.
'enabled' => false,
// Gate ability checked before showing or running a replay. Null means panel access only.
'authorize' => null,
],
];