winzou/state-machine-bundle
Lightweight PHP/Symfony bundle for defining state machines on your domain objects. Configure graphs with states, transitions, and optional guard/before/after callbacks via YAML/XML/PHP, then apply and test transitions without hard-coded state logic.
Article, Order, User), aligning with Domain-Driven Design (DDD) principles. It enforces business rules (e.g., "an Article can only be published if accepted") via explicit transitions, reducing ad-hoc state checks in business logic.Order could have payment and shipping state machines), though this adds complexity.AppKernel, Symfony DI, and YAML config). Laravel’s service container and configuration system (.env, config/) differ significantly, requiring adapters or workarounds.Bundle system is not native to Laravel. Would need to be rewritten as a Laravel package (e.g., using Illuminate\Support\ServiceProvider)..env are preferred). Would require migration to Laravel’s config format.state-machine PHP library (this bundle wraps it) directly in Laravel, avoiding Symfony dependencies.AppKernel, ServiceContainer). Porting to Laravel would require significant refactoring.@service.method) would need replacement with Laravel’s service resolution (app()->make() or resolve()).spatie/laravel-state-machines, verot/flow) been considered? Why was this bundle chosen?| Symfony Feature | Laravel Equivalent | Integration Strategy |
|---|---|---|
AppKernel |
ServiceProvider |
Replace with Laravel’s register()/boot() methods. |
| YAML Config | PHP arrays/.env |
Convert YAML to Laravel’s config/state_machines.php. |
Symfony DI (@service) |
Laravel DI (app()->make()) |
Replace service IDs with Laravel’s container calls. |
| Event Dispatcher | Laravel Events | Use Laravel’s Event::dispatch() or listeners. |
| Bundle System | Composer Packages | Package as a Laravel-specific Composer package. |
Phase 1: Dependency Extraction
AppKernel, ContainerAware traits).config() helper.app()->make()).Phase 2: Configuration Adaptation
config/state_machines.php:
'my_bundle_article' => [
'class' => MyBundle\Entity\Article::class,
'property_path' => 'state',
'graph' => 'simple',
'states' => ['new', 'pending_review', ...],
'transitions' => [
'create' => ['from' => ['new'], 'to' => 'pending_review'],
// ...
],
'callbacks' => [
'guard' => [
'guard_on_submitting' => [
'on' => 'submit_changes',
'do' => ['MyAwesomeService', 'isSubmittable'],
'args' => ['object'],
],
],
],
],
@service.method) with Laravel’s closure-based callbacks or service bindings.Phase 3: DI and Service Integration
public function register() {
$this->app->singleton('state_machine.factory', function () {
return new StateMachineFactory($this->app['config']['state_machines']);
});
}
$this->app->bind(MyAwesomeService::class, function () {
return new MyAwesomeService();
});
Phase 4: Testing and Validation
Article) to validate the approach.if-else checks.How can I help you explore Laravel packages today?