vr-shopxo-source/thinkphp/library/think/Template.php

1319 lines
47 KiB
PHP
Raw Normal View History

2018-12-25 05:35:44 +00:00
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think;
use think\exception\TemplateNotFoundException;
/**
* ThinkPHP分离出来的模板引擎
* 支持XML标签和普通标签的模板解析
* 编译型模板引擎 支持动态缓存
*/
class Template
{
protected $app;
/**
* 模板变量
* @var array
*/
protected $data = [];
/**
* 模板配置参数
* @var array
*/
protected $config = [
'view_path' => '', // 模板路径
'view_base' => '',
'view_suffix' => 'html', // 默认模板文件后缀
'view_depr' => DIRECTORY_SEPARATOR,
'cache_suffix' => 'php', // 默认模板缓存后缀
'tpl_deny_func_list' => 'echo,exit', // 模板引擎禁用函数
'tpl_deny_php' => false, // 默认模板引擎是否禁用PHP原生代码
'tpl_begin' => '{', // 模板引擎普通标签开始标记
'tpl_end' => '}', // 模板引擎普通标签结束标记
'strip_space' => false, // 是否去除模板文件里面的html空格与换行
'tpl_cache' => true, // 是否开启模板编译缓存,设为false则每次都会重新编译
'compile_type' => 'file', // 模板编译类型
'cache_prefix' => '', // 模板缓存前缀标识,可以动态改变
'cache_time' => 0, // 模板缓存有效期 0 为永久,(以数字为值,单位:秒)
'layout_on' => false, // 布局模板开关
'layout_name' => 'layout', // 布局模板入口文件
'layout_item' => '{__CONTENT__}', // 布局模板的内容替换标识
'taglib_begin' => '{', // 标签库标签开始标记
'taglib_end' => '}', // 标签库标签结束标记
'taglib_load' => true, // 是否使用内置标签库之外的其它标签库,默认自动检测
'taglib_build_in' => 'cx', // 内置标签库名称(标签使用不必指定标签库名称),以逗号分隔 注意解析顺序
'taglib_pre_load' => '', // 需要额外加载的标签库(须指定标签库名称),多个以逗号分隔
'display_cache' => false, // 模板渲染缓存
'cache_id' => '', // 模板缓存ID
'tpl_replace_string' => [],
'tpl_var_identify' => 'array', // .语法变量识别array|object|'', 为空时自动识别
'default_filter' => 'htmlentities', // 默认过滤方法 用于普通标签输出
];
/**
* 保留内容信息
* @var array
*/
private $literal = [];
/**
* 模板包含信息
* @var array
*/
private $includeFile = [];
/**
* 模板存储对象
* @var object
*/
protected $storage;
/**
* 架构函数
* @access public
* @param array $config
*/
public function __construct(App $app, array $config = [])
{
$this->app = $app;
$this->config['cache_path'] = $app->getRuntimePath() . 'temp/';
$this->config = array_merge($this->config, $config);
$this->config['taglib_begin_origin'] = $this->config['taglib_begin'];
$this->config['taglib_end_origin'] = $this->config['taglib_end'];
$this->config['taglib_begin'] = preg_quote($this->config['taglib_begin'], '/');
$this->config['taglib_end'] = preg_quote($this->config['taglib_end'], '/');
$this->config['tpl_begin'] = preg_quote($this->config['tpl_begin'], '/');
$this->config['tpl_end'] = preg_quote($this->config['tpl_end'], '/');
// 初始化模板编译存储器
$type = $this->config['compile_type'] ? $this->config['compile_type'] : 'File';
$this->storage = Loader::factory($type, '\\think\\template\\driver\\', null);
}
public static function __make(Config $config)
{
return new static($config->pull('template'));
}
/**
* 模板变量赋值
* @access public
* @param mixed $name
* @param mixed $value
* @return void
*/
public function assign($name, $value = '')
{
if (is_array($name)) {
$this->data = array_merge($this->data, $name);
} else {
$this->data[$name] = $value;
}
}
/**
* 模板引擎参数赋值
* @access public
* @param mixed $name
* @param mixed $value
*/
public function __set($name, $value)
{
$this->config[$name] = $value;
}
/**
* 模板引擎配置项
* @access public
* @param array|string $config
* @return void|array
*/
public function config($config)
{
if (is_array($config)) {
$this->config = array_merge($this->config, $config);
} elseif (isset($this->config[$config])) {
return $this->config[$config];
}
}
/**
* 模板变量获取
* @access public
* @param string $name 变量名
* @return mixed
*/
public function get($name = '')
{
if ('' == $name) {
return $this->data;
}
$data = $this->data;
foreach (explode('.', $name) as $key => $val) {
if (isset($data[$val])) {
$data = $data[$val];
} else {
$data = null;
break;
}
}
return $data;
}
/**
* 渲染模板文件
* @access public
* @param string $template 模板文件
* @param array $vars 模板变量
* @param array $config 模板参数
* @return void
*/
public function fetch($template, $vars = [], $config = [])
{
if ($vars) {
$this->data = $vars;
}
if ($config) {
$this->config($config);
}
$cache = $this->app['cache'];
if (!empty($this->config['cache_id']) && $this->config['display_cache']) {
// 读取渲染缓存
$cacheContent = $cache->get($this->config['cache_id']);
if (false !== $cacheContent) {
echo $cacheContent;
return;
}
}
$template = $this->parseTemplateFile($template);
if ($template) {
$cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($this->config['layout_on'] . $this->config['layout_name'] . $template) . '.' . ltrim($this->config['cache_suffix'], '.');
if (!$this->checkCache($cacheFile)) {
// 缓存无效 重新模板编译
$content = file_get_contents($template);
$this->compiler($content, $cacheFile);
}
// 页面缓存
ob_start();
ob_implicit_flush(0);
// 读取编译存储
$this->storage->read($cacheFile, $this->data);
// 获取并清空缓存
$content = ob_get_clean();
if (!empty($this->config['cache_id']) && $this->config['display_cache']) {
// 缓存页面输出
$cache->set($this->config['cache_id'], $content, $this->config['cache_time']);
}
echo $content;
}
}
/**
* 渲染模板内容
* @access public
* @param string $content 模板内容
* @param array $vars 模板变量
* @param array $config 模板参数
* @return void
*/
public function display($content, $vars = [], $config = [])
{
if ($vars) {
$this->data = $vars;
}
if ($config) {
$this->config($config);
}
$cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($content) . '.' . ltrim($this->config['cache_suffix'], '.');
if (!$this->checkCache($cacheFile)) {
// 缓存无效 模板编译
$this->compiler($content, $cacheFile);
}
// 读取编译存储
$this->storage->read($cacheFile, $this->data);
}
/**
* 设置布局
* @access public
* @param mixed $name 布局模板名称 false 则关闭布局
* @param string $replace 布局模板内容替换标识
* @return object
*/
public function layout($name, $replace = '')
{
if (false === $name) {
// 关闭布局
$this->config['layout_on'] = false;
} else {
// 开启布局
$this->config['layout_on'] = true;
// 名称必须为字符串
if (is_string($name)) {
$this->config['layout_name'] = $name;
}
if (!empty($replace)) {
$this->config['layout_item'] = $replace;
}
}
return $this;
}
/**
* 检查编译缓存是否有效
* 如果无效则需要重新编译
* @access private
* @param string $cacheFile 缓存文件名
* @return boolean
*/
private function checkCache($cacheFile)
{
if (!$this->config['tpl_cache'] || !is_file($cacheFile) || !$handle = @fopen($cacheFile, "r")) {
return false;
}
// 读取第一行
preg_match('/\/\*(.+?)\*\//', fgets($handle), $matches);
if (!isset($matches[1])) {
return false;
}
$includeFile = unserialize($matches[1]);
if (!is_array($includeFile)) {
return false;
}
// 检查模板文件是否有更新
foreach ($includeFile as $path => $time) {
if (is_file($path) && filemtime($path) > $time) {
// 模板文件如果有更新则缓存需要更新
return false;
}
}
// 检查编译存储是否有效
return $this->storage->check($cacheFile, $this->config['cache_time']);
}
/**
* 检查编译缓存是否存在
* @access public
* @param string $cacheId 缓存的id
* @return boolean
*/
public function isCache($cacheId)
{
if ($cacheId && $this->config['display_cache']) {
// 缓存页面输出
return $this->app['cache']->has($cacheId);
}
return false;
}
/**
* 编译模板文件内容
* @access private
* @param string $content 模板内容
* @param string $cacheFile 缓存文件名
* @return void
*/
private function compiler(&$content, $cacheFile)
{
// 判断是否启用布局
if ($this->config['layout_on']) {
if (false !== strpos($content, '{__NOLAYOUT__}')) {
// 可以单独定义不使用布局
$content = str_replace('{__NOLAYOUT__}', '', $content);
} else {
// 读取布局模板
$layoutFile = $this->parseTemplateFile($this->config['layout_name']);
if ($layoutFile) {
// 替换布局的主体内容
$content = str_replace($this->config['layout_item'], $content, file_get_contents($layoutFile));
}
}
} else {
$content = str_replace('{__NOLAYOUT__}', '', $content);
}
// 模板解析
$this->parse($content);
if ($this->config['strip_space']) {
/* 去除html空格与换行 */
$find = ['~>\s+<~', '~>(\s+\n|\r)~'];
$replace = ['><', '>'];
$content = preg_replace($find, $replace, $content);
}
// 优化生成的php代码
$content = preg_replace('/\?>\s*<\?php\s(?!echo\b|\bend)/s', '', $content);
// 模板过滤输出
$replace = $this->config['tpl_replace_string'];
$content = str_replace(array_keys($replace), array_values($replace), $content);
// 添加安全代码及模板引用记录
$content = '<?php /*' . serialize($this->includeFile) . '*/ ?>' . "\n" . $content;
// 编译存储
$this->storage->write($cacheFile, $content);
$this->includeFile = [];
}
/**
* 模板解析入口
* 支持普通标签和TagLib解析 支持自定义标签库
* @access public
* @param string $content 要解析的模板内容
* @return void
*/
public function parse(&$content)
{
// 内容为空不解析
if (empty($content)) {
return;
}
// 替换literal标签内容
$this->parseLiteral($content);
// 解析继承
$this->parseExtend($content);
// 解析布局
$this->parseLayout($content);
// 检查include语法
$this->parseInclude($content);
// 替换包含文件中literal标签内容
$this->parseLiteral($content);
// 检查PHP语法
$this->parsePhp($content);
// 获取需要引入的标签库列表
// 标签库只需要定义一次,允许引入多个一次
// 一般放在文件的最前面
// 格式:<taglib name="html,mytag..." />
// 当TAGLIB_LOAD配置为true时才会进行检测
if ($this->config['taglib_load']) {
$tagLibs = $this->getIncludeTagLib($content);
if (!empty($tagLibs)) {
// 对导入的TagLib进行解析
foreach ($tagLibs as $tagLibName) {
$this->parseTagLib($tagLibName, $content);
}
}
}
// 预先加载的标签库 无需在每个模板中使用taglib标签加载 但必须使用标签库XML前缀
if ($this->config['taglib_pre_load']) {
$tagLibs = explode(',', $this->config['taglib_pre_load']);
foreach ($tagLibs as $tag) {
$this->parseTagLib($tag, $content);
}
}
// 内置标签库 无需使用taglib标签导入就可以使用 并且不需使用标签库XML前缀
$tagLibs = explode(',', $this->config['taglib_build_in']);
foreach ($tagLibs as $tag) {
$this->parseTagLib($tag, $content, true);
}
// 解析普通模板标签 {$tagName}
$this->parseTag($content);
// 还原被替换的Literal标签
$this->parseLiteral($content, true);
}
/**
* 检查PHP语法
* @access private
* @param string $content 要解析的模板内容
* @return void
* @throws \think\Exception
*/
private function parsePhp(&$content)
{
// 短标签的情况要将<?标签用echo方式输出 否则无法正常输出xml标识
$content = preg_replace('/(<\?(?!php|=|$))/i', '<?php echo \'\\1\'; ?>' . "\n", $content);
// PHP语法检查
if ($this->config['tpl_deny_php'] && false !== strpos($content, '<?php')) {
throw new Exception('not allow php tag');
}
}
/**
* 解析模板中的布局标签
* @access private
* @param string $content 要解析的模板内容
* @return void
*/
private function parseLayout(&$content)
{
// 读取模板中的布局标签
if (preg_match($this->getRegex('layout'), $content, $matches)) {
// 替换Layout标签
$content = str_replace($matches[0], '', $content);
// 解析Layout标签
$array = $this->parseAttr($matches[0]);
if (!$this->config['layout_on'] || $this->config['layout_name'] != $array['name']) {
// 读取布局模板
$layoutFile = $this->parseTemplateFile($array['name']);
if ($layoutFile) {
$replace = isset($array['replace']) ? $array['replace'] : $this->config['layout_item'];
// 替换布局的主体内容
$content = str_replace($replace, $content, file_get_contents($layoutFile));
}
}
} else {
$content = str_replace('{__NOLAYOUT__}', '', $content);
}
}
/**
* 解析模板中的include标签
* @access private
* @param string $content 要解析的模板内容
* @return void
*/
private function parseInclude(&$content)
{
$regex = $this->getRegex('include');
$func = function ($template) use (&$func, &$regex, &$content) {
if (preg_match_all($regex, $template, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$array = $this->parseAttr($match[0]);
$file = $array['file'];
unset($array['file']);
// 分析模板文件名并读取内容
$parseStr = $this->parseTemplateName($file);
foreach ($array as $k => $v) {
// 以$开头字符串转换成模板变量
if (0 === strpos($v, '$')) {
$v = $this->get(substr($v, 1));
}
$parseStr = str_replace('[' . $k . ']', $v, $parseStr);
}
$content = str_replace($match[0], $parseStr, $content);
// 再次对包含文件进行模板分析
$func($parseStr);
}
unset($matches);
}
};
// 替换模板中的include标签
$func($content);
}
/**
* 解析模板中的extend标签
* @access private
* @param string $content 要解析的模板内容
* @return void
*/
private function parseExtend(&$content)
{
$regex = $this->getRegex('extend');
$array = $blocks = $baseBlocks = [];
$extend = '';
$func = function ($template) use (&$func, &$regex, &$array, &$extend, &$blocks, &$baseBlocks) {
if (preg_match($regex, $template, $matches)) {
if (!isset($array[$matches['name']])) {
$array[$matches['name']] = 1;
// 读取继承模板
$extend = $this->parseTemplateName($matches['name']);
// 递归检查继承
$func($extend);
// 取得block标签内容
$blocks = array_merge($blocks, $this->parseBlock($template));
return;