- How do I integrate Symfony Workflow into a Laravel application?
- Symfony Workflow is framework-agnostic but integrates smoothly with Laravel. Bind workflows to the container in `AppServiceProvider`, persist states to Eloquent models using `MethodMarkingStore`, and dispatch Laravel events on transitions. For example, trigger `OrderShipped` when an order transitions from `shipped` to `delivered`.
- What Laravel versions and PHP versions does Symfony Workflow support?
- Symfony Workflow supports Laravel as a standalone component and requires PHP 8.2+ (v7.x) or 8.4+ (v8.x). For older PHP (e.g., 8.1), use v6.4 of the component, but note that newer features like weighted transitions or BackedEnum support won’t be available. Laravel version compatibility is indirect—focus on PHP version support.
- Can I use Symfony Workflow for order fulfillment workflows in Laravel?
- Absolutely. Define states like `draft`, `review`, `shipped`, and `delivered` with transitions (e.g., `ship_order`). Use guards to enforce rules (e.g., `order.is_payable()`), persist states to an Eloquent model (e.g., `order.status`), and dispatch Laravel events like `OrderShipped` on transitions. The component handles complex pipelines like multi-step approvals.
- How do I persist workflow states to a database in Laravel?
- Use the `MethodMarkingStore` to sync workflow states to Eloquent model attributes. For example, map a workflow’s `marking` to a model’s `status` column. Configure the store in your workflow definition, and Laravel’s Eloquent will handle the database persistence automatically. For custom storage (e.g., Redis), implement a `MarkingStoreInterface`.
- What are guards in Symfony Workflow, and how do I use them in Laravel?
- Guards are conditions that validate whether a transition can occur. For example, a `publish_article` transition might require `article.is_reviewed()` AND `user.has_permission('publish')`. In Laravel, guards can call Eloquent methods, check auth (e.g., `auth()->user()->can('publish')`), or integrate with Laravel Policies for fine-grained control.
- How do I test Symfony Workflow transitions in Laravel?
- Test workflows by mocking the `Workflow` instance in PHPUnit and asserting transitions. For example, use `assertWorkflowTransitioned($workflow, $subject, 'draft', 'review')`. Test guards with mock data, and verify events are dispatched using Laravel’s `Events::assertDispatched()`. For integration tests, simulate full state paths (e.g., `draft → review → published`).
- Can I visualize Symfony Workflow states in Laravel?
- Yes. Use Symfony’s built-in `workflow:dump` CLI command to generate Graphviz or Mermaid diagrams. In Laravel, integrate the Symfony Profiler bundle to display workflow visualizations in the debug toolbar. For production, generate static diagrams via CLI and share them with stakeholders. No additional dependencies are needed beyond Symfony’s core tools.
- What’s the performance impact of Symfony Workflow in Laravel?
- Symfony Workflow adds minimal overhead—typically 5–10ms per workflow initialization. Benchmark in staging for your specific use case (e.g., 100 workflows/day vs. 100K). For high-throughput systems, cache workflow definitions or use lazy-loading. The component is optimized for clarity over raw speed, so prioritize readability and maintainability.
- How do I handle concurrent workflow transitions in Laravel (e.g., two users approving an order)?
- Concurrent transitions can cause race conditions. Use database transactions with `DB::transaction()` to ensure atomicity. For Eloquent, leverage optimistic locking (e.g., `increment('version')`) or pessimistic locks (`selectForUpdate()`). Symfony Workflow itself doesn’t handle concurrency—implement application-level locking in Laravel.
- Are there alternatives to Symfony Workflow for Laravel workflows?
- Alternatives include Laravel-specific packages like `spatie/laravel-workflow` (simpler, Laravel-focused) or `verot/flow` (lightweight). Symfony Workflow stands out for its maturity, guard conditions, event system, and FSM capabilities. Choose it if you need complex rules, auditability, or integration with Symfony’s ecosystem. For basic state machines, Spatie’s package may suffice.