https://github.com/tarfin-labs/event-machine
Event-driven state machines for Laravel with event sourcing, type-safe context, and full audit trail.
https://github.com/tarfin-labs/event-machine
event-sourcing finite-state-machine laravel laravel-package parallel-states php state-machine xstate
Last synced: 3 months ago
JSON representation
Event-driven state machines for Laravel with event sourcing, type-safe context, and full audit trail.
- Host: GitHub
- URL: https://github.com/tarfin-labs/event-machine
- Owner: tarfin-labs
- License: mit
- Created: 2023-03-14T09:16:35.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2026-03-30T10:28:56.000Z (3 months ago)
- Last Synced: 2026-03-30T10:29:46.795Z (3 months ago)
- Topics: event-sourcing, finite-state-machine, laravel, laravel-package, parallel-states, php, state-machine, xstate
- Language: PHP
- Homepage: https://eventmachine.dev
- Size: 5.53 MB
- Stars: 18
- Watchers: 6
- Forks: 1
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README

[](https://packagist.org/packages/tarfin-labs/event-machine)
[](https://github.com/tarfin-labs/event-machine/actions?query=workflow%3ACI+branch%3Amain)
[](https://packagist.org/packages/tarfin-labs/event-machine)
**Event-driven state machines for Laravel**
[Documentation](https://eventmachine.dev) · [Installation](#installation) · [Why EventMachine?](#why-eventmachine)
---
## Why EventMachine?
**Your business logic deserves better than nested if-statements.**
EventMachine brings the power of finite state machines to Laravel, inspired by [XState](https://xstate.js.org). Define your states, transitions, and behaviors declaratively - and let the machine handle the complexity.
### The Problem
```php
// 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...
}
}
```
### The Solution
```php
// 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,
],
],
],
);
```
### Key Benefits
| Feature | Description |
|---------|-------------|
| **Event Sourced** | Every transition persisted. Full audit trail. Replay history. |
| **Behaviors** | Guards validate, calculators compute, actions execute. |
| **Parallel Dispatch** | True parallel execution via Laravel queues. 5s + 2s = 5s, not 7s. |
| **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. |
## Installation
```bash
composer require tarfin-labs/event-machine
```
```bash
php artisan vendor:publish --tag="event-machine-migrations"
php artisan migrate
```
## Support Policy
Only the **latest major version** (currently v7) receives bug fixes and security patches. All previous versions are end of life. See the [Upgrading Guide](https://eventmachine.dev/getting-started/upgrading) for step-by-step migration from any version.
## Eloquent Integration
```php
class 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 tracked
```
## Documentation
For guards, actions, calculators, hierarchical states, parallel dispatch, validation, testing, and more:
**[Read the Documentation →](https://event-machine.tarfin.com)**
## Credits
- [Yunus Emre Deligöz](https://github.com/deligoez)
- [Fatih Aydın](https://github.com/aydinfatih)
- [Yunus Emre Nalbant](https://github.com/YunusEmreNalbant)
- [Faruk Can](https://github.com/frkcn)
- [Turan Karatuğ](https://github.com/tkaratug)
- [Yılmaz Demir](https://github.com/yidemir)
- Maybe you? [Contribute →](../../contributing)
## License
MIT License. See [LICENSE](LICENSE.md) for details.