zoukankan      html  css  js  c++  java
  • PHP面向对象之前端控制器模式

    /*
    
    前端控制器的主要组成部分及功能如下:
    1、入口文件类controller;(对这个系统的调用都是从这个文件开始的,也相当于一个控制中心,对所有相关的类进行调用)
    2、应用程序配置信息类applicationhelper;(用于获取应用程序所需的配置信息)
    3、命令类解释器commandresolver;(根据用户请求调用相应的命令类)
    4、命令类command;(调用用户请求信息类和业务逻辑,还可以调用视图文件)
    
    整个系统的调用步骤大概就是:
    1、获取程序所需的配置信息
    2、获取命令类
    3、执行命令类
    
    前端控制器与命令模式对比,在我看来二者基本没什么区别,原理大致相同。前端控制器的代码及注解如下:
    
    */
    
    
    namespace woocontroller;
    
    //入口文件类
    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 woocontrollerRequest();
            $cmd_r = new woocommandCommandResolver();
            $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 = wooaseApplicatonRegistry::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");
            wooaseApplicationRegistry::setDSN($dsn);        //配置信息将被缓存到一个注册表类中
            //设置其他值
            
        }
        
        private function ensure($expr,$message){
            if(!$expr){
                throw new wooaseAppException($message);
            }
        }
        
    }
    
    //用户请求信息类 
    class Request {
        private $properties;
        private $feedback =array();
        function __construct(){
            $this->init();
            wooaseRequestRegistry::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="
    "){
            return implode($separator,$this->feedback);
        }
        
    }
    
    
    
    namespace woocommand;
    
    //命令解释器 
    class CommandResolver{
        private static $base_cmd;
        private Static $default_cmd;
        
        function __construct (){
            if(!self::$base_cmd){
                //命令类的基类,这个主要将使用反射来检查找到的命令类是否是继承了它
                self::$base_cmd = new ReflectionClass("woocommandCommand");    
                self::$default_cmd = new DefaultCommand();    //默认的命令类,当找不到相应的命令类时将调用它
            }
        }
        
        //获取命令类
        function getCommand(woocontrollerRequest $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(woocontrollerRequest $request){
            $this->doExecute($request);
        }
        abstract function doExecute(woocontrollerRequest $request);
    }
    
    
    class DefaultCommand extends Command{
        function doExecute(woocontrollerRequest $request){
            $request->addFeedback("Welcome to Woo");
            include("woo/view/main.php");
        }
    }
    
    
    
    //客户端
    require("woo/controller/Controller.php");
    woocontrollerController::run();
  • 相关阅读:
    Json 格式化, 排序, 标准格式 ,修改字段名 @JSONField @JsonIgnore @JsonIgnoreProperties
    skywalking 服务间链路追踪 (4) --- 项目中追踪各个方法, 监测方法运行时间
    skywalking 服务间链路追踪 (3) ---整合docker使用
    skywalking 服务间链路追踪 (2) ---使用
    skywalking 服务间链路追踪 (1) ---安装
    Exception AOP 异常处理
    messageHelper 统一管理项目的中message
    Json 对象比较
    Nginx 针对建立TCP连接优化
    Nginx 建立三次握手
  • 原文地址:https://www.cnblogs.com/kerryw/p/6936223.html
Copyright © 2011-2022 走看看