2023-01-18 17:10:33 +08:00

106 lines
3.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* *
* * ============================================================================
* * Created by PhpStorm.
* * User: Ice
* * 邮箱: ice@sbing.vip
* * 网址: https://sbing.vip
* * Date: 2019/9/19 下午3:53
* * ============================================================================.
*/
declare(strict_types=1);
namespace app\common\middleware;
use Closure;
use think\facade\Lang;
use think\Request;
use think\Response;
use think\facade\Env;
use think\facade\Cookie;
use think\facade\Config;
/**
* Fast初始化Admin/Index/Addon都会执行此方法除了Api.
*/
class FastInit
{
/**
* Fast6框架初始化.
*
* @param Request $request
* @param Closure $next
*
* @return Response
*/
public function handle($request, Closure $next)
{
// 设置mbstring字符编码
mb_internal_encoding('UTF-8');
// 修复多语言
app()->lang = new \think\Lang(app(),\config('lang'));
// app()->lang->detect($request);
app()->lang->switchLangSet(Lang::getLangSet());
// 设置替换内容
$this->initReplaceString();
//设置DEBUG环境
$this->initDebugEnv();
// 切换多语言
if ($request->get('lang')) {
Cookie::set('think_var', $request->get('lang'));
}
// Form别名
if (! class_exists('Form')) {
class_alias('fast\\Form', 'Form');
}
return $next($request);
}
/**
* 模板内容替换
*/
private function initReplaceString(){
// 设置替换字符串
$url = ltrim(dirname(app()->request->root()), DIRECTORY_SEPARATOR);
// 如果未设置__CDN__则自动匹配得出
$tpl_replace_string = Config::get('view.tpl_replace_string');
if (!Config::get('view.tpl_replace_string.__CDN__')) {
$tpl_replace_string['__CDN__']=$url;
}
// 如果未设置__PUBLIC__则自动匹配得出
if (!Config::get('view.tpl_replace_string.__PUBLIC__')) {
$tpl_replace_string['__PUBLIC__']= $url . '/';
}
// 如果未设置__ROOT__则自动匹配得出
if (!Config::get('view.tpl_replace_string.__ROOT__')) {
$tpl_replace_string['__ROOT__']= preg_replace("/\/public\/$/", '', $url . '/');
}
Config::set(['tpl_replace_string'=>$tpl_replace_string],'view');
if (! Config::get('site.cdnurl')) {
Config::set(['cdnurl' => $url], 'site');
}
// 如果未设置cdnurl则自动匹配得出
if (! Config::get('upload.cdnurl')) {
Config::set(['cdnurl' => $url], 'upload');
}
}
/**
* 调试模式缓存
*/
private function initDebugEnv(){
if (Env::get('APP_DEBUG')) {
// 如果是调试模式将version置为当前的时间戳可避免缓存
Config::set(['version' => time()], 'site');
//如果是调试模式将关闭视图缓存
Config::set(['tpl_cache' => false], 'view');
// 如果是开发模式那么将异常模板修改成官方的
Config::set(['exception_tmpl' => app()->getThinkPath().'tpl/think_exception.tpl']);
}
}
}