The WorkflowBuilder class provides a fluent API for creating workflows.
Creates a new workflow builder instance.
$builder = WorkflowBuilder::create('my-workflow');Adds a custom action step to the workflow.
$builder->step('process-payment', ProcessPaymentAction::class);Adds an email step to the workflow.
$builder->email('welcome-email', [
'to' => '{{ user.email }}',
'subject' => 'Welcome {{ user.name }}!',
'data' => ['welcome_bonus' => 100]
]);Adds an HTTP request step to the workflow.
$builder->http('POST', 'https://api.example.com/webhooks', [
'event' => 'user_registered',
'user_id' => '{{ user.id }}'
]);Adds a delay step to the workflow.
$builder->delay(minutes: 30);
$builder->delay(hours: 2);
$builder->delay(days: 1);Adds conditional logic to the workflow.
$builder->when('user.age >= 18', function($builder) {
$builder->step('verify-identity', VerifyIdentityAction::class);
});Configures retry behavior for the previous step.
$builder->step('api-call', ApiCallAction::class)
->retry(attempts: 3, backoff: 'exponential');Sets a timeout for the previous step.
$builder->step('long-operation', LongOperationAction::class)
->timeout(minutes: 5);Builds and returns the workflow definition.
$workflow = $builder->build();The WorkflowContext class holds the data that flows through the workflow.
The workflow data.
The unique workflow instance ID.
The ID of the current step.
Gets data from the context.
$allData = $context->getData();
$user = $context->getData('user');
$userName = $context->getData('user.name');Creates a new context with additional data.
$newContext = $context->with(['step_result' => $result]);Creates a new context with a single data value.
$newContext = $context->withData('status', 'completed');Enum representing workflow states.
Pending- Workflow is created but not startedRunning- Workflow is currently executingCompleted- Workflow finished successfullyFailed- Workflow failed with an errorCancelled- Workflow was cancelledPaused- Workflow is temporarily paused
Returns a color representing the state.
WorkflowState::Running->color(); // 'blue'
WorkflowState::Completed->color(); // 'green'
WorkflowState::Failed->color(); // 'red'Returns an emoji icon for the state.
WorkflowState::Running->icon(); // '▶️'
WorkflowState::Completed->icon(); // '✅'
WorkflowState::Failed->icon(); // '❌'Returns a human-readable label.
WorkflowState::Running->label(); // 'In Progress'
WorkflowState::Completed->label(); // 'Completed'Checks if the state can transition to another state.
if ($currentState->canTransitionTo(WorkflowState::Completed)) {
// Can transition to completed
}Represents the result of an action execution.
Creates a successful result.
return ActionResult::success(['user_id' => 123]);Creates a failed result.
return ActionResult::failure('Payment failed', ['error_code' => 'CARD_DECLINED']);Creates a result that triggers a retry.
return ActionResult::retry('Rate limited, retrying...', ['retry_after' => 60]);Whether the action succeeded.
The result message.
Additional result data.
Helper class for creating common workflow patterns.
Creates a new simple workflow builder.
$workflow = SimpleWorkflow::quick()
->email('welcome', to: 'user@example.com')
->delay(days: 1)
->email('followup', to: 'user@example.com')
->build();Creates a workflow with sequential steps.
$workflow = SimpleWorkflow::sequential([
'step1' => StepOneAction::class,
'step2' => StepTwoAction::class,
'step3' => StepThreeAction::class
])->build();Marks a method as a workflow step.
use SolutionForest\WorkflowEngine\Attributes\WorkflowStep;
class MyAction implements WorkflowAction
{
#[WorkflowStep('my-step')]
public function execute(WorkflowContext $context): ActionResult
{
// Action logic
}
}Sets a timeout for an action.
use SolutionForest\WorkflowEngine\Attributes\Timeout;
class MyAction implements WorkflowAction
{
#[Timeout(minutes: 5)]
public function execute(WorkflowContext $context): ActionResult
{
// Action logic
}
}Configures retry behavior for an action.
use SolutionForest\WorkflowEngine\Attributes\Retry;
class MyAction implements WorkflowAction
{
#[Retry(attempts: 3, backoff: 'exponential')]
public function execute(WorkflowContext $context): ActionResult
{
// Action logic
}
}Sets a condition for when an action should execute.
use SolutionForest\WorkflowEngine\Attributes\Condition;
class MyAction implements WorkflowAction
{
#[Condition('user.age >= 18')]
public function execute(WorkflowContext $context): ActionResult
{
// Action logic
}
}