From eed59e2413c0659d0cce8e56964471560f0d68ec Mon Sep 17 00:00:00 2001 From: Anyon Date: Tue, 30 Dec 2025 16:52:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/think-library/src/Library.php | 20 +++++++++++++++++-- .../src/support/middleware/MultAccess.php | 3 ++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/plugin/think-library/src/Library.php b/plugin/think-library/src/Library.php index 5185e673e..cfa7910cb 100644 --- a/plugin/think-library/src/Library.php +++ b/plugin/think-library/src/Library.php @@ -30,6 +30,7 @@ use think\admin\support\command\Sysmenu; use think\admin\support\middleware\JwtSession; use think\admin\support\middleware\MultAccess; use think\admin\support\middleware\RbacAccess; +use think\exception\HttpException; use think\exception\HttpResponseException; use think\middleware\LoadLangPack; use think\Request; @@ -100,6 +101,21 @@ class Library extends Service }); } + /** + * 动态加载文件 + * @param string $file + * @return mixed + */ + public static function load(string $file) + { + try { + return include $file; + } catch (\Throwable|\Error $error) { + trace_file($error); + throw new HttpException(500, $error->getMessage()); + } + } + /** * 初始化服务 * @return void @@ -109,9 +125,9 @@ class Library extends Service // 动态加载全局配置 [$dir, $ext] = [$this->app->getBasePath(), $this->app->getConfigExt()]; ToolsExtend::find($dir, 2, function (SplFileInfo $info) use ($ext) { - $info->isFile() && $info->getBasename() === "sys{$ext}" && include_once $info->getPathname(); + $info->isFile() && $info->getBasename() === "sys{$ext}" && Library::load($info->getPathname()); }); - if (is_file($file = "{$dir}common{$ext}")) include_once $file; + if (is_file($file = "{$dir}common{$ext}")) Library::load($file); if (is_file($file = "{$dir}provider{$ext}")) $this->app->bind(include $file); if (is_file($file = "{$dir}event{$ext}")) $this->app->loadEvent(include $file); if (is_file($file = "{$dir}middleware{$ext}")) $this->app->middleware->import(include $file, 'app'); diff --git a/plugin/think-library/src/support/middleware/MultAccess.php b/plugin/think-library/src/support/middleware/MultAccess.php index 5d4ef7950..a98582d7f 100644 --- a/plugin/think-library/src/support/middleware/MultAccess.php +++ b/plugin/think-library/src/support/middleware/MultAccess.php @@ -21,6 +21,7 @@ namespace think\admin\support\middleware; use Closure; use SplFileInfo; use think\admin\extend\ToolsExtend; +use think\admin\Library; use think\admin\Plugin; use think\admin\service\NodeService; use think\admin\service\SystemService; @@ -168,7 +169,7 @@ class MultAccess { [$ext, $fmaps] = [$this->app->getConfigExt(), []]; // 加载应用函数文件 - if (is_file($file = "{$appPath}common{$ext}")) include_once $file; + if (is_file($file = "{$appPath}common{$ext}")) Library::load($file); // 加载应用配置文件 ToolsExtend::find($appPath . 'config', 1, function (SplFileInfo $info) use ($ext) { if ($info->isFile() && strtolower(".{$info->getExtension()}") === $ext) {