八. .PHP模式設計—-企業模式(1)

(*暫時未拆分前端控制器和應用控制器,全部集成在Command類實現)

1 註冊表模式
//註冊表模式
//註冊表模式用於提供一個系統級別對象,在任何地方都方便訪問(可以使用單例模式)
class Registry{
    private static $instance;
    private $request;
    private function __construct(){}
    static function instance(){
        if(!isset(self::$instance)){
            self::$instance=new self();
        }
        return self::$instance;
    }
    function getRequest(){
        $this->request;
    }
    function setRequest(Request $request){
        $this->request=$request;
    }
}
class Request{}  

2 三種作用域下的註冊表
namespace woo\controller;
class Request{}
class Complex{}
//創建一個具有作用域的註冊表模式
//請求級別註冊表
namespace woo\base;
use woo;
abstract class Registry{
    abstract protected function get($key);
    abstract protected function set($key,$val); 
}
class RequestRegistry extends Registry{
    private $values=array();
    private static $instance;
    private function __construct(){}
    //返回唯一實例
    static function instance(){
        if(!isset(self::$instance)){
            self::$instance=new self();
        }
        return self::$instance;
    }
    protected function get($key){
        if(isset($this->values[$key])){
            return isset($this->values[$key]);
        }
        return null;
    }
    protected function set($key, $val){
        $this->values[$key]=$val;
    }
    static function getRequest(){
        return self::instance()->get('request');
    }
    static function setRequest(woo\controller\Request $request){
        return self::instance()->set('request', $request);
    }
}
//會話級別的註冊表
class SessionRegistry extends Registry{
    private static $instance;
    private function __construct(){
        session_start();
    }
    //返回唯一實例
    static function instance(){
        if(!isset(self::$instance)){
            self::$instance=new self();
        }
        return self::$instance;
    }
    protected function get($key){
        if( isset($_SESSION[__CLASS__][$key]) ){
            return isset($_SESSION[__CLASS__][$key]);
        }
        return null;
    }
    protected function set($key, $val){
        $_SESSION[__CLASS__][$key]=$val;
    }
    static function getComplex(){
        return self::instance()->get('complex');
    }
    static function setRequest(woo\controller\Complex $request){
        return self::instance()->set('complex', $request);
    }
}
//應用程式級別的註冊表
class ApplicationRegistry extends Registry{
    private static $instance;
    private $freezedir='Data';
    private $values=array();
    private $mtimes=array();
    private function __construct(){
        session_start();
    }
    //返回唯一實例
    static function instance(){
        if(!isset(self::$instance)){
            self::$instance=new self();
        }
        return self::$instance;
    }
    //get,set都是單獨保存一個$key到文件中
    protected function get($key){
        $path=$this->freezedir.DIRECTORY_SEPARATOR.$key;
        if(file_exists($path)){
            clearstatcache();
            //獲取文件修改時間
            $mtime=filemtime($path);
            if(!isset($this->mtimes[$key])){
                $this->mtimes[$key]=0;
            }
            //如果文件被修改
            if($mtime > $this->mtimes[$key]){
                $data=file_get_contents($path);
                $this->mtimes[$key]=$mtime;
                return ($this->values[$key]=unserialize($data));
            }
        }
        if(isset($this->values[$key])){
            return $this->values[$key];
        }
        return null;
    }
    protected function set($key, $val){
        $this->values[$key]=$val;
        $path=$this->freezedir.DIRECTORY_SEPARATOR.$key;
        //文件不存在會自動創建
        file_put_contents($path, serialize($val));
        $this->mtimes[$key]=time();
    }
    static function getDSN(){
        return self::$instance()->get('DSN');
    }
    static function setDSN($dsn){
        return self::$instance()->set('DSN',$dsn);
    }
}
3 前端控制器Controller----結合註冊表模式與命令模式打造統一入口框架
doExecute($request);
    }
    function disPlay($request){
        //獲取cmd,用於決定調取那個頁面
        $cmd=$request->getProperty('cmd');
        //寫法不是很好....截取command之前的字符
        print $cmd;
        $viewUrl="./Woo/View/".substr($cmd,0,strlen($cmd)-(7))."View.html";
        include_once($viewUrl);
    }
    abstract function doExecute(\Woo\Controller\Request $request);
} 



getProperty('cmd');
        $sep=DIRECTORY_SEPARATOR;
        //找不到指定cmd數據時,返回默認cmd實例
        if(!$cmd){
            return clone self::$default_cmd;
        }
        $cmd=str_replace(array('.',$sep), "", $cmd);
        $filePath=".\Woo{$sep}Command{$sep}{$cmd}.php";
        $className="\Woo\Command\{$cmd}";
        if(file_exists($filePath)){
            require_once("$filePath");
            //判斷傳入的類是否存在,是否是base_cmd的子類
            if(class_exists($className)){
                $cmd_class=new \ReflectionClass($className);
                if($cmd_class->isSubclassOf(self::$base_cmd)){
                    return $cmd_class->newInstance();
                }else{
                    //解析失敗,跳轉到默認頁面
                    $request->addFeedback("command '$cmd' is not a command");
                    return clone self::$default_cmd;
                }
            }
        }else{
            $request->addFeedback("command '$cmd' is not found");
            return clone self::$default_cmd;
        }
    }
}



