feat: 同步下新环境

This commit is contained in:
笔记本里的永平
2025-07-07 11:31:25 +08:00
parent 3d1050db3d
commit 86c261ba70
196 changed files with 13146 additions and 29319 deletions

View File

@@ -14,13 +14,13 @@ namespace think\view\driver;
use think\App;
use think\exception\TemplateNotFoundException;
use think\Loader;
use think\Log;
use think\Request;
class Php
{
// 模板引擎参数
protected $config = [
// 默认模板渲染规则 1 解析为小写+下划线 2 全部转换小写
'auto_rule' => 1,
// 视图基础目录(集中式)
'view_base' => '',
// 模板起始路径
@@ -28,23 +28,22 @@ class Php
// 模板文件后缀
'view_suffix' => 'php',
// 模板文件名分隔符
'view_depr' => DIRECTORY_SEPARATOR,
'view_depr' => DS,
// 默认模板渲染规则 1 解析为小写+下划线 2 全部转换小写
'auto_rule' => 1,
];
protected $template;
protected $app;
protected $content;
public function __construct(App $app, $config = [])
public function __construct($config = [])
{
$this->app = $app;
$this->config = array_merge($this->config, (array) $config);
$this->config = array_merge($this->config, $config);
}
/**
* 检测是否存在模板文件
* @access public
* @param string $template 模板文件或者模板规则
* @param string $template 模板文件或者模板规则
* @return bool
*/
public function exists($template)
@@ -53,15 +52,14 @@ class Php
// 获取模板文件名
$template = $this->parseTemplate($template);
}
return is_file($template);
}
/**
* 渲染模板文件
* @access public
* @param string $template 模板文件
* @param array $data 模板变量
* @param string $template 模板文件
* @param array $data 模板变量
* @return void
*/
public function fetch($template, $data = [])
@@ -70,17 +68,13 @@ class Php
// 获取模板文件名
$template = $this->parseTemplate($template);
}
// 模板不存在 抛出异常
if (!is_file($template)) {
throw new TemplateNotFoundException('template not exists:' . $template, $template);
}
$this->template = $template;
// 记录视图信息
$this->app
->log('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]');
App::$debug && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info');
extract($data, EXTR_OVERWRITE);
include $this->template;
@@ -89,8 +83,8 @@ class Php
/**
* 渲染模板内容
* @access public
* @param string $content 模板内容
* @param array $data 模板变量
* @param string $content 模板内容
* @param array $data 模板变量
* @return void
*/
public function display($content, $data = [])
@@ -104,65 +98,52 @@ class Php
/**
* 自动定位模板文件
* @access private
* @param string $template 模板文件规则
* @param string $template 模板文件规则
* @return string
*/
private function parseTemplate($template)
{
if (empty($this->config['view_path'])) {
$this->config['view_path'] = $this->app->getModulePath() . 'view' . DIRECTORY_SEPARATOR;
$this->config['view_path'] = App::$modulePath . 'view' . DS;
}
$request = $this->app['request'];
$request = Request::instance();
// 获取视图根目录
if (strpos($template, '@')) {
// 跨模块调用
list($module, $template) = explode('@', $template);
}
if ($this->config['view_base']) {
// 基础视图目录
$module = isset($module) ? $module : $request->module();
$path = $this->config['view_base'] . ($module ? $module . DIRECTORY_SEPARATOR : '');
$path = $this->config['view_base'] . ($module ? $module . DS : '');
} else {
$path = isset($module) ? $this->app->getAppPath() . $module . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR : $this->config['view_path'];
$path = isset($module) ? APP_PATH . $module . DS . 'view' . DS : $this->config['view_path'];
}
$depr = $this->config['view_depr'];
if (0 !== strpos($template, '/')) {
$template = str_replace(['/', ':'], $depr, $template);
$controller = Loader::parseName($request->controller());
if ($controller) {
if ('' == $template) {
// 如果模板文件名为空 按照默认规则定位
$template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . $this->getActionTemplate($request);
$template = str_replace('.', DS, $controller) . $depr . (1 == $this->config['auto_rule'] ? Loader::parseName($request->action(true)) : $request->action());
} elseif (false === strpos($template, $depr)) {
$template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . $template;
$template = str_replace('.', DS, $controller) . $depr . $template;
}
}
} else {
$template = str_replace(['/', ':'], $depr, substr($template, 1));
}
return $path . ltrim($template, '/') . '.' . ltrim($this->config['view_suffix'], '.');
}
protected function getActionTemplate($request)
{
$rule = [$request->action(true), Loader::parseName($request->action(true)), $request->action()];
$type = $this->config['auto_rule'];
return isset($rule[$type]) ? $rule[$type] : $rule[0];
}
/**
* 配置模板引擎
* @access private
* @param string|array $name 参数名
* @param mixed $value 参数值
* @param string|array $name 参数名
* @param mixed $value 参数值
* @return void
*/
public function config($name, $value = null)
@@ -176,8 +157,4 @@ class Php
}
}
public function __debugInfo()
{
return ['config' => $this->config];
}
}

View File

