欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

一个做页面静态化的php类

程序员文章站 2022-07-04 12:12:15
php3.2,或者其他具有“单一入口且mvc模式”的其他php框架。 * * 使用方式:在controller的构造方法中获取其对象;在controller的销毁方法里,用其对象的_static...
php3.2,或者其他具有“单一入口且mvc模式”的其他php框架。
 *
 * 使用方式:在controller的构造方法中获取其对象;在controller的销毁方法里,用其对象的_static方法。
 *      例:xxxcontroller extends basecontroller.
 *         basecontroller:
 *         function __construct(){
 *             $this->__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: https://".$_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: https://www.baidu.com");
//                header("location: https://".$_server['http_host'].'/'.$_server['request_uri']);
            }*/
        }
    }
    /* 创建目录 */
    private function _mkdir($path){
        if (!file_exists($path)){
            $this->_mkdir(dirname($path));
            mkdir($path, 0777);
        }
    }
}
?>