Creates a RunInterface instance and executes the given workflow with the provided variables.
Signature
function run(
WorkflowInterface $workflow,
ContainerInterface $container = new Container(),
mixed ...$variable
): RunInterface
Parameters
workflow
WorkflowInterface
required
The workflow to execute, created with workflow().
Optional PSR-11 dependency injection container for resolving Action class dependencies. Defaults to an empty Container instance.
Named variables required by the workflow. These correspond to variable() references in job definitions.
Returns
The execution result containing job responses and execution metadata.
Usage
Basic execution
use function Chevere\Workflow\{workflow, sync, variable, run};
$workflow = workflow(
greet: sync(
fn(string $name): string => "Hello, {$name}!",
name: variable('username')
)
);
$result = run($workflow, username: 'World');
echo $result->response('greet')->string();
// Output: Hello, World!
With multiple variables
$workflow = workflow(
calculate: sync(
function (int $a, int $b): int {
return $a + $b;
},
a: 10,
b: variable('value')
)
);
$result = run($workflow, value: 5);
echo $result->response('calculate')->int();
// Output: 15
With dependency injection container
use Chevere\Container\Container;
use function Chevere\Workflow\run;
// Create container with dependencies
$container = new Container(
logger: new Logger(),
database: new Database()
);
$workflow = workflow(
notify: sync(
SendNotification::class, // Will auto-inject dependencies
email: variable('userEmail'),
message: 'Welcome!'
)
);
$result = run($workflow, $container, userEmail: '[email protected]');
Accessing typed responses
$result = run($workflow, username: 'Rodolfo');
// Get typed responses
$result->response('greet')->string(); // string
$result->response('count')->int(); // int
$result->response('price')->float(); // float
$result->response('active')->bool(); // bool
$result->response('data')->array(); // array
// Access array keys directly
$result->response('user', 'email')->string();
$result->response('user', 'id')->int();
Checking skipped jobs
$workflow = workflow(
greet: sync(
new Greet(),
username: variable('username')
)->withRunIf(
variable('sayHello')
)
);
$run = run(
$workflow,
username: '',
sayHello: false
);
if ($run->skip()->contains('greet')) {
echo "Greet job was skipped";
}
Error handling
use Chevere\Workflow\Exceptions\WorkflowException;
try {
$result = run($workflow, value: -1);
} catch (WorkflowException $e) {
echo "Job '{$e->name}' failed: {$e->getMessage()}";
// Access original exception
$original = $e->throwable;
}
Notes
- All variables declared with
variable() must be provided
- Variable names are case-sensitive and must match exactly
- The container is optional but required for Action classes with constructor dependencies
- Jobs execute in dependency order based on the workflow graph
- Async jobs run concurrently when possible
- Returns immediately after all jobs complete or on first error
- Use the returned
RunInterface to access job responses and execution metadata