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) {