From 873954614a147e2ea6fe0cc52317eaed2e6c8f2a Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Wed, 8 Mar 2023 15:50:52 +0800 Subject: [PATCH] Optimize plugin manager --- beike/Plugin/Manager.php | 17 +++++++++++++++++ beike/Shop/Http/Controllers/HomeController.php | 3 +-- .../Services/TotalServices/ShippingService.php | 3 +-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/beike/Plugin/Manager.php b/beike/Plugin/Manager.php index af3f2f79..49682ccb 100644 --- a/beike/Plugin/Manager.php +++ b/beike/Plugin/Manager.php @@ -136,6 +136,23 @@ class Manager return $plugin; } + /** + * Check plugin is active, include existed, installed and enabled + * + * @param $code + * @return bool + * @throws \Exception + */ + public function checkActive($code): bool + { + $plugin = $this->getPlugin($code); + if (empty($plugin) || ! $plugin->getInstalled() || ! $plugin->getEnabled()) { + return false; + } + + return true; + } + /** * 获取插件目录以及配置 * diff --git a/beike/Shop/Http/Controllers/HomeController.php b/beike/Shop/Http/Controllers/HomeController.php index 657f51da..d89342da 100644 --- a/beike/Shop/Http/Controllers/HomeController.php +++ b/beike/Shop/Http/Controllers/HomeController.php @@ -32,8 +32,7 @@ class HomeController extends Controller $paths = explode('::', $viewPath); if (count($paths) == 2) { $pluginCode = $paths[0]; - $plugin = app('plugin')->getPlugin($pluginCode); - if (! $plugin || ! $plugin->getEnabled()) { + if (! app('plugin')->checkActive($pluginCode)) { continue; } } diff --git a/beike/Shop/Services/TotalServices/ShippingService.php b/beike/Shop/Services/TotalServices/ShippingService.php index 64e8c963..92c9745c 100644 --- a/beike/Shop/Services/TotalServices/ShippingService.php +++ b/beike/Shop/Services/TotalServices/ShippingService.php @@ -33,8 +33,7 @@ class ShippingService $shippingPluginCode = self::parseShippingPluginCode($shippingMethod); $pluginCode = Str::studly($shippingPluginCode); - $plugin = app('plugin')->getPlugin($shippingPluginCode); - if (empty($plugin) || ! $plugin->getInstalled() || ! $plugin->getEnabled()) { + if (! app('plugin')->checkActive($shippingPluginCode)) { $cart = $checkout->cart; $cart->shipping_method_code = ''; $cart->saveOrFail();