支持插件修改订单状态机流程

This commit is contained in:
Edward Yang 2023-02-08 19:02:12 +08:00
parent 0f0ba924ce
commit 27bfe41623
1 changed files with 23 additions and 2 deletions

View File

@ -147,8 +147,10 @@ class StateMachineService
*/ */
public function nextBackendStatuses(): array public function nextBackendStatuses(): array
{ {
$machines = $this->getMachines();
$currentStatusCode = $this->order->status; $currentStatusCode = $this->order->status;
$nextStatus = self::MACHINES[$currentStatusCode] ?? []; $nextStatus = $machines[$currentStatusCode] ?? [];
if (empty($nextStatus)) { if (empty($nextStatus)) {
return []; return [];
@ -185,6 +187,12 @@ class StateMachineService
return; return;
} }
foreach ($functions as $function) { foreach ($functions as $function) {
if ($function instanceof \Closure) {
$function();
continue;
}
if (! method_exists($this, $function)) { if (! method_exists($this, $function)) {
throw new \Exception("{$function} not exist in StateMachine!"); throw new \Exception("{$function} not exist in StateMachine!");
} }
@ -215,6 +223,17 @@ class StateMachineService
} }
} }
/**
* 获取状态机流程, 此处通过 filter hook 可以被外部插件修改
* @return mixed
*/
private function getMachines()
{
$machines = self::MACHINES;
return hook_filter('service.state_machine.machines', $machines);
}
/** /**
* 通过订单当前状态以及即将变为的状态获取需要触发的事件 * 通过订单当前状态以及即将变为的状态获取需要触发的事件
* *
@ -224,7 +243,9 @@ class StateMachineService
*/ */
private function getFunctions($oldStatus, $newStatus): array private function getFunctions($oldStatus, $newStatus): array
{ {
return self::MACHINES[$oldStatus][$newStatus] ?? []; $machines = $this->getMachines();
return $machines[$oldStatus][$newStatus] ?? [];
} }
/** /**