addFeedback("Welcome to WOO!");
        $feedbacks=$request->getFeedback();
        foreach ($feedbacks as $key=>$val){
            print $val;
            print "
"; } include_once("Woo/View/main.html"); } } addFeedback("Welcome to WOO!"); $feedbacks=$request->getFeedback(); foreach ($feedbacks as $key=>$val){ print $val; print "
"; } $this->disPlay($request); } } freezedir.DIRECTORY_SEPARATOR.$key; if(file_exists($path)){ clearstatcache(); //獲取文件修改時間 $mtime=filemtime($path); if(!isset($this->mtimes[$key])){ $this->mtimes[$key]=0; } //如果文件被修改 if($mtime > $this->mtimes[$key]){ $data=file_get_contents($path); $this->mtimes[$key]=$mtime; return ($this->values[$key]=unserialize($data)); } } if(isset($this->values[$key])){ return $this->values[$key]; } return null; } protected function set($key, $val){ $this->values[$key]=$val; $path=$this->freezedir.DIRECTORY_SEPARATOR.$key; if(!is_dir($this->freezedir)){ mkdir($this->freezedir); } //文件不存在會自動創建 file_put_contents($path, serialize($val)); $this->mtimes[$key]=time(); } static function getDSN(){ return self::instance()->get('DSN'); } static function setDSN($dsn){ return self::instance()->set('DSN',$dsn); } } <?php namespace Woo\Base; //創建一個具有作用域的註冊表模式 //請求級別註冊表 abstract class Registry{ abstract protected function get($key); abstract protected function set($key,$val); } values[$key])){ return isset($this->values[$key]); } return null; } protected function set($key, $val){ $this->values[$key]=$val; } static function getRequest(){ return self::instance()->get('request'); } static function setRequest(\Woo\Controller\Request $request){ return self::instance()->set('request', $request); } } get('complex'); } static function setRequest(Woo\Controller\Complex $request){ return self::instance()->set('complex', $request); } } getOptions(); } //隻有緩存數據不存在時才會調用該方法 private function getOptions(){ $this->ensure(file_exists($this->config), "Could not find options file!"); $options=simplexml_load_file($this->config); print get_class($options); $dsn=$options->dsn; $this->ensure($dsn, "No DSN found!"); //獲取值之後,將其存放進應用程式級別註冊表中,方便緩存使用 //先轉化成數組,方便序列化 \Woo\Base\ApplicationRegistry::setDSN(array($dsn->__toString())); //設置其他值 //... } private function ensure($expr,$message){ if(!$expr){ throw new \Exception($message); } } } <?php namespace Woo\Controller; class Complex{ } init(); //理論上講handleRequest需要在每次請求到來時運行 $instance->handleRequest(); } function init(){ //獲取一個單例,用於做全局配置 $applicationHelper=ApplicationHelper::instance(); $applicationHelper->init(); } //每次請求都需要調用一次 function handleRequest(){ $request=new Request(); $cmd_r=new \Woo\Command\CommandResolver(); //根據request生成對應command抽象類(接口)的子類實例 //之後要利用command執行相關動作 $cmd=$cmd_r->getCommand($request); $cmd->execute($request); } } init(); \Woo\Base\RequestRegistry::setRequest($this); } //同時支持HTTP請求和命令行參數(可用於調試程式使用) //*用於將參數填充進properties屬性裡 function init(){ //表單提交方式 if(isset($_SERVER['REQUEST_METHOD'])){ //用於收集表單提交的數據 $this->properties=$_REQUEST; return; } //$_SERVER['argv'] 傳遞給該腳本的參數 foreach ($_SERVER['argv'] as $arg){ //搜索字符串第一次出現的位置 if(strpos($arg, '=')){ list($key,$val)=explode("=", $arg); $this->setProperty($key,$val); } } } function getProperty($key){ if(isset($this->properties[$key])){ return $this->properties[$key]; } return null; } function setProperty($key,$val){ $this->properties[$key]=$val; } function addFeedback($msg){ array_push($this->feedback, $msg); } function getFeedback(){ return $this->feedback; } function getFeedbackString($separator="\n"){ return implode($separator,$this->feedback); } } //woo_options.xml dsn DSN //框架入口index.php <?php use Woo\Controller\Controller; include_once("./Woo/Controller/Controller.php"); //print_r($_GET); //框架入口 Controller::run();

發佈留言