<?php
/**
* PHPnow Template 模板引擎 1.0
* @copyright (C) 2011-2013 PHPnow
* @license http://www.phpnow.cn
* @author jiaodu QQ:1286522207
*/
namespace PHPnow;
/**
* 模板编译类
* @author sanliang
*/
class compile {
public function __construct(&$content, \PHPnow\template $template, $templateFile) {
$this->__source = $content;
$this->__template = $template;
$this->__templateFile = $templateFile;
$this->__ldel = preg_quote($this->__template->__leftDelimiter);
$this->__rdel = preg_quote($this->__template->__rightDelimiter);
$content = preg_replace("/<\?xml(.*?)\?>/s", "##XML\\1XML##", $content);
$this->parsePhp($content);
$content = preg_replace_callback("/##XML(.*?)XML##/s", array($this, 'xmlSubstitution'), $content);
$this->compileTemplate($content);
$content = "<?php class_exists('PHPnow')?:exit;?>" . $content;
$this->stripWhitespace($content);
}
private function parsePhp(&$content) {
if (!$this->__template->__phpOff)
$content = str_replace(array("<?", "?>"), array("<?", "?>"), $content);
else
$content = preg_replace('/(<\?(?!php|=|$))/i', '<?php echo \'\\1\'; ?>', $content); return $content;
}
private function xmlSubstitution($capture) {
return "<?php echo '<?xml " . stripslashes($capture[1]) . " ?>'; ?>";
}
protected function compileTemplate(&$content) {
$tagRegexp = str_replace(array("__PHPNOW", "PHPNOW__"), array($this->__ldel, $this->__rdel), $this->__tagList);
$tagRegexp = "/" . implode("|", $tagRegexp) . "/";
$content = preg_split($tagRegexp, $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$this->pathReplace($content);
$this->compileCode($content);
return $content;
}
private function reducePath(&$path) {
$path = str_replace("://", "@not_replace@", $path);
$path = str_replace("//", "/", $path);
$path = str_replace("@not_replace@", "://", $path);
$path = preg_replace('/\w+\/\.\.\//', '', $path);
}
private function pathReplace(&$content) {
if ($this->__template->__pathReplace) {
$path = $this->__template->__pathUrl;
$this->reducePath($path);
$exp = $sub = array();
if (in_array("img", $this->__template->__pathReplaceList)) {
$exp = array('/<img(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<img(.*?)src=(?:")([^"]+?)#(?:")/i', '/<img(.*?)src="(.*?)"/', '/<img(.*?)src=(?:\@)([^"]+?)(?:\@)/i');
$sub = array('<img$1src=@$2://$3@', '<img$1src=@$2@', '<img$1src="' . $path . '$2"', '<img$1src="$2"');
} if (in_array("script", $this->__template->__pathReplaceList)) {
$exp = array_merge($exp, array('/<script(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<script(.*?)src=(?:")([^"]+?)#(?:")/i', '/<script(.*?)src="(.*?)"/', '/<script(.*?)src=(?:\@)([^"]+?)(?:\@)/i'));
$sub = array_merge($sub, array('<script$1src=@$2://$3@', '<script$1src=@$2@', '<script$1src="' . $path . '$2"', '<script$1src="$2"'));
} if (in_array("link", $this->__template->__pathReplaceList)) {
$exp = array_merge($exp, array('/<link(.*?)href=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<link(.*?)href=(?:")([^"]+?)#(?:")/i', '/<link(.*?)href="(.*?)"/', '/<link(.*?)href=(?:\@)([^"]+?)(?:\@)/i'));
$sub = array_merge($sub, array('<link$1href=@$2://$3@', '<link$1href=@$2@', '<link$1href="' . $path . '$2"', '<link$1href="$2"'));
} if (in_array("a", $this->__template->__pathReplaceList)) {
$exp = array_merge($exp, array('/<a(.*?)href=(?:")(http\:\/\/|https\:\/\/|javascript:)([^"]+?)(?:")/i', '/<a(.*?)href="(.*?)"/', '/<a(.*?)href=(?:\@)([^"]+?)(?:\@)/i'));
$sub = array_merge($sub, array('<a$1href=@$2$3@', '<a$1href="' . $this->__template->__baseUrl . '$2"', '<a$1href="$2"'));
} if (in_array("input", $this->__template->__pathReplaceList)) {
$exp = array_merge($exp, array('/<input(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<input(.*?)src=(?:")([^"]+?)#(?:")/i', '/<input(.*?)src="(.*?)"/', '/<input(.*?)src=(?:\@)([^"]+?)(?:\@)/i'));
$sub = array_merge($sub, array('<input$1src=@$2://$3@', '<input$1src=@$2@', '<input$1src="' . $path . '$2"', '<input$1src="$2"'));
} $content = preg_replace($exp, $sub, $content);
}
}
private function compileCode(&$content) {
$compiledCode = $open_if = $comment_is_open = $ignore_is_open = null;
$loop_level = 0;
while ($html = array_shift($content)) {
if (!$comment_is_open && ( strpos($html, $this->__template->__leftDelimiter . '/ignore' . $this->__template->__rightDelimiter) !== false || strpos($html, '*' . $this->__template->__rightDelimiter) !== false ))
$ignore_is_open = false; elseif ($ignore_is_open) {
} elseif (strpos($html, $this->__template->__leftDelimiter . '/noparse' . $this->__template->__rightDelimiter) !== false)
$comment_is_open = false; elseif ($comment_is_open)
$compiledCode .= $html; elseif (strpos($html, $this->__template->__leftDelimiter . 'ignore' . $this->__template->__rightDelimiter) !== false || strpos($html, '{*') !== false)
$ignore_is_open = true; elseif (strpos($html, $this->__template->__leftDelimiter . 'noparse' . $this->__template->__rightDelimiter) !== false)
$comment_is_open = true; elseif (preg_match('/' . $this->__ldel . 'include="([^"]*)"(?: cache="([^"]*)"){0,1}' . $this->__rdel . '/', $html, $code)) {
$include_var = $this->varReplace($code[1], $left_delimiter = null, $right_delimiter = null, $php_left_delimiter = '".', $php_right_delimiter = '."', $loop_level);
$compiledCode .=isset($code[2]) ? '<?php $tpl = clone $this;' . '$tpl->__caching = true;' . '$tpl->__acheLifetime=' . $code[2] . ';' . '$tpl->display("' . $include_var . '");' . '?>' : '<?php $tpl = clone $this;' . '$tpl->display("' . $include_var . '");' . '?>';
} elseif (preg_match('/' . $this->__ldel . 'config="([^"]*)"(?: file="([^"]*)"){0,1}' . $this->__rdel . '/', $html, $code)) {
$include_var = $this->varReplace($code[1], $left_delimiter = null, $right_delimiter = null, $php_left_delimiter = '".', $php_right_delimiter = '."', $loop_level);
$compiledCode .=isset($code[2]) ? '<?php echo $this->getConfig("' . $include_var . '","' . $code[2] . '");' . '?>' : '<?php echo $this->getConfig("' . $include_var . '");' . '?>';
} elseif (preg_match('/' . $this->__ldel . 'loop(?: name){0,1}="\${0,1}([^"]*)"' . $this->__rdel . '/', $html, $code)) {
$loop_level++;
$var = $this->varReplace('$' . $code[1], $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level - 1);
$counter = "\$counter$loop_level";
$key = "\$key$loop_level";
$value = "\$value$loop_level";
$compiledCode .= "<?php $counter=-1; if( isset($var) && is_array($var) && sizeof($var) ) foreach( $var as $key => $value ){ $counter++; ?>";
} elseif (strpos($html, $this->__template->__leftDelimiter . '/loop' . $this->__template->__rightDelimiter) !== false) {
$counter = "\$counter$loop_level";
$loop_level--;
$compiledCode .= "<?php } ?>";
} elseif (preg_match('/' . $this->__ldel . 'if(?: condition){0,1}="([^"]*)"' . $this->__rdel . '/', $html, $code)) {
$open_if++;
$tag = $code[0];
$condition = $code[1];
$this->functionCheck($tag);
$parsed_condition = $this->varReplace($condition, $tag_left
基于PHP的nowtemplateengine模板引擎源码.zip
版权申诉
27 浏览量
2023-08-27
23:54:55
上传
评论
收藏 38KB ZIP 举报
毕业_设计
- 粉丝: 1936
- 资源: 1万+
最新资源
- java-leetcode题解之第443题压缩字符串.zip
- java-leetcode题解之第415题字符串相加.zip
- java-leetcode题解之第387题字符串中的第一个唯一字符.zip
- java-leetcode题解之第165题比较版本号.zip
- java-leetcode题解之第67题二进制求和.zip
- javaleetcode题解之第58题最后一个单词的长度.zip
- 基于Javascript和Vue的微信小程序抽奖打地鼠游戏设计源码 - 抽奖打地鼠
- 基于Python和Javascript的车展大屏演示前后端web应用设计源码 - autoshow
- 基于Javascript和微信小程序的Anna设计源码
- 基于Java的仿制品设计源码 - bilibili
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