程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 一個做頁面靜態化的php類

一個做頁面靜態化的php類

編輯:關於PHP編程

一個做頁面靜態化的php類


__sh = StaticHtml::getInstance();
 *         }
 *         function __destruct(){
 *             $this->__sh->_static();
 *         }
 *
 * */
class StaticHtml{
    private static $_instance = null;   /* 單例模式,自身的引用 */
    private $_needStatic = false;   /* 是否需要將其靜態化 */
    private $_needDeleteStatic = false;     /* 是否需要刪除其靜態化的頁面 */
    private $_hasStaticed = true;   /* 是否存在其的靜態化頁面 */
    private $_controller = null;    /* 當前被訪問的controller */
    private $_action = null;        /* 當前被訪問的action */
//    private $_staticAgain = false;   /* 刪除靜態文件後,是否馬上重新更新【【注意】再次請求】 */
    private $_save_path = null;     /* 將要創建或者刪除的靜態文件的路徑 */
    private $_conf = array( /* 此文件定義一些靜態文件的存放方式 */
        'files_per_directory' => 100        /* 此值不允許被修改,除非是要刪除所有已經存在的靜態文件,重新緩存 */
    );
    private $_staticList = array(   /* 此數組,定義了需要創建靜態化頁面的Controller的action */
//        'Base' => array(      /* Base為controller name */
//            'aaa' => array(       /* aaa為action name */
//                'save_path' => '/StaticHtml/Base/aaa/',   /* save_path為生成的靜態文件存放的根路徑 */
//                'static_base' => 'id',        /* static_base為生成靜態文件的“依據”。建議為對應數據庫的primary_key */
//                'alias' => 'aaa'  /* 靜態文件的名字,否則為1.html */
//            )
//        )
        'Mynotes' => array(
            'look_notes' => array(
                'save_path' => '/StaticHtml/Mynotes/look_notes/',
                'static_base' => 'id',
                'alias' => 'note'
            ),
            'add_personal_notes' => array(
                'save_path' => '/StaticHtml/Mynotes/',
                'alias' => 'note-add'
            )
        ),
        'Resource' => array(
            'allResource' => array(
                'save_path' => '/StaticHtml/Resource/',
                'alias' => 'allResource'
            ),
            'resource_add' => array(
                'save_path' => '/StaticHtml/Resource/',
                'alias' => 'resourceAdd'
            )
        ),
        'Thing' => array(
            'suggestion_of_lecture' => array(
                'save_path' => '/StaticHtml/Lecture/',
                'alias' => 'voteLecture'
            )
        ),
        'Passwordfix' => array(
            'index' => array(
                'save_path' => '/StaticHtml/Information/',
                'alias' => 'updatePassword'
            )
        ),
        'Information' => array(
            'index' => array(
                'save_path' => '/StaticHtml/Information/',
                'static_base' => 'user_id',
                'alias' => 'information'
            )
        ),
        'Courseinfo' => array(
            'course_show' => array(
                'save_path' => '/StaticHtml/Information/',
                'static_base' => 'user_id',
                'alias' => 'course'
            )
        )
    );
    private $_deleteList = array(   /* 此數組,定義了需要刪除某些靜態化頁面的Controller的action */
//        'Base' => array(      /* Base為controller name */
//            'bbb' => array(       /* bbb為action name */
//                'save_path' => '/StaticHtml/Base/aaa/',   /* save_path為要刪除的靜態文件存放的根路徑 */
//                'static_base' => 'id',        /* static_base為確定靜態文件路徑的“依據”。建議為對應數據庫的primary_key */
//                'alias' => 'aaa'  /* 靜態文件的名字,否則為1.html */
//            )
//        )
        'Mynotes' => array(
            'edits_notes' => array(
                'save_path' => '/StaticHtml/Mynotes/look_notes/',
                'static_base' => 'id',
                'alias' => 'note'
            )
        ),
        'Information' => array(
            'save_user_info' => array(
                'save_path' => '/StaticHtml/Information/',
                'static_base' => 'user_id',
                'alias' => 'information'
            )
        ),
        'Courseinfo' => array(
            'course_update' => array(
                'save_path' => '/StaticHtml/Information/',
                'static_base' => 'user_id',
                'alias' => 'course'
            )
        )
    );
    private function __construct(){
        $this->needStatic(); /* 確定本次請求是否需要靜態化 */
        $this->hasStaticed(); /* 確定本次請求是否已經存在靜態化頁面 */
        $this->needDeleteStatic(); /* 確定本次請求是否需要刪除某些靜態頁面 */
    }
    /* 確定需要刪除的靜態文件的存放路徑 */
    private function needDeleteStatic(){
        if($this->_needDeleteStatic){
            $save_path = $this->getSavePath($this->_deleteList[$this->_controller][$this->_action]);
            $this->_hasStaticed = false;
            if(file_exists(ROOT_PATH.$save_path)){
                $this->_hasStaticed = true;
            }
//            $this->_staticAgain = $this->_deleteList[$this->_controller][$this->_action]['visitAgain'];
            $this->_save_path = ROOT_PATH.$save_path;
        }
    }
    /* 獲取本類的,唯一的,實例化 */
    public static function getInstance(){
        if(!(self::$_instance instanceof self)){
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    /* 判斷是否存在其靜態化的文件 */
    private function hasStaticed(){
        if($this->_needStatic){
            $save_path = $this->getSavePath($this->_staticList[$this->_controller][$this->_action]);
            if(!file_exists(ROOT_PATH.$save_path)){
                $this->_hasStaticed = false;
                ob_start();
            }else{
                header("location: http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']).$save_path);
            }
            $this->_save_path = ROOT_PATH.$save_path;
        }
    }
    /* 獲取本次請求要生成或者刪除的,靜態化文件的路徑 */
    private function getSavePath($conf){
        if(!isset($conf['static_base'])){
            $save_path = $conf['save_path'];
            $save_path .= $conf['alias'].'.html';
        }else{
            if($conf['static_base'] == 'user_id'){
                $id = (int)$_SESSION['logined_user']['id'];
            }else{
                if(IS_GET){
                    $id = $_GET[$conf['static_base']];
                }else{
                    $id =  $_POST[$conf['static_base']];
                }
            }
            $save_path = $conf['save_path'];
            $directory_id = ceil($id/$this->_conf['files_per_directory']);
            $save_path .= $directory_id.'/';
            if($conf['alias']){
                $fileName = $conf['alias'].'-';
            }
            $fileName .= $id.'.html';
            $save_path .= $fileName;
        }
        return $save_path;
    }
    /* 確定本次請求,是否需要生成靜態化文件 */
    private function needStatic(){
        $url = explode('/',__ACTION__);
        $this->_controller = $url[4];
        $this->_action = $url[5];
        if(isset($this->_staticList[$this->_controller]) && isset($this->_staticList[$this->_controller][$this->_action])){
            $this->_needStatic = true;
        }
        if(isset($this->_deleteList[$this->_controller]) && isset($this->_deleteList[$this->_controller][$this->_action])){
            $this->_needDeleteStatic = true;
        }
    }
    /* 生成,或者刪除,靜態化文件 */
    public function _static(){
        if($this->_needStatic && !$this->_hasStaticed){
            $html = ob_get_contents();
            $this->_mkdir(dirname($this->_save_path));
            file_put_contents($this->_save_path,$html);
        }
        if($this->_needDeleteStatic && $this->_hasStaticed){
            unlink($this->_save_path);
            /*if($this->_staticAgain){
                header("location: http://www.baidu.com");
//                header("location: http://".$_SERVER['HTTP_HOST'].'/'.$_SERVER['REQUEST_URI']);
            }*/
        }
    }
    /* 創建目錄 */
    private function _mkdir($path){
        if (!file_exists($path)){
            $this->_mkdir(dirname($path));
            mkdir($path, 0777);
        }
    }
}
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved