christhompsontldr/laravel-fsm
Robust finite state machine for Laravel with zero-config setup. Define states and transitions with guards, actions, and entry/exit callbacks. Event-driven with comprehensive transition logging, validation, caching, and support for multiple state machines per model column.
TransitionAttempted and TransitionFailed events provide hooks for observability and error handling.FsmBuilder API promotes composability and customization.approval_status and publication_status for a Document), addressing complex workflows without coupling.config/fsm.php.TransitionAttempted) may lead to spaghetti logic. Encourage modular event handlers (e.g., via Laravel’s HandleEvents trait).status column) or refactoring to adopt the trait. The package provides tools like fsm:diagram to visualize workflows pre-migration.Delivered) could block legitimate transitions. The package enforces this at the definition level but requires careful design.event_logging config enables queued logging, but custom storage (e.g., PostgreSQL JSONB) may be needed for compliance.use_transactions: true) mitigate race conditions but add overhead.dryRun()) and diagram generation, but complex workflows may require custom test doubles.status column? The default_column_name config allows flexibility, but migrations may be needed.FsmStateEnum), attributes, and typed properties for type safety. Compatible with modern Laravel practices (e.g., Pint, PHPStan).composer test. Mocking FSM transitions can be done via the HasFsm trait’s public methods (e.g., transitionFsm()).fsm:diagram, fsm:cache:clear) integrate with CI/CD pipelines for workflow visualization and cache management.OrderStatusFsm for orders).Document with approval_status).HasFsm trait and define transitions incrementally.dryRun() to validate transitions before enabling them.status column (or custom column) to models if missing. Example:
Schema::table('orders', function (Blueprint $table) {
$table->string('status')->default(OrderStatus::Pending->value);
});
config/fsm.php (e.g., disable transactions for read-heavy workflows).FsmBuilder API.TransitionFailed events for issues.pdo, mbstring, and json (standard in Laravel). No additional extensions needed.queuedAction(\App\Jobs\NotifyOpsJob::class)) require Laravel Queues to be configured.app/Fsm/Definitions before integrating with models.HasFsm trait to models after definitions are registered.StateTransitioned/TransitionFailed post-integration.TransitionFailed events during the first 24 hours post-launch.php artisan fsm:cache:clear) and may need database migrations if states/columns change.fsm:diagram) serve as living documentation. Encourage team adoption of this practice.TransitionFailed event provides context for failed transitions (e.g., guard failures). Log these to a monitoring tool (e.g., Sentry, Datadog).use_transactions: true) for critical workflows (e.g., payments).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Database transaction rollback | State and related data (e.g., payment) revert. | Use use_transactions: true and validate business logic in guards. |
| Guard validation failure | Transition blocked; user sees error. | Provide user-friendly error messages via custom guard logic. |
| Queue worker failure (queued actions) | Side effects (e.g., notifications) fail. | Implement retries (Laravel Queues) and dead-letter queues for monitoring. |
| Cache corruption | Stale FSM definitions loaded. | Cache invalidation on deployments; use `fsm |
How can I help you explore Laravel packages today?