修改目录结构,值传递改为引用
This commit is contained in:
parent
88ce4744ef
commit
a9ad41e054
|
@ -71,10 +71,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);
|
||||||
|
@ -88,8 +87,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);
|
||||||
// 继承模板中的块
|
// 继承模板中的块
|
||||||
|
@ -119,15 +117,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) {
|
||||||
|
@ -140,9 +136,8 @@ class Engine
|
||||||
}, $template);
|
}, $template);
|
||||||
// 处理多层include
|
// 处理多层include
|
||||||
if ($this->hasInclude($template)) {
|
if ($this->hasInclude($template)) {
|
||||||
$template = $this->parseInclude($template);
|
$this->parseInclude($template);
|
||||||
}
|
}
|
||||||
return $template;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -159,10 +154,9 @@ class Engine
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 分析参数以及函数输出
|
* 分析参数以及函数输出
|
||||||
* @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 = [];
|
||||||
|
@ -188,8 +182,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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -303,10 +295,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 . '))';
|
||||||
|
@ -382,7 +373,6 @@ class Engine
|
||||||
}, $template);
|
}, $template);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $template;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -409,14 +399,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,
|
||||||
'{', '}'
|
'{', '}'
|
||||||
|
@ -429,12 +418,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>'
|
||||||
], [
|
], [
|
||||||
|
@ -451,17 +439,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);
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ $t1 = microtime(true);
|
||||||
// 获取模板引擎实例
|
// 获取模板引擎实例
|
||||||
$config = [
|
$config = [
|
||||||
'ext' => 'html',
|
'ext' => 'html',
|
||||||
'dir' => './view/',
|
'dir' => './templates/',
|
||||||
'left' => '<',
|
'left' => '<',
|
||||||
'right' => '>',
|
'right' => '>',
|
||||||
];
|
];
|
||||||
|
@ -18,10 +18,10 @@ $engine = Engine::instance($config);
|
||||||
// 加载自定义标签库
|
// 加载自定义标签库
|
||||||
$engine->loadTaglib('article', Article::class);
|
$engine->loadTaglib('article', Article::class);
|
||||||
// 读取模板内容
|
// 读取模板内容
|
||||||
$template = file_get_contents('./view/index.html');
|
$template = file_get_contents('./templates/index.html');
|
||||||
// 编译并写入
|
// 编译并写入
|
||||||
$content = $engine->compile($template);
|
$content = $engine->compile($template);
|
||||||
file_put_contents('compile.php', $content);
|
file_put_contents('./compile/result.php', $content);
|
||||||
|
|
||||||
$t2 = microtime(true);
|
$t2 = microtime(true);
|
||||||
|
|
Loading…
Reference in New Issue