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