Skip to content

API Reference

Workflowable Facade

php
use Workflowable\Workflowable\Facades\Workflowable;

dispatch

Dispatch a workflow event asynchronously (queued).

php
Workflowable::dispatch(WorkflowEvent $event, ?int $userId = null): Collection

Returns a Collection<WorkflowInstance> of created instances.

dispatchSync

Dispatch a workflow event synchronously (inline execution).

php
Workflowable::dispatchSync(WorkflowEvent $event, ?int $userId = null): Collection

Blocks until each workflow reaches completion.


WorkflowEvent

Abstract base class for workflow event DTOs.

php
use Workflowable\Workflowable\Abstracts\WorkflowEvent;

class OrderSubmitted extends WorkflowEvent
{
    public function __construct(
        public readonly int $orderId,
        public readonly float $amount,
    ) {}

    // Optional: fields to encrypt at rest
    public static function encrypted(): array
    {
        return ['amount'];
    }
}

Constructor parameters become workflow variables accessible via ExecutionContext::getVariable().


Workflow Model

php
use Workflowable\Workflowable\Models\Workflow;

Attributes

AttributeTypeDescription
namestringWorkflow name
definitionarrayJSON definition (cast)
descriptionstring|nullOptional description
event_namestring|nullEvent name this workflow responds to
versionintVersion number (default: 1)
is_activeboolWhether this version accepts new instances
created_byint|nullOptional user ID

Methods

php
// Publish a new version
$workflow->publish(WorkflowDefinition $definition): Workflow

// Get all step names from the definition
$workflow->getAllStepNames(): array

// Check if a newer version exists
$workflow->hasNewerVersion(): bool

// Get the latest version model
$workflow->latestVersion(): Workflow

Scopes

php
Workflow::versionsOf('order_processing')->get();
Workflow::latestVersion('order_processing')->first();
Workflow::active()->get();
Workflow::forEvent('order_submitted')->get();
Workflow::byName('order_processing')->get();

Relationships

php
$workflow->instances(); // HasMany WorkflowInstance

WorkflowInstance Model

php
use Workflowable\Workflowable\Models\WorkflowInstance;

Attributes

AttributeTypeDescription
uuidstringUnique identifier for event sourcing aggregate
workflow_idintFK to Workflow
current_stepstring|nullCurrent step (null when completed)
statusstringpending, in_progress, completed, failed
workflow_eventWorkflowEventSerialized WorkflowEvent DTO
statearrayMutable runtime variables (event data + step outputs)
error_messagestring|nullError message if failed
created_byint|nullUser who created the instance
completed_atCarbon|nullWhen execution finished

Methods

php
$instance->isCompleted(): bool
$instance->isFailed(): bool
$instance->isPending(): bool
$instance->isInProgress(): bool

// Get a specific variable from state
$instance->getVariable(string $key, mixed $default = null): mixed

// Set a workflow variable (call save() to persist)
$instance->setVariable(string $key, mixed $value): void

// Event sourcing
$instance->storedEvents(int $limit = 50): Collection
$instance->rebuildState(): array
$instance->verifyState(): array  // Returns ['match' => bool, 'persisted' => [], 'replayed' => [], 'diff' => []]

Relationships

php
$instance->workflow();        // BelongsTo Workflow
$instance->stepExecutions();  // HasMany StepExecution

ExecutionContext

php
use Workflowable\Workflowable\Engine\ExecutionContext;

Passed to action handlers during step execution.

php
// Variables
$context->getVariable(string $key, mixed $default = null): mixed
$context->getVariables(): array
$context->setVariable(string $key, mixed $value): void
$context->mergeVariables(array $variables): void

// Step definition (config set at design time)
$context->getStepConfig(string $key, mixed $default = null): mixed
$context->getStepDefinition(): array

// Navigation
$context->getCurrentStep(): string
$context->getStepHistory(): array

// Models
$context->getInstance(): WorkflowInstance
$context->getWorkflow(): Workflow

ActionRegistry

php
use Workflowable\Workflowable\Registries\ActionRegistry;

Manages the mapping of friendly action names to handler classes.

php
$registry = app(ActionRegistry::class);

// Registration
$registry->register(string $name, string $class, string $description = ''): void

// Resolution
$registry->resolve(string $name): callable  // Throws StepExecutionException if not found
$registry->has(string $name): bool

// Discovery
$registry->names(): array                   // All registered names
$registry->all(): array                     // All actions with metadata
$registry->catalog(): array                 // name => description map
$registry->schema(): array                  // Full schema with parameter definitions

// Parameters (for handlers implementing DefinesParameters)
$registry->getParameters(string $name): array  // Returns Parameter[] or []

ConditionalRegistry

php
use Workflowable\Workflowable\Registries\ConditionalRegistry;

Manages the mapping of conditional names to evaluator classes. All classes must implement the Conditional interface.

php
$registry = app(ConditionalRegistry::class);

// Registration
$registry->register(string $name, string $class, string $description = ''): void

// Resolution
$registry->resolve(string $name): Conditional  // Throws StepExecutionException if not found
$registry->has(string $name): bool

// Discovery
$registry->names(): array                   // All registered names
$registry->all(): array                     // All conditionals with metadata
$registry->catalog(): array                 // name => description map
$registry->schema(): array                  // Full schema with parameter definitions

// Parameters (for handlers implementing DefinesParameters)
$registry->getParameters(string $name): array  // Returns Parameter[] or []

StepResult

php
use Workflowable\Workflowable\Engine\StepResult;

StepResult::success(mixed $output = null, ?string $nextStep = null, ?string $condition = null);
StepResult::failed(string $error);
StepResult::waiting(mixed $output = null, ?string $nextStep = null);