# Workflow

The Workflow component provides the ability to define a runtime execution based on the workflow pattern (opens new window). It allows to define a series of individual interconnected steps with goal to describe and execute a sequential procedure.

# Creating a Workflow

To create a workflow pass the workflow name:

use Chevere\Components\Workflow\Workflow;

$workflow = new Workflow(name: 'insert-user');

# Adding Steps

The withAdded method allows to append steps to the Workflow. For the code below, note how validate and insert steps reference arguments stored in the response of the fetch step.

Variables payload and useCache will be required to be provided by the Workflow runner.

use Chevere\Components\Workflow\Step;

$workflow = $workflow
        fetch: (new Step('FetchAction'))
                payload: '${payload}'
        validate: (new Step('ValidateAction'))
                cache: '${useCache}',
                email: '${fetch:email}',
                username: '${fetch:username}',
        insert: (new Step('InsertAction'))
                email: '${fetch:email}',
                username: '${fetch:username}',

# Adding Steps Before and After

The withAddedBefore and withAddedAfter methods allows to add steps before/after another step.

Code below modify $workflow to extend its functionality.

# From this:

# To this:
use Chevere\Components\Workflow\Step;

$workflow = $workflow
        logging: (new Step('LoggingAction'))
            ->withArguments(device: '${logDevice}'),
        updateCount: (new Step('UpdateUserCountAction'))
            ->withArguments(add: 1),
        caching: (new Step('CachingAction'))
            ->withArguments(userId: '${insert:userId}')

# Step

The Step component define an Action with arguments to pass.

use Chevere\Components\Workflow\Step;

new Step(action: 'SomeActionClassName');

For the code above, the argument passed must be a class name implementing the ActionInterface.

# Step Parameters

Parameters for the step are defined in the Action Parameters.

# Step Arguments

The withArguments method is used to define arguments passed on runtime to the action defined in the step.

use Chevere\Components\Workflow\Step;

(new Step('SomeAction'))
        firstName: 'Rodolfo',
        lastName: 'Berrios'

For the code above, arguments Rodolfo and Berrios will be passed to SomeAction when running the Workflow. These arguments will be matched against the Parameters defined at the Action.

# Referenced Arguments

Referenced arguments can be defined to bind arguments referencing Workflow variables or responses returned after running any previous step.

Expression Meaning
${var} A context argument
${stepName:key} The value of key for the response of named step stepName