首页 > 代码库 > PHP面向对象之前端控制器模式

PHP面向对象之前端控制器模式

/*

前端控制器的主要组成部分及功能如下:
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面向对象之前端控制器模式