@@ -14,18 +14,16 @@ namespace think\view\driver;
use think\App;
use think\exception\TemplateNotFoundException;
use think\Loader;
use think\Log;
use think\Request;
use think\Template;
class Think
{
// 模板引擎实例
private $template;
private $app;
// 模板引擎参数
protected $config = [
// 默认模板渲染规则 1 解析为小写+下划线 2 全部转换小写
'auto_rule' => 1,
// 视图基础目录(集中式)
'view_base' => '',
// 模板起始路径
@@ -33,27 +31,27 @@ class Think
// 模板文件后缀
'view_suffix' => 'html',
// 模板文件名分隔符
'view_depr' => DIRECTORY_SEPARATOR,
'view_depr' => DS,
// 是否开启模板编译缓存,设为false则每次都会重新编译
'tpl_cache' => true,
// 默认模板渲染规则 1 解析为小写+下划线 2 全部转换小写
'auto_rule' => 1,
];
public function __construct(App $app, $config = [])
public function __construct($config = [])
{
$this->app = $app;
$this->config = array_merge($this->config, (array) $config);
$this->config = array_merge($this->config, $config);
if (empty($this->config['view_path'])) {
$this->config['view_path'] = $app->getModulePath() . 'view' . DIRECTORY_SEPARATOR;
$this->config['view_path'] = App::$modulePath . 'view' . DS;
}
$this->template = new Template($app, $this->config);
$this->template = new Template($this->config);
}
/**
* 检测是否存在模板文件
* @access public
* @param string $template 模板文件或者模板规则
* @param string $template 模板文件或者模板规则
* @return bool
*/
public function exists($template)
@@ -62,16 +60,15 @@ class Think
// 获取模板文件名
$template = $this->parseTemplate($template);
}
return is_file($template);
}
/**
* 渲染模板文件
* @access public
* @param string $template 模板文件
* @param array $data 模板变量
* @param array $config 模板参数
* @param string $template 模板文件
* @param array $data 模板变量
* @param array $config 模板参数
* @return void
*/
public function fetch($template, $data = [], $config = [])
@@ -80,25 +77,21 @@ class Think
// 获取模板文件名
$template = $this->parseTemplate($template);
}
// 模板不存在 抛出异常
if (!is_file($template)) {
throw new TemplateNotFoundException('template not exists:' . $template, $template);
}
// 记录视图信息
$this->app
->log('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]');
App::$debug && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info');
$this->template->fetch($template, $data, $config);
}
/**
* 渲染模板内容
* @access public
* @param string $template 模板内容
* @param array $data 模板变量
* @param array $config 模板参数
* @param string $template 模板内容
* @param array $data 模板变量
* @param array $config 模板参数
* @return void
*/
public function display($template, $data = [], $config = [])
@@ -109,62 +102,49 @@ class Think
/**
* 自动定位模板文件
* @access private
* @param string $template 模板文件规则
* @param string $template 模板文件规则
* @return string
*/
private function parseTemplate($template)
{
// 分析模板文件规则
$request = $this->app['request'];
$request = Request::instance();
// 获取视图根目录
if (strpos($template, '@')) {
// 跨模块调用
list($module, $template) = explode('@', $template);
}
if ($this->config['view_base']) {
// 基础视图目录
$module = isset($module) ? $module : $request->module();
$path = $this->config['view_base'] . ($module ? $module . DIRECTORY_SEPARATOR : '');
$path = $this->config['view_base'] . ($module ? $module . DS : '');
} else {
$path = isset($module) ? $this->app->getAppPath() . $module . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR : $this->config['view_path'];
$path = isset($module) ? APP_PATH . $module . DS . 'view' . DS : $this->config['view_path'];
}
$depr = $this->config['view_depr'];
if (0 !== strpos($template, '/')) {
$template = str_replace(['/', ':'], $depr, $template);
$controller = Loader::parseName($request->controller());
if ($controller) {
if ('' == $template) {
// 如果模板文件名为空 按照默认规则定位
$template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . $this->getActionTemplate($request);
$template = str_replace('.', DS, $controller) . $depr . (1 == $this->config['auto_rule'] ? Loader::parseName($request->action(true)) : $request->action());
} elseif (false === strpos($template, $depr)) {
$template = str_replace('.', DIRECTORY_SEPARATOR, $controller) . $depr . $template;
$template = str_replace('.', DS, $controller) . $depr . $template;
}
}
} else {
$template = str_replace(['/', ':'], $depr, substr($template, 1));
}
return $path . ltrim($template, '/') . '.' . ltrim($this->config['view_suffix'], '.');
}
protected function getActionTemplate($request)
{
$rule = [$request->action(true), Loader::parseName($request->action(true)), $request->action()];
$type = $this->config['auto_rule'];
return isset($rule[$type]) ? $rule[$type] : $rule[0];
}
/**
* 配置或者获取模板引擎参数
* @access private
* @param string|array $name 参数名
* @param mixed $value 参数值
* @param string|array $name 参数名
* @param mixed $value 参数值
* @return mixed
*/
public function config($name, $value = null)
@@ -184,9 +164,4 @@ class Think
{
return call_user_func_array([$this->template, $method], $params);
}
public function __debugInfo()
{
return ['config' => $this->config];
}
}