模板内容由值传递改为引用
This commit is contained in:
parent
7f90071773
commit
85ddec7fdd
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace top\library\template\driver\engine;
|
namespace top\library\template\driver\engine;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use top\traits\Instance;
|
use top\traits\Instance;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -63,10 +62,9 @@ class Engine
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理模板继承
|
* 处理模板继承
|
||||||
* @param $template
|
* @param &$template
|
||||||
* @return mixed
|
|
||||||
*/
|
*/
|
||||||
private function parseExtend($template)
|
private function parseExtend(&$template)
|
||||||
{
|
{
|
||||||
$pattern = '/' . $this->left . 'extend\s+file[\s\S]*?=[\s\S]*?[\'"](.*?)[\'"][\s\S]*?\/' . $this->right . '/is';
|
$pattern = '/' . $this->left . 'extend\s+file[\s\S]*?=[\s\S]*?[\'"](.*?)[\'"][\s\S]*?\/' . $this->right . '/is';
|
||||||
preg_match($pattern, $template, $matches);
|
preg_match($pattern, $template, $matches);
|
||||||
|
@ -80,8 +78,7 @@ class Engine
|
||||||
$extendFileContent = file_get_contents($file);
|
$extendFileContent = file_get_contents($file);
|
||||||
}
|
}
|
||||||
// 处理继承中的include标签
|
// 处理继承中的include标签
|
||||||
// $tempContent = $this->parseInclude($extendFileContent);
|
$this->parseInclude($extendFileContent);
|
||||||
// $extendFileContent = $tempContent !== false ? $tempContent : $extendFileContent;
|
|
||||||
// 被继承模板中的块
|
// 被继承模板中的块
|
||||||
preg_match_all($blockPattern, $extendFileContent, $extendResult);
|
preg_match_all($blockPattern, $extendFileContent, $extendResult);
|
||||||
// 继承模板中的块
|
// 继承模板中的块
|
||||||
|
@ -111,15 +108,13 @@ class Engine
|
||||||
}
|
}
|
||||||
$template = str_replace($searchArray, $replaceArray, $extendFileContent);
|
$template = str_replace($searchArray, $replaceArray, $extendFileContent);
|
||||||
}
|
}
|
||||||
return $template;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理include标签
|
* 处理include标签
|
||||||
* @param $template
|
* @param &$template
|
||||||
* @return null|string|string[]
|
|
||||||
*/
|
*/
|
||||||
private function parseInclude($template)
|
private function parseInclude(&$template)
|
||||||
{
|
{
|
||||||
$pattern = '/' . $this->left . 'include\s+file[\s\S]*?=[\s\S]*?[\'"](.*?)[\'"][\s\S]*?\/' . $this->right . '/is';
|
$pattern = '/' . $this->left . 'include\s+file[\s\S]*?=[\s\S]*?[\'"](.*?)[\'"][\s\S]*?\/' . $this->right . '/is';
|
||||||
$template = preg_replace_callback($pattern, function ($result) {
|
$template = preg_replace_callback($pattern, function ($result) {
|
||||||
|
@ -132,28 +127,27 @@ class Engine
|
||||||
}, $template);
|
}, $template);
|
||||||
// 处理多层include
|
// 处理多层include
|
||||||
if ($this->hasInclude($template)) {
|
if ($this->hasInclude($template)) {
|
||||||
$template = $this->parseInclude($template);
|
$this->parseInclude($template);
|
||||||
}
|
}
|
||||||
return $template;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检测是否含有include
|
* 检测是否含有include
|
||||||
* @param $template
|
* @param $template
|
||||||
* @return int|false
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function hasInclude($template)
|
private function hasInclude($template)
|
||||||
{
|
{
|
||||||
$pattern = '/' . $this->left . 'include\s+file[\s\S]*?=[\s\S]*?[\'"](.*?)[\'"][\s\S]*?\/' . $this->right . '/is';
|
$pattern = '/' . $this->left . 'include\s+file[\s\S]*?=[\s\S]*?[\'"](.*?)[\'"][\s\S]*?\/' . $this->right . '/is';
|
||||||
return preg_match($pattern, $template, $matches);
|
preg_match($pattern, $template, $matches);
|
||||||
|
return !empty($matches);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 分析参数以及函数输出
|
* 分析参数以及函数输出
|
||||||
* @param $template
|
* @param &$template
|
||||||
* @return mixed
|
|
||||||
*/
|
*/
|
||||||
private function parseVars($template)
|
private function parseVars(&$template)
|
||||||
{
|
{
|
||||||
preg_match_all('/{(.*?)}/', $template, $matches);
|
preg_match_all('/{(.*?)}/', $template, $matches);
|
||||||
$search = [];
|
$search = [];
|
||||||
|
@ -179,8 +173,6 @@ class Engine
|
||||||
if (!empty($search) && !empty($replace)) {
|
if (!empty($search) && !empty($replace)) {
|
||||||
$template = str_replace($search, $replace, $template);
|
$template = str_replace($search, $replace, $template);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $template;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -246,7 +238,7 @@ class Engine
|
||||||
public function loadTaglib($prefix, $className)
|
public function loadTaglib($prefix, $className)
|
||||||
{
|
{
|
||||||
if ($prefix == 'default') {
|
if ($prefix == 'default') {
|
||||||
throw new Exception('扩展标签库前缀不能为default');
|
throw new \Exception('扩展标签库前缀不能为default');
|
||||||
}
|
}
|
||||||
$this->libs[$prefix] = $className;
|
$this->libs[$prefix] = $className;
|
||||||
}
|
}
|
||||||
|
@ -294,10 +286,9 @@ class Engine
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 进行标签处理
|
* 进行标签处理
|
||||||
* @param $template
|
* @param &$template
|
||||||
* @return null|string|string[]
|
|
||||||
*/
|
*/
|
||||||
private function parseTags($template)
|
private function parseTags(&$template)
|
||||||
{
|
{
|
||||||
foreach ($this->getTags() as $name => $item) {
|
foreach ($this->getTags() as $name => $item) {
|
||||||
$pattern = '/' . $this->left . '(?:(' . $name . ')\b(?>[^' . $this->right . ']*)|\/(' . $name . '))';
|
$pattern = '/' . $this->left . '(?:(' . $name . ')\b(?>[^' . $this->right . ']*)|\/(' . $name . '))';
|
||||||
|
@ -335,9 +326,8 @@ class Engine
|
||||||
// 得到准备替换的值
|
// 得到准备替换的值
|
||||||
$replace = explode($cut, $this->getTagParseResult($name, $attr, $cut));
|
$replace = explode($cut, $this->getTagParseResult($name, $attr, $cut));
|
||||||
$replace = [
|
$replace = [
|
||||||
// 递归解析标签,使之可以在自定义标签中嵌套标签
|
(isset($replace[0])) ? $replace[0] : [],
|
||||||
(isset($replace[0])) ? $this->parseTags($replace[0]) : '',
|
(isset($replace[1])) ? $replace[1] : [],
|
||||||
(isset($replace[1])) ? $replace[1] : '',
|
|
||||||
];
|
];
|
||||||
while ($startArray) {
|
while ($startArray) {
|
||||||
$begin = end($startArray);
|
$begin = end($startArray);
|
||||||
|
@ -374,7 +364,6 @@ class Engine
|
||||||
}, $template);
|
}, $template);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $template;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -401,14 +390,13 @@ class Engine
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理original标签
|
* 处理original标签
|
||||||
* @param $template
|
* @param &$template
|
||||||
* @return null|string|string[]
|
|
||||||
*/
|
*/
|
||||||
private function parseOriginal($template)
|
private function parseOriginal(&$template)
|
||||||
{
|
{
|
||||||
$pattern = '/' . $this->left . 'original' . $this->right . '([\s\S]*?)';
|
$pattern = '/' . $this->left . 'original' . $this->right . '([\s\S]*?)';
|
||||||
$pattern .= $this->left . '\/original' . $this->right . '/is';
|
$pattern .= $this->left . '\/original' . $this->right . '/is';
|
||||||
return preg_replace_callback($pattern, function ($matches) {
|
$template = preg_replace_callback($pattern, function ($matches) {
|
||||||
return str_replace([
|
return str_replace([
|
||||||
$this->left, $this->right,
|
$this->left, $this->right,
|
||||||
'{', '}'
|
'{', '}'
|
||||||
|
@ -421,12 +409,11 @@ class Engine
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 还原original内容
|
* 还原original内容
|
||||||
* @param $template
|
* @param &$template
|
||||||
* @return mixed
|
|
||||||
*/
|
*/
|
||||||
private function returnOriginal($template)
|
private function returnOriginal(&$template)
|
||||||
{
|
{
|
||||||
return str_replace([
|
$template = str_replace([
|
||||||
'<!ORIGINAL--', '--ORIGINAL>',
|
'<!ORIGINAL--', '--ORIGINAL>',
|
||||||
'<!PARAM--', '--PARAM>'
|
'<!PARAM--', '--PARAM>'
|
||||||
], [
|
], [
|
||||||
|
@ -443,17 +430,17 @@ class Engine
|
||||||
public function compile($template)
|
public function compile($template)
|
||||||
{
|
{
|
||||||
// 处理original标签
|
// 处理original标签
|
||||||
$template = $this->parseOriginal($template);
|
$this->parseOriginal($template);
|
||||||
// 处理模板继承标签
|
// 处理模板继承标签
|
||||||
$template = $this->parseExtend($template);
|
$this->parseExtend($template);
|
||||||
// 处理include标签
|
// 处理include标签
|
||||||
$template = $this->parseInclude($template);
|
$this->parseInclude($template);
|
||||||
// 处理定义的标签
|
// 处理定义的标签
|
||||||
$template = $this->parseTags($template);
|
$this->parseTags($template);
|
||||||
// 处理变量以及函数
|
// 处理变量以及函数
|
||||||
$template = $this->parseVars($template);
|
$this->parseVars($template);
|
||||||
// 还原original内容
|
// 还原original内容
|
||||||
$template = $this->returnOriginal($template);
|
$this->returnOriginal($template);
|
||||||
// 清除多余开始结束标签
|
// 清除多余开始结束标签
|
||||||
$template = preg_replace('/\?>([\r|\n|\s]*?)<\?php/is', '', $template);
|
$template = preg_replace('/\?>([\r|\n|\s]*?)<\?php/is', '', $template);
|
||||||
|
|
||||||
|
|
|
@ -126,7 +126,7 @@ class Tags
|
||||||
$parse = '<?php $' . $attr['name'] . ' = ' . (is_numeric($attr['value']) ? $attr['value'] : $quot . $attr['value'] . $quot) . '; ?>';
|
$parse = '<?php $' . $attr['name'] . ' = ' . (is_numeric($attr['value']) ? $attr['value'] : $quot . $attr['value'] . $quot) . '; ?>';
|
||||||
return $parse;
|
return $parse;
|
||||||
}
|
}
|
||||||
throw new Exception('assign标签必须拥有属性:' . $this->defaultTags['assign']['attr']);
|
throw new Exception('assign标签必须拥有属性:' . $this->tags['assign']['attr']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -143,7 +143,7 @@ class Tags
|
||||||
$parse .= '<?php endswitch; ?>';
|
$parse .= '<?php endswitch; ?>';
|
||||||
return $parse;
|
return $parse;
|
||||||
}
|
}
|
||||||
throw new Exception('switch标签必须拥有属性:' . $this->defaultTags['switch']['attr']);
|
throw new Exception('switch标签必须拥有属性:' . $this->tags['switch']['attr']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue