diff --git a/src/app/event/init/InitAddon.php b/src/app/event/init/InitAddon.php index 264c65b73..fb6a53b1e 100644 --- a/src/app/event/init/InitAddon.php +++ b/src/app/event/init/InitAddon.php @@ -3,6 +3,7 @@ namespace app\event\init; use app\model\system\Addon; +use Exception; use think\facade\Event; use think\facade\Cache; @@ -14,6 +15,7 @@ class InitAddon // 行为扩展的执行入口必须是run public function handle() { + log_write('InitAddon初始化插件', 'info'); if (defined('BIND_MODULE') && BIND_MODULE === 'install') return; $this->InitEvent(); } @@ -23,122 +25,127 @@ class InitAddon */ private function InitEvent() { - $cache = Cache::get("addon_event_list"); - if (!defined("initroute_tag")) exit(); - - // 获取当前插件列表 - $addon_model = new Addon(); - $addon_data = $addon_model->getAddonList([], 'name'); - $current_addons = $addon_data['data']; - $current_addon_names = array_column($current_addons, 'name'); - - // 1. 记录总的addon数量和名称 - $total_addons = count($current_addon_names); - log_write("[Addon Init] 总插件数量: {$total_addons}, 插件列表: " . implode(', ', $current_addon_names), 'info'); - - // 2. 记录缓存中不存在但实际存在的addon - $cache_addon_names = []; - if (!empty($cache) && isset($cache['addon_events'])) { - $cache_addon_names = array_keys($cache['addon_events']); - } - $new_addons_in_cache = array_diff($current_addon_names, $cache_addon_names); - if (!empty($new_addons_in_cache)) { - log_write("[Addon Init] 缓存中不存在但实际存在的插件: " . implode(', ', $new_addons_in_cache), 'info'); - } - - // 构建新的插件事件映射 - $new_addon_events = []; - $new_merged_listeners = []; - $addons_with_events = []; - $addons_without_events = []; - - // 遍历当前插件,收集事件信息 - foreach ($current_addons as $addon) { - $addon_name = $addon['name']; - if (file_exists('addon/' . $addon_name . '/config/event.php')) { - $addon_event = require_once 'addon/' . $addon_name . '/config/event.php'; - $listen = $addon_event['listen'] ?? []; - - if (!empty($listen)) { - $new_addon_events[$addon_name] = $listen; - $addons_with_events[] = $addon_name; - - // 合并事件监听器 - foreach ($listen as $event => $listeners) { - if (!isset($new_merged_listeners[$event])) { - $new_merged_listeners[$event] = []; + try { + $cache = Cache::get("addon_event_list"); + if (!defined("initroute_tag")) exit(); + + // 获取当前插件列表 + $addon_model = new Addon(); + $addon_data = $addon_model->getAddonList([], 'name'); + $current_addons = $addon_data['data']; + $current_addon_names = array_column($current_addons, 'name'); + + // 1. 记录总的addon数量和名称 + $total_addons = count($current_addon_names); + log_write("[Addon Init] 总插件数量: {$total_addons}, 插件列表: " . implode(', ', $current_addon_names), 'info'); + + // 2. 记录缓存中不存在但实际存在的addon + $cache_addon_names = []; + if (!empty($cache) && isset($cache['addon_events'])) { + $cache_addon_names = array_keys($cache['addon_events']); + } + $new_addons_in_cache = array_diff($current_addon_names, $cache_addon_names); + if (!empty($new_addons_in_cache)) { + log_write("[Addon Init] 缓存中不存在但实际存在的插件: " . implode(', ', $new_addons_in_cache), 'info'); + } + + // 构建新的插件事件映射 + $new_addon_events = []; + $new_merged_listeners = []; + $addons_with_events = []; + $addons_without_events = []; + + // 遍历当前插件,收集事件信息 + foreach ($current_addons as $addon) { + $addon_name = $addon['name']; + if (file_exists('addon/' . $addon_name . '/config/event.php')) { + $addon_event = require_once 'addon/' . $addon_name . '/config/event.php'; + $listen = $addon_event['listen'] ?? []; + + if (!empty($listen)) { + $new_addon_events[$addon_name] = $listen; + $addons_with_events[] = $addon_name; + + // 合并事件监听器 + foreach ($listen as $event => $listeners) { + if (!isset($new_merged_listeners[$event])) { + $new_merged_listeners[$event] = []; + } + $new_merged_listeners[$event] = array_unique(array_merge($new_merged_listeners[$event], $listeners)); } - $new_merged_listeners[$event] = array_unique(array_merge($new_merged_listeners[$event], $listeners)); + } else { + $addons_without_events[] = $addon_name; } } else { $addons_without_events[] = $addon_name; } - } else { - $addons_without_events[] = $addon_name; } - } - - // 3. 记录注册和未注册事件监听的addon - log_write("[Addon Init] 注册了事件监听的插件: " . (empty($addons_with_events) ? '无' : implode(', ', $addons_with_events)), 'info'); - log_write("[Addon Init] 未注册事件监听的插件: " . (empty($addons_without_events) ? '无' : implode(', ', $addons_without_events)), 'info'); - - // 获取当前所有事件名称 - $current_events = array_keys($new_merged_listeners); - - // 获取缓存中的旧事件名称 - $old_events = []; - if (!empty($cache) && isset($cache['addon_events'])) { - $old_addon_events = $cache['addon_events']; - foreach ($old_addon_events as $event_listeners) { - $old_events = array_merge($old_events, array_keys($event_listeners)); + + // 3. 记录注册和未注册事件监听的addon + log_write("[Addon Init] 注册了事件监听的插件: " . (empty($addons_with_events) ? '无' : implode(', ', $addons_with_events)), 'info'); + log_write("[Addon Init] 未注册事件监听的插件: " . (empty($addons_without_events) ? '无' : implode(', ', $addons_without_events)), 'info'); + + // 获取当前所有事件名称 + $current_events = array_keys($new_merged_listeners); + + // 获取缓存中的旧事件名称 + $old_events = []; + if (!empty($cache) && isset($cache['addon_events'])) { + $old_addon_events = $cache['addon_events']; + foreach ($old_addon_events as $event_listeners) { + $old_events = array_merge($old_events, array_keys($event_listeners)); + } + $old_events = array_unique($old_events); } - $old_events = array_unique($old_events); - } - - // 4. 记录已存在和新注册的事件 - $all_events = array_unique(array_merge($current_events, $old_events)); - $events_already_exists = []; - $events_new_registered = []; - - foreach ($current_events as $event) { - if (in_array($event, $old_events)) { - $events_already_exists[] = $event; - } else { - $events_new_registered[] = $event; - } - } - - if (!empty($events_already_exists)) { - log_write("[Addon Init] 已存在的事件: " . implode(', ', $events_already_exists), 'info'); - } - - if (!empty($events_new_registered)) { - log_write("[Addon Init] 新注册的事件: " . implode(', ', $events_new_registered), 'info'); - } - - // 处理每个事件的监听器 - foreach ($all_events as $event) { - // 获取当前事件的目标监听器列表 - $target_listeners = $new_merged_listeners[$event] ?? []; - - // 检查事件是否已存在监听 - if (Event::hasListener($event)) { - // 直接重新设置该事件的监听器,覆盖旧的监听器 - // 这样可以确保移除已删除插件的监听器,同时只保留当前有效的监听器 - Event::listenEvents([$event => $target_listeners]); - } else { - // 事件不存在监听,直接添加 - if (!empty($target_listeners)) { - Event::listenEvents([$event => $target_listeners]); + + // 4. 记录已存在和新注册的事件 + $all_events = array_unique(array_merge($current_events, $old_events)); + $events_already_exists = []; + $events_new_registered = []; + + foreach ($current_events as $event) { + if (in_array($event, $old_events)) { + $events_already_exists[] = $event; + } else { + $events_new_registered[] = $event; } } - } - - // 更新缓存 - Cache::tag("addon")->set("addon_event_list", [ - 'addon_events' => $new_addon_events, - 'merged_listeners' => $new_merged_listeners - ]); - } -} \ No newline at end of file + if (!empty($events_already_exists)) { + log_write("[Addon Init] 已存在的事件: " . implode(', ', $events_already_exists), 'info'); + } + + if (!empty($events_new_registered)) { + log_write("[Addon Init] 新注册的事件: " . implode(', ', $events_new_registered), 'info'); + } + + // 处理每个事件的监听器 + foreach ($all_events as $event) { + // 获取当前事件的目标监听器列表 + $target_listeners = $new_merged_listeners[$event] ?? []; + + // 检查事件是否已存在监听 + if (Event::hasListener($event)) { + // 直接重新设置该事件的监听器,覆盖旧的监听器 + // 这样可以确保移除已删除插件的监听器,同时只保留当前有效的监听器 + Event::listenEvents([$event => $target_listeners]); + } else { + // 事件不存在监听,直接添加 + if (!empty($target_listeners)) { + Event::listenEvents([$event => $target_listeners]); + } + } + } + + // 更新缓存 + Cache::tag("addon")->set("addon_event_list", [ + 'addon_events' => $new_addon_events, + 'merged_listeners' => $new_merged_listeners + ]); + } catch (\Exception $e) { + // 计划任务异常,直接返回 + log_write('InitAddon初始化插件异常: ' . $e->getMessage(), 'error'); + //formatThrowForDebugWithExit($th); + } + } +} diff --git a/src/app/event/init/InitConfig.php b/src/app/event/init/InitConfig.php index a056251c6..fe5e4a143 100644 --- a/src/app/event/init/InitConfig.php +++ b/src/app/event/init/InitConfig.php @@ -13,6 +13,7 @@ class InitConfig { public function handle() { + log_write('InitConfig 初始化配置信息', 'info'); // 初始化常量 $this->initConst(); //初始化配置信息 diff --git a/src/app/event/init/InitRoute.php b/src/app/event/init/InitRoute.php index cfa0974e4..ee95a52c8 100644 --- a/src/app/event/init/InitRoute.php +++ b/src/app/event/init/InitRoute.php @@ -1,45 +1,75 @@ initEvent(); - } - private function InitEvent() { - try { - $cache = Cache::get("addon_event_list"); - if(empty($cache)) { - $addon_model = new Addon(); - $addon_data = $addon_model->getAddonList([], 'name'); - $listen_array = []; - foreach ($addon_data['data'] as $k => $v) { - if(file_exists('addon/'.$v['name'].'/config/event.php')) { - $addon_event = require_once 'addon/'.$v['name'].'/config/event.php'; - $listen = isset($addon_event['listen']) ? $addon_event['listen'] : []; - if(!empty($listen)) { - $listen_array[] = $listen; - } - } - } - Cache::tag("addon")->set("addon_event_list", $listen_array); - } else { - $listen_array = $cache; - } - if(!empty($listen_array)) { - foreach ($listen_array as $k => $listen) { - if(!empty($listen)) { - Event::listenEvents($listen); - } - } - } - } catch (\Throwable $th) { - formatThrowForDebugWithExit($th); - } +namespace app\event\init; + +use think\app\Service; +use think\facade\Route; +use app\model\system\Addon; +use think\facade\Cache; +use app\model\web\WebSite; + +define("initroute_tag", 1); +/** + * 初始化路由规则 + * @author Administrator + * + */ +class InitRoute extends Service +{ + + public function handle() + { + if (defined('BIND_MODULE') && BIND_MODULE === 'install') + return; + //检测当前pathinfo + $pathinfo = request()->pathinfo(); + $pathinfo_array = explode('/', $pathinfo); + $url = request()->domain(); + $check_model = $pathinfo_array[0]; + //检测当前插件情况 + $addon = in_array($check_model, $GLOBALS['system_array']) ? '' : $check_model; + if (!empty($addon)) { + $module = isset($pathinfo_array[1]) ? $pathinfo_array[1] : 'shop'; + $controller = isset($pathinfo_array[2]) ? $pathinfo_array[2] : 'index'; + $method = isset($pathinfo_array[3]) ? $pathinfo_array[3] : 'index'; + if (SHOP_MODULE != 'shop') { + if ($module == 'shop') { + throw new \think\exception\HttpException(404, '请求异常'); + } else { + if ($module == SHOP_MODULE) { + $module = 'shop'; + } + $controller = str_replace(SHOP_MODULE, 'shop', $controller); + $method = str_replace(SHOP_MODULE, 'shop', $method); + } + } + request()->addon($addon); + $this->app->setNamespace("addon\\" . $addon . '\\' . $module); + $this->app->setAppPath($this->app->getRootPath() . 'addon' . DIRECTORY_SEPARATOR . $addon . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR); + } else { + $module = isset($pathinfo_array[0]) ? $pathinfo_array[0] : 'shop'; + $controller = isset($pathinfo_array[1]) ? $pathinfo_array[1] : 'index'; + $method = isset($pathinfo_array[2]) ? $pathinfo_array[2] : 'index'; + if (SHOP_MODULE != 'shop') { + if ($module == 'shop') { + throw new \think\exception\HttpException(404, '请求异常'); + } else { + if ($module == SHOP_MODULE) { + $module = 'shop'; + } + $controller = str_replace(SHOP_MODULE, 'shop', $controller); + $method = str_replace(SHOP_MODULE, 'shop', $method); + } + } + $this->app->setNamespace("app\\" . $module); + $this->app->setAppPath($this->app->getRootPath() . 'app' . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR); + } + $addons = ['bundling', 'coupon', 'discount', 'freeshipping', 'manjian', 'membercancel', 'memberconsume', 'memberprice', 'membersignin', 'niusms', 'weapp', 'wechat', 'wechatpay', 'diy_default1', 'diy_default2']; + //解析路由 + $pathinfo = str_replace(".html", '', $pathinfo); + $controller = str_replace(".html", '', $controller); + $method = str_replace(".html", '', $method); + request()->module($module); + Route::rule($pathinfo, $module . '/' . $controller . '/' . $method); } -} \ No newline at end of file +}