A powerful, framework-agnostic workflow engine for PHP applications. This core library provides comprehensive workflow definition, execution, and state management capabilities without any framework dependencies.
β οΈ WARNING: DEVELOPMENT STATUSβ οΈ This package is currently under active development and is NOT READY FOR PRODUCTION USE.
Features may be incomplete, APIs might change, and there could be breaking changes. Use at your own risk in development environments only.
- PHP 8.3+ - Leverages modern PHP features for type safety and performance
- Composer - For dependency management
- No framework dependencies - Works with any PHP project
- π Framework Agnostic: Works with any PHP framework or standalone applications
- π Type Safe: Full PHP 8.3+ type safety with strict typing and generics
- π§ Extensible: Plugin architecture for custom actions and storage adapters
- π State Management: Robust workflow instance state tracking and persistence
- β‘ Performance: Optimized for high-throughput workflow execution
- π‘οΈ Error Handling: Comprehensive exception handling with detailed context
- π Retry Logic: Built-in retry mechanisms with configurable strategies
- β±οΈ Timeouts: Step-level timeout controls for reliable execution
- π Conditions: Conditional workflow execution based on runtime data
- π― Events: Rich event system for monitoring and integration
- π§ͺ Well Tested: Comprehensive test suite with 160+ assertions
composer require solution-forest/workflow-engine-core
# Clone the repository
git clone https://github.com/solution-forest/workflow-engine-core.git
cd workflow-engine-core
# Install dependencies
composer install
# Run quality checks
composer ci
use SolutionForest\WorkflowEngine\Core\WorkflowBuilder;
use SolutionForest\WorkflowEngine\Core\WorkflowEngine;
use SolutionForest\WorkflowEngine\Core\WorkflowContext;
use SolutionForest\WorkflowEngine\Actions\BaseAction;
// Define custom actions
class ValidateOrderAction extends BaseAction
{
public function execute(WorkflowContext $context): ActionResult
{
$orderId = $context->getData('order_id');
// Your validation logic here
if ($this->isValidOrder($orderId)) {
return ActionResult::success(['validated' => true]);
}
return ActionResult::failure('Invalid order');
}
}
// Create a workflow
$workflow = WorkflowBuilder::create('order-processing')
->addStep('validate', ValidateOrderAction::class)
->addStep('payment', ProcessPaymentAction::class)
->addStep('fulfillment', FulfillOrderAction::class)
->addTransition('validate', 'payment')
->addTransition('payment', 'fulfillment')
->build();
// Execute the workflow
$engine = new WorkflowEngine();
$context = new WorkflowContext(
workflowId: 'order-processing',
stepId: 'validate',
data: ['order_id' => 123, 'customer_id' => 456]
);
$instance = $engine->start($workflow, $context);
$result = $engine->executeStep($instance, $context);
use SolutionForest\WorkflowEngine\Attributes\Condition;
class ConditionalAction extends BaseAction
{
#[Condition("data.amount > 1000")]
public function execute(WorkflowContext $context): ActionResult
{
// This action only executes if amount > 1000
return ActionResult::success();
}
}
use SolutionForest\WorkflowEngine\Attributes\Retry;
class ReliableAction extends BaseAction
{
#[Retry(maxAttempts: 3, delay: 1000)]
public function execute(WorkflowContext $context): ActionResult
{
// This action will retry up to 3 times with 1 second delay
return ActionResult::success();
}
}
use SolutionForest\WorkflowEngine\Attributes\Timeout;
class TimedAction extends BaseAction
{
#[Timeout(seconds: 30)]
public function execute(WorkflowContext $context): ActionResult
{
// This action will timeout after 30 seconds
return ActionResult::success();
}
}
The workflow engine follows a clean architecture pattern with clear separation of concerns:
βββββββββββββββββββ
β Workflow β
β Builder β
βββββββββββββββββββ
β
βΌ
βββββββββββββββββββ βββββββββββββββββββ
β Workflow ββββββ Workflow β
β Definition β β Engine β
βββββββββββββββββββ βββββββββββββββββββ
β β
βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββ
β Steps β β Executor β
β & Actions β β β
βββββββββββββββββββ βββββββββββββββββββ
β β
βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββ
β State β β Events β
β Manager β β Dispatcher β
βββββββββββββββββββ βββββββββββββββββββ
- Purpose: Fluent interface for creating workflow definitions
- Responsibilities:
- Provides method chaining (
.addStep()
,.when()
,.email()
, etc.) - Validates workflow structure during construction
- Creates immutable workflow definitions
- Supports conditional steps and common patterns
- Provides method chaining (
- Example:
WorkflowBuilder::create('user-onboarding')->addStep(...)->build()
- Purpose: Immutable data structure representing a complete workflow
- Responsibilities:
- Contains workflow metadata (name, description, version)
- Stores all steps and their relationships
- Defines step execution order and conditions
- Serves as a blueprint for workflow execution
- Key data: Steps, transitions, conditions, metadata
- Purpose: Central orchestrator that manages workflow execution
- Responsibilities:
- Starts new workflow instances from definitions
- Manages workflow lifecycle (start, pause, resume, cancel)
- Coordinates between different components
- Provides API for workflow operations
- Main methods:
start()
,pause()
,resume()
,cancel()
,getInstance()
- Purpose: Individual workflow tasks and their implementations
- Responsibilities:
- Steps: Define what should happen (metadata, config, conditions)
- Actions: Implement the actual business logic (
execute()
method) - Handle step-specific configuration (timeout, retry, conditions)
- Support compensation actions for rollback scenarios
- Examples:
SendEmailAction
,CreateUserAction
,ValidateOrderAction
- Purpose: Runtime engine that executes individual workflow steps
- Responsibilities:
- Executes actions in the correct sequence
- Handles conditional execution based on workflow context
- Manages timeouts and retry logic
- Processes step transitions and flow control
- Handles errors and compensation
- Purpose: Component responsible for workflow instance state persistence
- Responsibilities:
- Saves/loads workflow instances to/from storage
- Tracks workflow execution state (running, paused, completed, failed)
- Manages workflow context data
- Handles state transitions and validation
- Supports different storage adapters (database, file, memory)
- Purpose: Event system for monitoring and integration
- Responsibilities:
- Fires events during workflow execution
- Enables workflow monitoring and logging
- Supports custom event listeners
- Provides hooks for external system integration
- Events:
WorkflowStarted
,StepCompleted
,WorkflowFailed
, etc.
- Builder β creates β Definition
- Engine β uses Definition to create instances
- Engine β delegates to Executor for step execution
- Executor β runs β Steps & Actions
- State Manager β persists β workflow state
- Events Dispatcher β broadcasts β execution events
- Separation of concerns - each component has a single responsibility
- Extensibility - you can swap out storage adapters, add custom actions
- Testability - each component can be tested independently
- Framework agnostic - no dependencies on specific frameworks
- Type safety - full PHP 8.3+ type hints throughout
Implement the StorageAdapter
interface for custom storage:
use SolutionForest\WorkflowEngine\Contracts\StorageAdapter;
class CustomStorageAdapter implements StorageAdapter
{
public function save(WorkflowInstance $instance): void
{
// Save workflow instance to your storage
}
public function load(string $instanceId): ?WorkflowInstance
{
// Load workflow instance from your storage
}
public function delete(string $instanceId): void
{
// Delete workflow instance from your storage
}
}
Listen to workflow events:
use SolutionForest\WorkflowEngine\Contracts\EventDispatcher;
class CustomEventDispatcher implements EventDispatcher
{
public function dispatch(object $event): void
{
// Handle workflow events
match (get_class($event)) {
'SolutionForest\WorkflowEngine\Events\WorkflowStarted' => $this->onWorkflowStarted($event),
'SolutionForest\WorkflowEngine\Events\StepCompletedEvent' => $this->onStepCompleted($event),
'SolutionForest\WorkflowEngine\Events\WorkflowCompletedEvent' => $this->onWorkflowCompleted($event),
default => null,
};
}
}
Provide custom logging implementation:
use SolutionForest\WorkflowEngine\Contracts\Logger;
class CustomLogger implements Logger
{
public function info(string $message, array $context = []): void
{
// Log info messages
}
public function error(string $message, array $context = []): void
{
// Log error messages
}
public function warning(string $message, array $context = []): void
{
// Log warning messages
}
}
Run the test suite:
# Run all tests
composer test
# Run tests with coverage
composer test:coverage
# Run specific test file
vendor/bin/pest tests/Unit/WorkflowEngineTest.php
# Run tests with detailed output
vendor/bin/pest --verbose
We use several tools to maintain high code quality:
# Static analysis with PHPStan
composer analyze
# Code formatting with Laravel Pint
composer pint
# Check code formatting without making changes
composer pint --test
# Run all quality checks
composer pint && composer analyze && composer test
- Pest - Testing framework with expressive syntax
- Pest Architecture Testing - Architectural constraints and code quality rules
- PHPStan - Static analysis tool for catching bugs
- Laravel Pint - Code style fixer built on PHP-CS-Fixer
- Framework-agnostic - No Laravel dependencies in the core library
phpstan.neon.dist
- PHPStan configuration for static analysispint.json
- Laravel Pint configuration for code formattingphpunit.xml.dist
- PHPUnit configuration for testing.github/workflows/run-tests.yml
- CI/CD pipeline configuration
We maintain high code quality through:
- 100% PHPStan Level 6 - Static analysis with no errors
- Laravel Pint - Consistent code formatting following Laravel standards
- Comprehensive Testing - 40 tests with 160+ assertions covering all core functionality
- Type Safety - Full PHP 8.3+ type declarations and documentation
- Continuous Integration - Automated quality checks on every commit
This core library is framework-agnostic. For specific framework integrations:
- Laravel: Use
solution-forest/workflow-engine-laravel
- Symfony: Coming soon
- Other frameworks: Easily integrate using the provided interfaces
We welcome contributions! Please see CONTRIBUTING.md for details.
This project is licensed under the MIT License - see the LICENSE file for details.
- Solution Forest Team - Initial development and maintenance
- Contributors - Thank you to all contributors who have helped improve this project