Skip to main content
Creates a WorkflowInterface instance for the given jobs.

Signature

function workflow(JobInterface ...$job): WorkflowInterface

Parameters

job
JobInterface
required
Named jobs created with sync() or async() functions. Pass jobs as named arguments where the name identifies the job in the workflow.

Returns

WorkflowInterface
WorkflowInterface
A workflow instance containing the defined jobs and their dependency graph.

Usage

Basic workflow

use function Chevere\Workflow\{workflow, sync, variable};

$workflow = workflow(
    greet: sync(
        fn(string $name): string => "Hello, {$name}!",
        name: variable('username')
    )
);

Multiple jobs with dependencies

use function Chevere\Workflow\{workflow, sync, async, variable, response};

$workflow = workflow(
    fetch: async(
        FetchData::class,
        url: variable('endpoint')
    ),
    process: sync(
        ProcessData::class,
        data: response('fetch')
    ),
    store: sync(
        StoreData::class,
        result: response('process')
    )
);

Parallel execution

$workflow = workflow(
    // These run in parallel
    thumb: async(
        ImageResize::class,
        file: variable('image'),
        size: 'thumb'
    ),
    medium: async(
        ImageResize::class,
        file: variable('image'),
        size: 'medium'
    ),
    large: async(
        ImageResize::class,
        file: variable('image'),
        size: 'large'
    ),
    // This waits for all above
    store: sync(
        StoreFiles::class,
        thumb: response('thumb'),
        medium: response('medium'),
        large: response('large')
    )
);

Notes

  • Job names must be unique within a workflow
  • The workflow automatically builds a dependency graph based on response() references
  • Jobs without dependencies can run in parallel when using async()
  • Use sync() for synchronous jobs and async() for asynchronous jobs

Build docs developers (and LLMs) love