PHP物件導向之前端控制器模式

來源:互聯網
上載者:User

標籤:require   blog   top   win   null   app   對比   步驟   val   

/*前端控制器的主要組成部分及功能如下:1、入口檔案類controller;(對這個系統的調用都是從這個檔案開始的,也相當於一個控制中心,對所有相關的類進行調用)2、應用程式配置資訊類applicationhelper;(用於擷取應用程式所需的配置資訊)3、命令類解譯器commandresolver;(根據使用者請求調用相應的命令類)4、命令類command;(調用使用者請求資訊類和商務邏輯,還可以調用視圖檔案)整個系統的調用步驟大概就是:1、擷取程式所需的配置資訊2、擷取命令類3、執行命令類前端控制器與命令模式對比,在我看來二者基本沒什麼區別,原理大致相同。前端控制器的代碼及註解如下:*/namespace woo\controller;//入口檔案類class Controller {    private $applicationHelper;    private function __construct ();        static function run(){        $instance = new Controller();        $instance->init();        $instance->handleRequest();    }        function init(){                    //擷取程式所需的配置資訊            $applicationHelper = ApplicationHelper::instance();        $applicationHelper->init();    }        function handleRequest(){            //擷取並執行命令類        $request = new \woo\controller\Request();        $cmd_r = new \woo\command\CommandResolver();        $cmd = $cmd_r->getCommand($request);        $cmd->execute($request);    }}//擷取應用程式配置資訊class ApplicationHelper {    private static $instance;    private $config = "/tmp/data/woo_options.xml";        //配置資訊儲存的檔案        private function __construct (){}        static function instance(){        if(!self::$instance){            selff::$instance = new self();        }        return self::$instance;    }        function init(){        $dsn = \woo\base\ApplicatonRegistry::getDSN();            if(!is_null($dsn)){            return;        }        $this->getOptions();    }        private function getOptions(){        $this->ensure(file_exists($this->config),"conld not find options file");            $options = SimpleXml_load_file($this->config);        print get_class($options);        $dsn = (string)$options->dsn;        $this->ensure($dsn,"No DSN found");        \woo\base\ApplicationRegistry::setDSN($dsn);        //配置資訊將被緩衝到一個註冊表類中        //設定其他值            }        private function ensure($expr,$message){        if(!$expr){            throw new \woo\base\AppException($message);        }    }    }//使用者請求資訊類 class Request {    private $properties;    private $feedback =array();    function __construct(){        $this->init();        \woo\base\RequestRegistry::setRequest($this);    }        function init(){        if(isset($_SERVER[‘REQUEST_METHOD‘])){            $this->properties = $_REQUEST;            return;        }        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];        }    }        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);    }    }namespace woo\command;//命令直譯器 class CommandResolver{    private static $base_cmd;    private Static $default_cmd;        function __construct (){        if(!self::$base_cmd){            //命令類的基類,這個主要將使用反射來檢尋找到的命令類是否是繼承了它            self::$base_cmd = new \ReflectionClass("\woo\command\Command");                self::$default_cmd = new DefaultCommand();    //預設的命令類,當找不到相應的命令類時將調用它        }    }        //擷取命令類    function getCommand(\woo\controller\Request $request){        $cmd = $request->getProperty(‘cmd‘);        $sep = DIRECTORY_SEPARATOR;            //代表"/"        if(!$cmd){            return 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");            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");                }            }        }        $request->addFeedback("command ‘$cmd‘ not found");        return clone self::$default_cmd;    }}//命令類abstract  class Command{    final function __construct(){}    function execute(\woo\controller\Request $request){        $this->doExecute($request);    }    abstract function doExecute(\woo\controller\Request $request);}class DefaultCommand extends Command{    function doExecute(\woo\controller\Request $request){        $request->addFeedback("Welcome to Woo");        include("woo/view/main.php");    }}//用戶端require("woo/controller/Controller.php");\woo\controller\Controller::run();

 

PHP物件導向之前端控制器模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.