Saga
in package
Registers activity compensations and replays them in deterministic reverse order.
Registration is workflow-local state. Durable activity history reconstructs that state on every replay and records each compensation through the ordinary activity command path.
Table of Contents
Properties
- $compensated : bool
- $compensating : bool
-
$compensations
: array<int, array{activity_type: string, arguments: list, options: array
, registration_order: int}> - $context : WorkflowContext
- $failure : SagaCompensationFailed|null
Methods
- addCompensation() : self
- Register an activity to compensate a forward step that has completed.
- compensate() : void
- Execute registered compensation activities in reverse order.
- run() : TResult
- Run a forward path and automatically compensate any thrown failure.
Properties
$compensated
private
bool
$compensated
= false
$compensating
private
bool
$compensating
= false
$compensations
private
array<int, array{activity_type: string, arguments: list, options: array, registration_order: int}>
$compensations
= []
$context read-only
private
WorkflowContext
$context
$failure
private
SagaCompensationFailed|null
$failure
= null
Methods
addCompensation()
Register an activity to compensate a forward step that has completed.
public
addCompensation(string $activityType[, array<int, mixed> $arguments = [] ][, array<string, mixed> $options = [] ]) : self
Call this only after the forward activity returns. Arguments and options use the same typed command shape as WorkflowContext::activity().
Parameters
- $activityType : string
- $arguments : array<int, mixed> = []
- $options : array<string, mixed> = []
Return values
selfcompensate()
Execute registered compensation activities in reverse order.
public
compensate(Throwable $forwardFailure) : void
Repeated calls after success are no-ops. Compensations stop at the first terminal ActivityFailed. That failure is stable for the rest of the current replay and is rethrown instead of starting a second compensation pass.
Parameters
- $forwardFailure : Throwable
run()
Run a forward path and automatically compensate any thrown failure.
public
run(callable(self): TResult $forward) : TResult
The original failure is rethrown after successful compensation. If a compensation fails, a typed SagaCompensationFailed preserves both failures. Nested Saga::run() calls remain isolated; an escaping inner failure triggers the outer saga's own compensation list.
Parameters
- $forward : callable(self): TResult