首页 >php教程 >PHP源码 >编译型PHP模板引擎大致实现过程

编译型PHP模板引擎大致实现过程

PHP中文网
PHP中文网原创
2016-05-25 17:10:491338浏览

模板引擎

<?php
/**
 * @author Jiawei
 * @Completed in 2012-6-29 0:23
 */
class JTemplate{
    //通过assign函数传入的变量临时存放数组
    private $templateVar = array();
    //模板目录
    private $templateDir = &#39;&#39;;
    //编译目录
    private $templateCompileDir = &#39;&#39;;
     
    private $fileName = &#39;&#39;;
    /**
     * 构造函数
     * @param string $templateDir 模板目录
     * @param string $templateComplieDir 模板编译目录
     */
    public function __construct($templateDir,$templateComplieDir){
        $this->templateDir = $templateDir;
        $this->templateCompileDir = $templateComplieDir; 
    }
    /**
     * 显示模板
     * @param string $fileName 模板文件名
     */
    public function display($fileName){
        $this->fileName = $fileName;
        if(file_exists($this->templateDir.&#39;/&#39;.$this->fileName)){
            $compileFileName = $this->templateCompileDir.&#39;/&#39;.$this->file_safe_name().&#39;.php&#39;;
            if(!file_exists($compileFileName) || filemtime($compileFileName)< filemtime($this->templateDir.&#39;/&#39;.$this->fileName)){
                $this->del_old_file();
                $this->compile();
            }
            extract($this->templateVar);
            include $compileFileName;
        }else{
            $this->error(&#39;Sorry,the template file &#39;.$this->fileName.&#39; does not exist!!&#39;);
        }
    }
    /**
     * 获取编译文件名
     */
    private function get_compile_file(){
        $compileFile = explode(&#39;.&#39;,$this->fileName);
        unset($compileFile[count($compileFile)-1]);
        return implode(&#39;.&#39;,$compileFile);
    }
    /**
     * 编译
     */
    private function compile(){
        $fileHandle = @fopen($this->templateDir.&#39;/&#39;.$this->fileName, &#39;r&#39;);
        while(!feof($fileHandle)){
            $fileContent = fread($fileHandle,1024);
        }
        fclose($fileHandle);
        $fileContent = $this->template_replace($fileContent);
        //$compileFile = $this->get_compile_file($fileName);
        $fileHandle = @fopen($this->templateCompileDir.&#39;/&#39;.$this->file_safe_name().&#39;.php&#39;,&#39;w&#39;);
        if($fileHandle){
            fwrite($fileHandle, $fileContent);
            fclose($fileHandle);
        }else{
            $this->error(&#39;Sorry,Compile dir can not write!&#39;);
        }
    }
    /**
     * 模板传值
     * @param string $valueName 模板中使用的变量名
     * @param $value 变量值
     */
    public function assign($valueName,$value){
        $this->templateVar[$valueName] = $value;
    }
     
    /**
     * 模板正则替换
     * @param string $content 替换内容
     * @return string 替换过后的内容
     */
    private function template_replace($content){
        $orginArray = array(
            &#39;/<!--loop\s+\$(\w+)\s+\$(\w+)-->/s&#39;,
            &#39;/<!--loop\s+\$(\w+)\s+\$(\w+)\s+\$(\w+)-->/s&#39;,
            &#39;/<!--elseloop-->(.+?)<!--endloop-->/s&#39;,
            &#39;/<!--endloop-->/s&#39;,
            &#39;/<!--if\s+\((.+?)\)-->/s&#39;,
            &#39;/<!--endif-->/s&#39;,
            &#39;/<!--elseif\s+\((.+?)\)-->/s&#39;,
            &#39;/<!--else-->/s&#39;,
            &#39;/\{P:(.+?)\:}/s&#39;,
            &#39;/\{C:(\w+)\}/s&#39;,
            &#39;/\{I:(.+?)\}/s&#39;,
            &#39;/\{F:(.+?)\}/s&#39;,
            &#39;/\{EF:(.+?)\}/s&#39;,
            &#39;/\{([a-zA-Z0-9_\[\]\&#39;\"\$\.\x7f-\xff]+)\}/s&#39;,
        );
         
        $changeArray = array(
            &#39;<?php if(!empty($$1)&&is_array($$1)){$countLoop = 1;foreach($$1 as $$2){$countLoop++;?>&#39;,
            &#39;<?php if(!empty($$1)&&is_array($$1)){$countLoop = 1;foreach($$1 as $$2=>$$3){$countLoop++;?>&#39;,
            &#39;<?php }if(!empty($countLoop))$countLoop--;}else{?>$1<?php }?>&#39;,
            &#39;<?php }if(!empty($countLoop))$countLoop--;}?>&#39;,
            &#39;<?php if($1){?>&#39;,
            &#39;<?php }?>&#39;,
            &#39;<?php }elseif($1){?>&#39;,
            &#39;<?php }else{?>&#39;,
            &#39;<?php $1?>&#39;,
            &#39;<?php echo $1;?>&#39;,
            &#39;<?php include_once "&#39;.$this->templateDir.&#39;/$1";?>&#39;,
            &#39;<?php $1;?>&#39;,
            &#39;<?php echo $1;?>&#39;,
            &#39;<?php echo $$1;?>&#39;,
        );
        return $repContent=preg_replace($orginArray,$changeArray,$content);
    }
    /**
     * 删除旧文件
     */
    private function del_old_file(){
        $compileFile = $this->get_compile_file($this->fileName);
        $files = glob($this->templateCompileDir.&#39;/&#39;.$compileFile.&#39;*.php&#39;);
        // print_r($files);
        if($files){
            @unlink($files[0]);
        }
    }
    /**
     * 编译文件名安全处理方法
     * @return string 返回编译文件名
     */
    private function file_safe_name(){
        $compileFile = $this->get_compile_file($this->fileName);
        return $compileFile.filemtime($this->templateDir.&#39;/&#39;.$this->fileName);
    }
     
    /**
     * 错误输出函数
     * @param string $content 错误输出信息
     */
    private function error($content){
        $stringHtml = &#39;<div style="width:780px;height:auto;padding:10px;border:1px solid #CCC;margin:0 auto;">&#39;;
        $stringHtml .= &#39;Error information:<br />&#39;;
        $stringHtml .= &#39;<font color="red">&#39;;
        $stringHtml .= $content;
        $stringHtml .= &#39;</font>&#39;;
        $stringHtml .= &#39;</div>&#39;;
        exit($stringHtml);
    }
}
?>

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn