From 27bfe4162307d39b9143de7db47c7907cb7b16f4 Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Wed, 8 Feb 2023 19:02:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=8F=92=E4=BB=B6=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E8=AE=A2=E5=8D=95=E7=8A=B6=E6=80=81=E6=9C=BA=E6=B5=81?= =?UTF-8?q?=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- beike/Services/StateMachineService.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/beike/Services/StateMachineService.php b/beike/Services/StateMachineService.php index dd411f9d..40fac40b 100644 --- a/beike/Services/StateMachineService.php +++ b/beike/Services/StateMachineService.php @@ -147,8 +147,10 @@ class StateMachineService */ public function nextBackendStatuses(): array { + $machines = $this->getMachines(); + $currentStatusCode = $this->order->status; - $nextStatus = self::MACHINES[$currentStatusCode] ?? []; + $nextStatus = $machines[$currentStatusCode] ?? []; if (empty($nextStatus)) { return []; @@ -185,6 +187,12 @@ class StateMachineService return; } foreach ($functions as $function) { + if ($function instanceof \Closure) { + $function(); + + continue; + } + if (! method_exists($this, $function)) { 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 { - return self::MACHINES[$oldStatus][$newStatus] ?? []; + $machines = $this->getMachines(); + + return $machines[$oldStatus][$newStatus] ?? []; } /**