php 工作流实现
PHP 工作流实现方法
使用状态机模式
状态机模式适合处理具有明确状态转换的工作流。定义状态和转换规则,通过条件判断实现状态切换。
class WorkflowStateMachine {
private $currentState;
public function __construct($initialState) {
$this->currentState = $initialState;
}
public function transition($event) {
switch ($this->currentState) {
case 'draft':
if ($event === 'submit') {
$this->currentState = 'pending_review';
}
break;
case 'pending_review':
if ($event === 'approve') {
$this->currentState = 'approved';
} elseif ($event === 'reject') {
$this->currentState = 'rejected';
}
break;
}
}
public function getCurrentState() {
return $this->currentState;
}
}
数据库驱动工作流
在数据库中存储工作流定义和实例数据,使用表结构管理状态和转换。

CREATE TABLE workflows (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE workflow_states (
id INT AUTO_INCREMENT PRIMARY KEY,
workflow_id INT NOT NULL,
name VARCHAR(255) NOT NULL,
FOREIGN KEY (workflow_id) REFERENCES workflows(id)
);
CREATE TABLE workflow_transitions (
id INT AUTO_INCREMENT PRIMARY KEY,
workflow_id INT NOT NULL,
from_state_id INT NOT NULL,
to_state_id INT NOT NULL,
event VARCHAR(255) NOT NULL,
FOREIGN KEY (workflow_id) REFERENCES workflows(id),
FOREIGN KEY (from_state_id) REFERENCES workflow_states(id),
FOREIGN KEY (to_state_id) REFERENCES workflow_states(id)
);
使用 Symfony Workflow 组件
Symfony 提供了强大的 Workflow 组件,支持复杂的工作流定义和操作。
use Symfony\Component\Workflow\DefinitionBuilder;
use Symfony\Component\Workflow\MarkingStore\MethodMarkingStore;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\Transition;
use Symfony\Component\Workflow\Workflow;
$definitionBuilder = new DefinitionBuilder();
$definition = $definitionBuilder
->addPlaces(['draft', 'review', 'approved', 'rejected'])
->addTransition(new Transition('submit', 'draft', 'review'))
->addTransition(new Transition('approve', 'review', 'approved'))
->addTransition(new Transition('reject', 'review', 'rejected'))
->build();
$marking = new MethodMarkingStore(true, 'currentState');
$workflow = new Workflow($definition, $marking);
$document = new Document();
$workflow->apply($document, 'submit');
Laravel 队列系统
对于异步工作流任务,可以使用 Laravel 的队列系统处理长时间运行的任务。

class ProcessDocument implements ShouldQueue {
public function handle(Document $document) {
// 处理文档工作流逻辑
$document->status = 'processed';
$document->save();
}
}
// 分发任务
ProcessDocument::dispatch($document)->onQueue('workflow');
事件驱动架构
通过事件监听器实现松散耦合的工作流步骤,适合分布式系统。
class DocumentSubmitted {
public $document;
public function __construct(Document $document) {
$this->document = $document;
}
}
class SendReviewNotification {
public function handle(DocumentSubmitted $event) {
// 发送审核通知
}
}
Event::listen(
DocumentSubmitted::class,
[SendReviewNotification::class, 'handle']
);
// 触发事件
event(new DocumentSubmitted($document));
工作流引擎集成
对于企业级应用,可以考虑集成 Camunda 或 Activiti 等工作流引擎,通过 REST API 与 PHP 应用交互。
$client = new GuzzleHttp\Client();
$response = $client->post('https://engine.example.com/process-definition/key/document-approval/start', [
'json' => [
'variables' => [
'documentId' => ['value' => $document->id, 'type' => 'string']
]
]
]);






