grazulex / laravel-statecraft
A declarative and testable way to model entity state transitions (orders, documents, processesβ¦) with guard conditions, actions, and events.
Fund package maintenance!
Grazulex
Buy Me A Coffee
paypal.me/strauven
Requires
- php: ^8.3
- illuminate/contracts: ^12.0
- illuminate/support: ^12.19
- nesbot/carbon: ^3.10
- symfony/yaml: ^7.3
Requires (Dev)
- doctrine/dbal: ^4.2
- larastan/larastan: ^3.4
- laravel/pint: ^1.22
- orchestra/testbench: ^10.0
- pestphp/pest: ^3.8
- pestphp/pest-plugin-laravel: ^3.2
- rector/rector: ^2.0
README
Advanced State Machine implementation for Laravel applications. Declarative state management with support for conditions, actions, and complex workflows through YAML configuration.
π Table of Contents
- Overview
- β¨ Features
- π¦ Installation
- π Quick Start
- π Complete Documentation
- π‘ Examples & Use Cases
- π§ Requirements
- π§ͺ Testing
- π Performance
- π€ Contributing
- π Security
- π License
Overview
Laravel Statecraft is a powerful state machine implementation for Laravel that provides declarative state management through YAML configuration. Build complex workflows with conditional transitions, guards, actions, and comprehensive state tracking.
Perfect for order processing, user workflows, approval systems, and any application requiring sophisticated state management.
π― Use Cases
Laravel Statecraft is perfect for:
- Order Processing - Complex e-commerce order workflows
- User Registration - Multi-step user onboarding flows
- Approval Systems - Document or request approval workflows
- Content Management - Publishing and moderation workflows
- Business Processes - Any multi-state business logic
β¨ Features
- π Declarative Configuration - Define state machines in YAML files
- π Flexible Transitions - Conditional transitions with guards and actions
- π― Event System - Built-in events for state changes and transitions
- π State History - Track all state changes with timestamps
- π‘οΈ Guards & Actions - Pre/post transition validation and processing
- π Model Integration - Seamless Eloquent model integration
- π YAML Support - Human-readable state machine definitions
- π¨ Artisan Commands - CLI tools for state machine management
- β Validation - Comprehensive state machine validation
- π Visualization - Export state machines to Mermaid diagrams
- π§ͺ Test-Friendly - Built-in testing utilities
- β‘ Performance - Optimized for speed with caching support
π¦ Installation
Detailed installation instructions are available in our wiki: π¦ Installation & Setup
Quick install via Composer:
composer require grazulex/laravel-statecraft
π‘ Auto-Discovery
The service provider will be automatically registered thanks to Laravel's package auto-discovery.
Publish configuration:
php artisan vendor:publish --tag=statecraft-config
Publish migrations (if using history tracking):
php artisan vendor:publish --tag=statecraft-migrations php artisan migrate
π Quick Start
For a complete getting started guide, visit: π Basic Usage Guide
1. Create a State Machine Definition
php artisan statecraft:make OrderStateMachine --model=Order
2. Define Your State Machine in YAML
# state-machines/OrderStateMachine.yaml name: OrderStateMachine model: App\Models\Order initial_state: pending states: - name: pending description: Order is pending payment - name: paid description: Order has been paid - name: shipped description: Order has been shipped transitions: - name: pay from: pending to: paid guard: PaymentGuard action: ProcessPayment
3. Add the Trait to Your Model
use Grazulex\LaravelStatecraft\HasStateMachine; class Order extends Model { use HasStateMachine; protected $stateMachine = 'OrderStateMachine'; }
4. Use State Transitions
// Create a new order (starts in 'pending' state) $order = Order::create(['total' => 100.00]); // Transition to next state $order->transition('pay'); // Moves to 'paid' state // Check current state echo $order->currentState(); // 'paid'
π Complete Documentation
Our comprehensive documentation is available in the GitHub Wiki:
ποΈ Core Concepts
- π YAML Configuration - Complete YAML syntax and options
- π‘οΈ Guards & Actions - Pre/post transition logic
- π― Events System - State change events and listeners
- π State History - Track and audit state changes
π§ Advanced Topics
- βοΈ Configuration Guide - Package configuration options
- π¨ Console Commands - Artisan commands reference
- π§ͺ Testing Guide - Testing your state machines
π Getting Started
- π Wiki Home - Complete documentation homepage
- π¦ Installation & Setup - Detailed installation guide
- π Basic Usage Guide - Step-by-step tutorial
π‘ Examples & Use Cases
Explore real-world implementations and patterns:
- π Examples Collection - Complete examples overview
- π¦ Order Workflow Example - E-commerce order processing
- π° Article Publishing Example - Content management workflow
- π³ User Subscription Example - Subscription lifecycle management
- π― Event Usage Example - Advanced event handling
π§ Requirements
- PHP 8.2 or higher
- Laravel 11.x or higher
- Optional: Redis for caching (recommended for production)
π§ͺ Testing
Comprehensive testing guide: π§ͺ Testing Guide
composer test
composer test:coverage
composer test:types
π Performance
Laravel Statecraft is optimized for production use with caching support and minimal overhead. See our βοΈ Configuration Guide for performance optimization tips.
π€ Contributing
We welcome contributions! Please see CONTRIBUTING.md for details.
Before contributing:
- Read our Code of Conduct
- Check the issue tracker
- Review our π Complete Documentation
π Security
If you discover a security vulnerability, please send an e-mail via the security policy. All security vulnerabilities will be promptly addressed.
π License
Laravel Statecraft is open-sourced software licensed under the MIT license.
Made with β€οΈ for the Laravel community
Resources
Community Links
- CODE_OF_CONDUCT.md - Our code of conduct
- CONTRIBUTING.md - How to contribute
- SECURITY.md - Security policy
- RELEASES.md - Release notes and changelog