Library.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://gitee.com/zoujingli/ThinkLibrary
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkLibrary
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkLibrary
  13. // +----------------------------------------------------------------------
  14. namespace think\admin;
  15. use Closure;
  16. use think\admin\command\Database;
  17. use think\admin\command\Install;
  18. use think\admin\command\Queue;
  19. use think\admin\command\Replace;
  20. use think\admin\command\Version;
  21. use think\admin\multiple\BuildUrl;
  22. use think\admin\multiple\command\Build;
  23. use think\admin\multiple\command\Clear;
  24. use think\admin\multiple\Multiple;
  25. use think\admin\service\AdminService;
  26. use think\admin\service\SystemService;
  27. use think\middleware\LoadLangPack;
  28. use think\middleware\SessionInit;
  29. use think\Request;
  30. use think\Service;
  31. use function Composer\Autoload\includeFile;
  32. /**
  33. * 模块注册服务
  34. * Class Library
  35. * @package think\admin
  36. */
  37. class Library extends Service
  38. {
  39. /**
  40. * 版本号
  41. */
  42. const VERSION = '6.0.23';
  43. /**
  44. * 启动服务
  45. */
  46. public function boot()
  47. {
  48. // 服务初始化处理
  49. $this->app->event->listen('HttpRun', function (Request $request) {
  50. // 配置默认输入过滤
  51. $request->filter(['trim']);
  52. // 注册多应用中间键
  53. $this->app->middleware->add(Multiple::class);
  54. // 判断访问模式兼容处理
  55. if ($request->isCli()) {
  56. // 兼容 CLI 访问控制器
  57. if (empty($_SERVER['REQUEST_URI']) && isset($_SERVER['argv'][1])) {
  58. $request->setPathinfo($_SERVER['argv'][1]);
  59. }
  60. } else {
  61. // 兼容 HTTP 调用 Console 后 URL 问题
  62. $request->setHost($request->host());
  63. }
  64. });
  65. // 替换 ThinkPHP 地址
  66. $this->app->bind('think\route\Url', BuildUrl::class);
  67. // 替换 ThinkPHP 指令
  68. $this->commands(['build' => Build::class, 'clear' => Clear::class]);
  69. // 注册 ThinkAdmin 指令
  70. $this->commands([Queue::class, Install::class, Version::class, Database::class, Replace::class]);
  71. // 动态应用运行参数
  72. SystemService::instance()->bindRuntime();
  73. }
  74. /**
  75. * 初始化服务
  76. */
  77. public function register()
  78. {
  79. // 加载中文语言
  80. $this->app->lang->load(__DIR__ . '/lang/zh-cn.php', 'zh-cn');
  81. $this->app->lang->load(__DIR__ . '/lang/en-us.php', 'en-us');
  82. // 终端 HTTP 访问时特殊处理
  83. if (!$this->app->request->isCli()) {
  84. // 如果是 YAR 接口或指定情况下,不需要初始化会话和语言包,否则有可能会报错
  85. $isYarRpc = stripos($this->app->request->header('user_agent', ''), 'PHP Yar RPC-');
  86. if ($isYarRpc === false && intval($this->app->request->get('not_init_session', 0)) < 1) {
  87. // 注册会话初始化中间键
  88. $this->app->middleware->add(SessionInit::class);
  89. // 注册语言包处理中间键
  90. $this->app->middleware->add(LoadLangPack::class);
  91. }
  92. // 注册访问处理中间键
  93. $this->app->middleware->add(function (Request $request, Closure $next) {
  94. $header = [];
  95. if (($origin = $request->header('origin', '*')) !== '*') {
  96. $header['Access-Control-Allow-Origin'] = $origin;
  97. $header['Access-Control-Allow-Methods'] = 'GET,PUT,POST,PATCH,DELETE';
  98. $header['Access-Control-Allow-Headers'] = 'Authorization,Content-Type,If-Match,If-Modified-Since,If-None-Match,If-Unmodified-Since,X-Requested-With,Api-Name,Api-Type,Api-Token,User-Form-Token,User-Token,Token';
  99. $header['Access-Control-Expose-Headers'] = 'Api-Name,Api-Type,Api-Token,User-Form-Token,User-Token,Token';
  100. $header['Access-Control-Allow-Credentials'] = 'true';
  101. }
  102. // 访问模式及访问权限检查
  103. if ($request->isOptions()) {
  104. return response()->code(204)->header($header);
  105. } elseif (AdminService::instance()->check()) {
  106. return $next($request)->header($header);
  107. } elseif (AdminService::instance()->isLogin()) {
  108. return json(['code' => 0, 'info' => lang('think_library_not_auth')])->header($header);
  109. } else {
  110. return json(['code' => 0, 'info' => lang('think_library_not_login'), 'url' => sysuri('admin/login/index')])->header($header);
  111. }
  112. }, 'route');
  113. }
  114. // 动态加载应用初始化系统函数
  115. [$ds, $base] = [DIRECTORY_SEPARATOR, $this->app->getBasePath()];
  116. foreach (glob("{$base}*{$ds}sys.php") as $file) includeFile($file);
  117. // 动态加载插件初始化系统函数
  118. $base = "{$this->app->getBasePath()}addons{$ds}";
  119. if (file_exists($base) && is_dir($base)) {
  120. foreach (glob("{$base}*{$ds}sys.php") as $file) includeFile($file);
  121. }
  122. }
  123. }