When your application needs to run a sequence of steps — some dependent on each other, others that could run at the same time — it's tempting to stuff everything into a single class or chain method calls together. The Chevere Workflow package offers a cleaner approach: declare your jobs, wire up their dependencies, and let the engine automatically determine execution order.
安装
作曲家
要求
chevere/workflow
A Practical Example
Consider a user registration flow that needs to:
- Create the user account
- Send a welcome email
- Set up a free trial subscription
- Notify your team on Slack
Some of these steps depend on the account being created first, while others can run in parallel once it is. Managing this manually means tracking shared state, writing glue code, and handling partial failures yourself. Workflow handles all of that declaratively.
Building the Pipeline
Here is how you might model the onboarding flow above:
使用
功能
Chevere\Workflow\
{
工作流程
,
同步
,
异步
,
多变的
,
回复
,
跑步
};$workflow
=
工作流程
(
createAccount
:
同步
(
CreateUserAccount
::班级
,
姓名
:
多变的
(
'姓名'
),
电子邮件
:
多变的
(
'电子邮件'
),
密码
:
多变的
(
'密码'
),),
sendWelcomeEmail
:
异步
(
发送欢迎电子邮件
::班级
,
用户身份
:
回复
(
'createAccount'
,
'ID'
),
电子邮件
:
回复
(
'createAccount'
,
'电子邮件'
),),
createTrial
:
同步
(
CreateTrialSubscription
::班级
,
用户身份
:
回复
(
'createAccount'
,
'ID'
),)
->
withRunIf
(
多变的
(
'enableTrial'
)),
notifyTeam
:
异步
(
NotifySlackChannel
::班级
,
userEmail
:
回复
(
'createAccount'
,
'电子邮件'
),),(英文):
A few things worth noting here:
sync()blocks until the job completes;async()runs concurrently once its dependencies are metvariable('name')is a placeholder for a value you supply at runtimeresponse('createAccount', 'id')引用idfield fromcreateAccount's output->withRunIf(variable('enableTrial'))makes thecreateTrialjob conditional — it is skipped entirely ifenableTrialis falsy
The engine builds a dependency graph from these declarations. Because
sendWelcomeEmail
和
notifyTeam
both depend only on
createAccount
, they run concurrently once the account is ready — without you needing to orchestrate that yourself.
Defining a Job
Each job can be a closure, an invocable class, or a class extending Chevere's
Action
. Here's a straightforward invocable:
使用
Chevere\Action\Action
;班级
CreateUserAccount
延伸
行动{
民众
功能
__invoke
(
细绳
$名称,
细绳
$email,
细绳
$password,)
:
大批
{$用户
=
用户
::
创造
([
'姓名'
=>
$名称,
'电子邮件'
=>
$email,
'密码'
=>
加密
($password),]);
返回
[
'ID'
=>
$用户
->
ID,
'电子邮件'
=>
$用户
->
电子邮件,];}}
The return value becomes the job's response. Downstream jobs reference individual fields via
response('createAccount', 'id')
or the whole response via
response('createAccount')
。
Running the Workflow
Pass your variables directly to
run()
, and access any job's output from the result:
$结果
=
跑步
($workflow,
姓名
:
“无名氏”
,
电子邮件
:
'jane@example.com'
,
密码
:
'秘密'
,
enableTrial
:
真的
,(英文):用户 ID
=
$结果
->
回复
(
'createAccount'
)
->
整数
(
'ID'
(英文):
Handling Failures
If any job throws, Workflow wraps it in a
WorkflowException
that tells you exactly which job failed and why:
使用
Chevere\Workflow\Exceptions\WorkflowException
;尝试
{$结果
=
跑步
($workflow,
...
(英文):}
抓住
(
WorkflowException
$e) {
记录器
()
->
错误
(
"Job '{
$e
->
姓名
}' failed"
,[
'错误'
=>
$e
->
可抛出
->
获取消息
(),]);}
Retrying Unreliable Jobs
For jobs that call external services prone to occasional hiccups, attach a retry policy with
withRetry()
:
notifyTeam
:
异步
(
NotifySlackChannel
::班级
,
userEmail
:
回复
(
'createAccount'
,
'电子邮件'
),)
->
withRetry
(
暂停
:
60
,
maxAttempts
:
3
,
延迟
:
5
),
Laravel 集成
A dedicated package brings first-class Laravel support, adding Artisan commands, an
AbstractWorkflow
base class, and a facade for running workflows anywhere in your app.
作曲家
要求
chevere/workflow-laravel
Generate a workflow class with Artisan:
php
工匠
制作:工作流
UserOnboarding
Then fill in the definition using the same jobs from earlier:
使用
Chevere\WorkflowLaravel\AbstractWorkflow
;使用
Chevere\Workflow\Interfaces\WorkflowInterface
;使用
功能
Chevere\Workflow\
{
工作流程
,
同步
,
异步
,
多变的
,
回复
};班级
UserOnboarding
延伸
AbstractWorkflow{
受保护
功能
定义
()
:
WorkflowInterface{
返回
工作流程
(
createAccount
:
同步
(
CreateUserAccount
::班级
,
姓名
:
多变的
(
'姓名'
),
电子邮件
:
多变的
(
'电子邮件'
),
密码
:
多变的
(
'密码'
),),
sendWelcomeEmail
:
异步
(
发送欢迎电子邮件
::班级
,
用户身份
:
回复
(
'createAccount'
,
'ID'
),
电子邮件
:
回复
(
'createAccount'
,
'电子邮件'
),),
createTrial
:
同步
(
CreateTrialSubscription
::班级
,
用户身份
:
回复
(
'createAccount'
,
'ID'
),)
->
withRunIf
(
多变的
(
'enableTrial'
)),
notifyTeam
:
异步
(
NotifySlackChannel
::班级
,
userEmail
:
回复
(
'createAccount'
,
'电子邮件'
),),(英文):}}
Run it via the
Workflow
facade — from a controller, a job, or anywhere else:
使用
Chevere\WorkflowLaravel\Facades\Workflow
;$结果
=
工作流
::
跑步
(
UserOnboarding
::班级
,
姓名
:$request
->
姓名,
电子邮件
:$request
->
电子邮件,
密码
:$request
->
password,
enableTrial
:$request
->
布尔值
(
'enable_trial'
),(英文):用户 ID
=
$结果
->
回复
(
'createAccount'
)
->
整数
(
'ID'
(英文):
Laravel's service container automatically handles constructor injection for class-based jobs, so any service your job needs can be type-hinted and resolved without extra configuration.
You can also list all registered workflows with
php artisan workflow:list
, or trigger one directly from the terminal with
php artisan workflow:run
。
Visualising the Graph
If you use VS Code, the package has an official extension that renders your workflow as a Mermaid flowchart. The diagram shows each job as a node, draws edges between dependent jobs, and annotates those edges with the data flowing between them — so you can see at a glance that
sendWelcomeEmail
waits on
createAccount
, or that
createTrial
only runs when
enableTrial
is truthy. Because the graph is derived directly from your code, it stays accurate as you add or rearrange jobs with nothing to maintain separately.
包起来
Chevere Workflow provides structure for multi-step processes that would otherwise scatter logic across multiple classes or make a single method unwieldy. The declarative style makes it easy to see what runs when, and the automatic dependency graph means parallel execution happens with no extra configuration.
The package also ships with testing utilities so you can verify each job in isolation and assert that your workflow's execution graph matches your expectations — something that is much harder when steps are tightly coupled.
查看 Chevere Workflow repository on GitHub for the full feature set, including Action classes, Workflow Providers, and the VS Code extension.







