Your business logic deserves better than nested if-statements.
EventMachine brings the power of finite state machines to Laravel, inspired by XState. Define your states, transitions, and behaviors declaratively - and let the machine handle the complexity.
// Without state machines: scattered conditionals, hidden rules, impossible to test
if ($order->status === 'pending' && $user->can('approve') && !$order->isExpired()) {
if ($order->total > 10000 && !$order->hasSecondApproval()) {
// More nested logic...
}
}// With EventMachine: clear states, explicit transitions, testable behaviors
MachineDefinition::define(
config: [
'initial' => 'pending',
'states' => [
'pending' => [
'on' => [
'APPROVE' => [
'target' => 'approved',
'guards' => [CanApproveGuard::class, NotExpiredGuard::class],
],
],
],
'approved' => [
'entry' => NotifyCustomerAction::class,
],
],
],
);| Feature | Description |
|---|---|
| Event Sourced | Every transition persisted. Full audit trail. Replay history. |
| Behaviors | Guards validate, calculators compute, actions execute. |
| Testable | Fake any behavior. Assert states. Verify transitions. |
| Type-Safe Context | Spatie Data powered. Validated. IDE autocompletion. |
| Archival | Compress millions of events. Restore any machine instantly. |
| Laravel Native | Eloquent, DI, Artisan commands. Built for Laravel. |
composer require tarfin-labs/event-machinephp artisan vendor:publish --tag="event-machine-migrations"
php artisan migrateclass Order extends Model
{
use HasMachines;
protected $casts = [
'machine' => MachineCast::class.':'.OrderMachine::class,
];
}
// Use it naturally
$order = Order::create();
$order->machine->send(['type' => 'SUBMIT']);
$order->machine->send(['type' => 'APPROVE']);
$order->machine->state->matches('approved'); // true
$order->machine->state->history->count(); // 3 events trackedFor guards, actions, calculators, hierarchical states, validation, testing, and more:
- Yunus Emre Deligöz
- Fatih Aydın
- Yunus Emre Nalbant
- Faruk Can
- Turan Karatuğ
- Yılmaz Demir
- Maybe you? Contribute →
MIT License. See LICENSE for details.