tarfin-labs / event-machine
Event-driven state machines for Laravel with event sourcing, type-safe context, and full audit trail.
Requires
- php: ^8.3|^8.4|^8.5
- ext-zlib: *
- illuminate/contracts: ^11.0.8|^12.0
- nikic/php-parser: ^5.4
- spatie/laravel-data: ^4.13.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- driftingly/rector-laravel: ^2.1
- larastan/larastan: ^3.1.0
- laravel/pint: ^1.0
- orchestra/testbench: ^9.0|^10.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- pestphp/pest-plugin-type-coverage: ^4.0
- rector/rector: ^2.0
- testflowlabs/doctest: ^1.0
- dev-main
- 7.9.2
- 7.9.1
- 7.9.0
- 7.8.0
- 7.7.1
- 7.7.0
- 7.6.2
- 7.6.1
- 7.6.0
- 7.5.0
- 7.4.1
- 7.4.0
- 7.3.0
- 7.2.0
- 7.1.0
- v7.0.0
- 6.4.0
- 6.3.0
- 6.2.0
- 6.1.0
- 6.0.0
- 5.1.2
- 5.1.1
- 5.1.0
- 5.0.0
- 4.0.2
- 4.0.1
- 4.0.0
- v3.x-dev
- 3.0.2
- 3.0.1
- v3.0.0
- v2.x-dev
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- 1.7.0
- 1.6.0
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
- dev-feat/forward-aware-endpoints
- dev-release/v7
- dev-feature/infinite-loop-v7
- dev-dependabot/github_actions/ramsey/composer-install-4
- dev-feature/scheduled-events
- dev-feature/time-based-events
- dev-feature/inter-machine-communication
- dev-feature/http-endpoints
- dev-feature/parallel-dispatch
- dev-feature/naming-conventions
- dev-feature/max-transition-depth-protection
- dev-feature/type-string-event-resolution
- dev-feature/parallel-states-and-docs
- dev-v3-docs
- dev-improve-invokable-execution
- dev-docs
- dev-WB-180-event-machine-interacts-with-input-trait
- dev-improve-lock-mechanism
- dev-json-compression
- dev-WB-1389-fakeable-trait-issue-event-machine
- dev-laravel-12
- dev-config-validator-command
- dev-WB-814-event-machine-add-tests-to-verify-calculator-execution-behavior-in-guarded-transitions
- dev-WB-805-event-machine-feat-validator-add-support-for-status-events-in-root-keys
- dev-WB-784-event-machine-validation-for-state-machine-configuration-structure
- dev-WB-707-event-machine-enable-testing-of-inline-behaviors-in-machine-definitions
- dev-WB-675-event-machine-introduce-calculator-behaviors
- dev-v2-prep
- dev-WB-623-event-machine-reset-all-fakes
- dev-WB-624-event-machine-convert-context-validation-methods-to-static-in-invokable-behavior-class
- dev-WB-610-event-machine-fakeable-invokable-behaviors
- dev-setting-machines-in-model-method
- dev-WB-338-event-machine-fix-type-checking-behavior-on-behavior-dependency-injection
- dev-WB-291-event-machine-update-machine-cast-set-method-to-handle-uninitialized-machines
- dev-laravel-11-upgrade
- dev-WEB-5096-event-machine-state-machine-diagramlarini-otomatik-olusturabilmek
- dev-WEB-5120-form-request-siniflarinda-bail-ifadesinin-kaldirilmasi-ve-stop-on-first-failure-ozelliginin-kullanilmasi
- dev-WEB-4823-event-machine-mockable-actions
- dev-WEB-4724-basvuru-makinesi-disindaki-makinelerin-loglari-kapatilabilir
- dev-WEB-4689-replace-deprecated-package-nunomaduro-larastan-with-larastan-larastan
- dev-WEB-4344-event-machine-behavior-dependency-injection
- dev-WEB-4286-event-machine-json-alanlarinin-boyutunu-azaltmak
- dev-WEB-4311-event-machine-guardlarin-start-eventlerini-artik-kaydetmeyelim
- dev-WEB-4061-test-senaryo-datalarinin-v-4-te-kullanilabilir-olmasinin-saglanmasi
- dev-WEB-4267-event-machine-makine-calisiyor-yeni-event-kabul-etmeye-hazir-durumlari
- dev-WEB-3989-event-machine-transactional-transition-tanimlanmasi
- dev-WEB-4240-event-machine-actions-and-guards-that-log-itself
- dev-WEB-4287-event-machine-auto-castingi-engellemek
- dev-WEB-3988-event-machine-performer-tanimlanmasi
- dev-WEB-3991-event-machine-introduce-top-level-transitions
- dev-WEB-3993-event-machine-full-path-on-internal-event-types
- dev-WEB-3985-event-machine-introduce-machine-results
- dev-WEB-3980-event-machine-capture-events-from-child-states-and-forbidden-transitions
- dev-WEB-3973-event-machine-enable-validation-guards-in-guarded-transitions
- dev-WEB-3936-event-machine-initialize-custom-context-manager-from-machine-config
- dev-WEB-3924-event-machine-do-not-throw-validation-errors-for-the-past-events-in-the-history
- dev-WEB-3929-event-machine-do-not-throw-validation-errors
- dev-WEB-3913-event-machine-class-type-control-issue-in-get-set-and-has-methods-in-context-manager
- dev-WEB-3905-event-machine-use-logic-exceptions-instead-of-runtime-exceptions
- dev-WEB-3908-event-machine-improve-event-name-generation-from-class-names
- dev-WEB-3907-event-machine-internal-raised-event-bug
- dev-WEB-3900-event-machine-improved-internal-events
- dev-WEB-3904-event-machine-introduce-final-states
- dev-WEB-3903-event-machine-prevent-validation-guards-inside-guarded-transitions
- dev-WEB-3902-event-machine-fix-generating-state-init-event-on-initial-state
- dev-WEB-3901-event-machine-rename-internal-events-using-machine-id
- dev-WEB-3893-event-machine-introducing-validation-guards
- dev-WEB-3891-event-machine-first-guard-error-returns-instead-of-last
- dev-WEB-3874-event-machine-validate-invokable-behavior-through-itself
- dev-WEB-3873-event-machine-dont-validate-factory-created-events-immediately
- dev-WEB-3872-event-machine-validate-events
- dev-WEB-3866-event-machine-introduce-required-context-for-actions
- dev-WEB-3864-event-machine-introduce-no-transition-found-exception
- dev-WEB-3851-event-machine-introduce-required-context-definitions
- dev-WEB-3846-event-machine-casting-machine-actor-to-string
- dev-WEB-3843-event-machine-save-raised-events-to-history
- dev-WEB-3842-event-machine-save-state-init-internal-event
- dev-WEB-3841-event-machine-handle-json-serialization-of-the-machine-actor
- dev-WEB-3840-event-machine-entry-actions-are-saved-in-wrong-state-in-machine-events-table
- dev-WEB-3839-event-machine-sub-state-transition-error
- dev-WEB-3837-event-machine-entry-actions-of-states-with-sub-states-do-not-work
- dev-WEB-3825-event-machine-ability-to-raise-events-and-introduce-internal-event-queue
- dev-WEB-3824-event-machine-matching-state-values
- dev-WEB-3823-event-machine-actions-that-returns
- dev-WEB-3816-event-machine-introduce-transition-branch-definitions
- dev-WEB-3815-event-machine-introduce-invokable-behavior-arguments
- dev-WEB-3803-event-machine-introduce-type-check-for-context-manager
- dev-WEB-3802-event-machine-introduce-magic-methods-for-context-manager
- dev-WEB-3786-event-machine-introduce-event-factory-to-generate-fake-events-using-laravels-factory-mechanism
- dev-WEB-3782-throwing-validation-guards-with-error-messages
- dev-WEB-3781-content-manager-handle-context-manager-subclasses
- dev-WEB-3770-access-laravel-models-through-the-machines
- dev-WEB-3768-cast-model-attributes-as-event-machines
- dev-WEB-3760-persisting-machine-state
- dev-WEB-3761-record-internal-event-types-as-base-class-names-instead-of-fqcn
- dev-WEB-3748-propagate-event-and-state-definition-information-through-state
- dev-WEB-3747-fix-machine-actor-update-send-method-to-assign-the-new-state
- dev-WEB-3703-introduce-internal-events
- dev-WEB-3702-introduce-event-store
- dev-WEB-3701-update-event-behavior-constructor-with-optional-version-parameter
- dev-WEB-3699-add-version-property-to-event-behavior
- dev-initial-state-entry-action-fix
This package is auto-updated.
Last update: 2026-03-20 07:04:41 UTC
README
Event-driven state machines for Laravel
Documentation · Installation · 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. Define your states, transitions, and behaviors declaratively - and let the machine handle the complexity.
The Problem
// 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
// 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
composer require tarfin-labs/event-machine
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 for step-by-step migration from any version.
Eloquent Integration
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:
Credits
- Yunus Emre Deligöz
- Fatih Aydın
- Yunus Emre Nalbant
- Faruk Can
- Turan Karatuğ
- Yılmaz Demir
- Maybe you? Contribute →
License
MIT License. See LICENSE for details.