<?php

include_once SERVER_ROOT.'/library/smarty/Smarty.class.php';

class extSmarty extends Smarty {
    private $tempdata;
    private $cache_id;

    public function __construct($tpl_dir='') {
        parent::Smarty();
        $this->template_dir = SMARTY_TPL_PATH;
        // 私有路径传递
        if($tpl_dir) $this->template_dir = $tpl_dir;

        $this->compile_dir = SMARTY_COMPILE_PATH;
        if(!file_exists($this->compile_dir)) {
            mkdir($this->compile_dir,755);
        }
        $this->cache_dir_base = SMARTY_CACHE_PATH;
        if(!file_exists($this->cache_dir_base)) {
            mkdir($this->cache_dir_base,755);
        }
        $this->cache_dir = $this->cache_dir_base;
    }

    public function assign($key, $value = null) {
        if(is_object($value)) {
            parent::assign($key,$value);
        } else {
            parent::assign_by_ref($key,$value);
        }
    }

    public function setCacheDir($cacheid) {
        $this->cache_dir = $this->cache_dir_base ;
    }

    public function SetCache($bCache = false,$lifeTime = 0,$cacheId = "") {
        if ($bCache) {
            if ($lifeTime == 0) {
                $this->caching = true;
            } else {
                $this->caching = 2;
                $this->cache_lifetime = $lifeTime;
            }
            $this->compile_check = true;
            if ($cacheId == "")
                $this->cache_id = "http://" . $_SERVER ['HTTP_HOST'] . $_SERVER ['REQUEST_URI']; else
                $this->cache_id = $cacheId;
            $this->SetCacheDir ( $this->cache_id );
        } else {
            $this->caching = false;
        }
    }

    public function display($tpl_file, $cache_id = null, $compile_id = null) {
        if($cache_id==null){
            $cache_id = $this->cache_id;
        }
        $bRet = parent::display($tpl_file, $cache_id, $compile_id);
        parent::clear_all_assign();
        return $bRet;
    }

    public function is_cached($tpl_file, $cache_id = null, $compile_id = null) {
        if (!isset($cache_id)) {
            $cache_id = $this->cache_id;
        }
        $bRet = parent::is_cached($tpl_file, $cache_id, $compile_id);
        return $bRet;
    }

    function fetch($resource_name, $cache_id = null, $compile_id = null, $display = false) {
        if (!isset($cache_id)) {
            $cache_id = $this->cache_id;
        }
        return parent::fetch($resource_name, $cache_id, $compile_id, $display);
    }
}