3 weeks ago
Event sourcing is a powerful pattern for tracking changes to application state, offering a robust way to manage domain-driven design in Laravel. To streamline this process, I’ve created a Laravel package that automates the creation of event-sourcing domains using Spatie’s event sourcing library. Running this will generate the following structure: app/ └── Domain/ └── Sales/ ├── Actions/ │ ├── CreateOrder.php │ ├── DeleteOrder.php │ └── UpdateOrder.php ├── Aggregates/ │ └── OrderAggregate.php ├── Events/ │ ├── OrderCreated.php │ ├── OrderDeleted.php │ └── OrderUpdated.php ├── Projections/ │ └── Order.php └──Projectors/ └── OrderProjector.php #example-workflowExample workflow Running this will generate the following structure: app/ └── Domain/ └── Sales/ ├── Aggregates/ │ └── OrderAggregate.php ├── DataTrasnferObjects/ │ └── OrderData.php ├── Events/ │ ├── OrderCreated.php │ ├── OrderUpdated.php │ └── OrderDeleted.php ├── Projections/ │ └── Order.php └── Projectors/ └── OrderProjector.php #step-2-use-itStep Use it use App\Domain\Sales\Actions\CreateOrder; use App\Domain\Sales\DataTransferObjects\OrderData; use App\Domain\Sales\Projections\Order; (new CreateOrder)(new OrderData( customer_email: 'john@acme.org', customer_name: 'John Doe', total_amount: 110.2, items: [[ 'id' => 12345, 'name' => 'Whiskey bottle', 'quantity' => 2, 'price' => 55.1, ]], )); $order = Order::query()->where('email', 'john@acme.org')->first(); #advanced-examplesAdvanced Examples #generate-phpunit-testsGenerate PHPUnit tests https://github.com/albertoarena/laravel-event-sourcing-generator/blob/main/docs/unit-tests.md Running this will generate the following structure: app/ └── Domain/ └── Sales/ ├── Aggregates/ │ └── OrderAggregate.php ├── DataTrasnferObjects/ │ └── OrderData.php ├── Events/ │ ├── OrderCreated.php │ ├── OrderUpdated.php │ └── OrderDeleted.php ├── Notifications/ │ ├── Concerns/ │ │ └── HasDataAsArray │ ├── OrderCreated.php │ ├── OrderUpdated.php │ └── OrderDeleted.php ├── Projections/ │ └── Order.php └── Projectors/ └── OrderProjector.php #whats-nextWhat’s next