[php]应用控制器(一)
前端控制器已经能很好地在一个地方集中处理请求并选择适当的command了,但是command子类对象自己处理了视图的分配工作。要是能够使用一个类(根据command处理后返回的状态值)来决定视图并返回到前端控制器,再由前端控制器来调用视图显示,这样的话前端控制器就处于视图层和业务层的中间了,而且也很好地把command和视图分开了。应用控制器是个好的解决方案。
应用控制器负责映射请求到命令,并映射命令到视图。它允许改变应用程序的流程而不需要修改核心代码。它能把command类解放出来,让command类集中精力完成自己的工作,包括处理输入、调用应用程序逻辑和处理结果等。
应用控制器是一个类(或者一组类),它帮助前端控制接管处理请求的任务而且又把适当的视图返回给前端控制器调用。那么应用控制是什么方式运行的呢?它是通过一个xml配置文件来决定command和视图工作的方式。比如下面这个xml文件(有点像struts的方式):
sqlite://data/demo.dbrootrootmainmainerrorlist_studentsadd_studentliststudentssimple_add_student
可以看到xml中中可以包含、、三类子元素,分别表示的是command对应的视图、command处理业务后的状态值、command处理后的跳转(这里跳转到另一个command)。
从xml的结构就能了解到command类需要一些新的属性status了。
namespace demo\command;/** * 抽象父类 */abstract class command { // 状态值映射 private static $status_strings = array( 'cmd_default' => 0, 'cmd_ok' => 1, 'cmd_error' => 2, 'cmd_insufficient_data' => 3 ); // 当前状态值 private $status = 0; public final function __construct() { // 子类不能重写构造函数 } /** * 按状态字符串返回状态值 * @param unknown_type $stastr */ public static function status($staustr = 'cmd_default') { if (empty($staustr)) { $staustr = 'cmd_default'; } return self::$status_strings[$staustr]; } /** * 调用子类实现的doexecute * @param \demo\controller\request $request */ public function execute(\demo\controller\request $request) { $this->doexecute($request); } protected abstract function doexecute(\demo\controller\request $request);}
系统中有个专门获取配置的助手类applicationhelper可以实现对xml配置的读取。由于xml中的元素结构相对灵活一些,那么就需要一个controllermap来管理各元素中的值和command、视图的一一映射关系。
namespace demo\controller;class controllermap { private $classrootmap = array(); private $forwardmap = array(); private $viewmap = array(); public function addclassroot($cmd, $classroot) { $this->classrootmap[$cmd] = $classroot; } public function getclassroot($cmd) { if (isset($this->classrootmap[$cmd])) { return $this->classrootmap[$cmd]; } return $cmd; } public function addforward($cmd = 'default', $status = 0, $newcmd) { $this->forwardmap[$cmd][$status] = $newcmd; } public function getforward($cmd, $status) { if (isset($this->forwardmap[$cmd][$status])) { return $this->forwardmap[$cmd][$status]; } return null; } public function addview($cmd = 'default', $status = 0, $view) { $this->viewmap[$cmd][$status] = $view; } public function getview($cmd, $status) { if (isset($this->viewmap[$cmd][$status])) { return $this->viewmap[$cmd][$status]; } return null; }}
首先需要获取xml中的配置applicationhelper类的getoptions。
namespace demo\controller;/** * 助手类:获取xml配置 * 单例模式 * */class applicationhelper { private static $instance; private $config = 'data/config.xml'; private function __construct() { } public static function getinstance() { if (isset(self::$instance) == false) { self::$instance = new self(); } return self::$instance; } public function init() { // 初始化配置从序列化文件中获取 $dsn = \demo\base\applicationregistry::getinstance()->getdsn(); $camp = \demo\base\applicationregistry::getinstance()->getcontrollermap(); if (is_null($dsn) || is_null($camp)) { $this->getoptions(); } } /** * 获取xml配置 */ public function getoptions() { // xml $this->ensure(file_exists($this->config), could not find options file!); $options = @simplexml_load_file($this->config); var_dump($options); $this->ensure($options instanceof \simplexmlelement, 'could not resolve options file!'); // $dsn = (string)$options->dsn; $this->ensure($dsn, 'no dsn found!'); \demo\base\applicationregistry::getinstance()->setdsn($dsn); // $map = new controllermap(); // foreach ($options->controller->view as $default_view) { $staustr = trim((string)$default_view['status']); $status = \demo\command\command::status($staustr); $map->addview('default', $status, (string)$default_view); } // foreach ($options->controller->command as $cvf) { $cmd = trim((string)$cvf['name']); // if($cvf->classalias) { $classroot = (string)$cvf->classalias['name']; $map->addclassroot($cmd, $classroot); } // 、 if ($cvf->view) { $view = trim((string)$cvf->view); $forward = trim((string)$cvf->forward); $map->addview($cmd, 0, $view); if ($forward) { $map->addforward($cmd, 0, $forward); } } // foreach ($cvf->status as $status) { $staustr = trim((string)$status['value']); $view = trim((string)$status->view); $forward = trim((string)$status->forward); $stau = \demo\command\command::status($staustr); if ($view) { $map->addview($cmd, $stau, $view); } if ($forward) { $map->addforward($cmd, $stau, $forward); } } } var_dump($map); \demo\base\applicationregistry::getinstance()->setcontrollermap($map); } private function ensure($expr, $msg) { if (!$expr) { throw new \demo\base\appexception($msg); } }}
获取xml配置的过程是一个比较费时的操作,可以先把controllermap对象序列化到文件中去,之后可以通过applicationregistry获取并把它当做全局数据缓存起来。
/** * application作用域 */class applicationregistry extends registry { private static $instance; private $freezedir = ./data; // 此处硬编码,具体根据实际情况配置 private $values = array(); private $mtimes = array(); private function __construct() { } public static function getinstance() { if (isset(self::$instance) == false) { self::$instance = new self(); } return self::$instance; } /** * 从序列化文件中获取$key数据 */ protected function get($key) { $path = $this->freezedir . directory_separator . $key; if (file_exists($path)) { // 清楚文件缓存 clearstatcache(); $mtime = filemtime($path); if (isset($this->mtimes[$key]) == false) { $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]) == true) { return $this->values[$key]; } return null; } protected function set($key, $val) { $this->values[$key] = $val; $path = $this->freezedir . directory_separator . $key; if (file_exists($path)) { touch($path); } file_put_contents($path, serialize($val)); $this->mtimes[$key]=time(); } public function getdsn() { if (isset($this->values['dsn'])) { return $this->values['dsn']; } return self::getinstance()->get('dsn'); } public function setdsn($dsn) { return self::getinstance()->set('dsn', $dsn); } /** * * @param \demo\controller\controllermap $map */ public function setcontrollermap(\demo\controller\controllermap $map) { self::getinstance()->set('cmap', $map); } public function getcontrollermap() { if (isset($this->values['cmap'])) { return $this->values['cmap']; } return self::getinstance()->get('cmap'); } /** * 获取appcontroller */ public function getappcontroller() { $obj = self::instance(); if (!isset($obj->appcontroller)) { $cmap = $obj->getcontrollermap(); $obj->appcontroller = new \demo\controller\appcontroller($cmap); } return $obj->appcontroller; } // 其它一些列getter和setter // ......}
这次需要实现更加复杂的调用,比如forward,那么就需要简单地修改request类的代码了,使它能够符合调用逻辑的需要。
namespace demo\controller;/** * 封装用户请求 * request */class request { private $properties; private $feedback = array(); // 保存业务对象,可以供给视图使用 private $objects = array(); // 保存上一个已执行的command对象 private $lastcommand; public function __construct() { $this->init(); $this->filterproperties(); \demo\base\requestregistry::getinstance()->setrequest($this); } public function __clone() { $this->properties = array(); } public function init() { if (isset($_server['request_method'])) { if ($_server['request_method']) { $this->properties = $_request; return ; } } // 命令行下的方式 foreach ($_server['argv'] as $arg) { if (strpos($arg, '=')) { list($key, $val) = explode('=', $arg); $this->setproperties($key, $val); } } } public function filterproperties() { // 过滤用户请求... } public function getproperty($key) { return $this->properties[$key]; } public function setproperties($key, $val) { $this->properties[$key] = $val; } public function getfeedback() { return $feedback; } public function addfeedback($msg) { array_push($this->feedback, $msg); } public function getfeedbackstring($separator = '\n') { return implode('\n', $this->feedback); } /** * * @param \demo\command\command $cmd */ public function setcommand(\demo\command\command $cmd) { $this->lastcommand = $cmd; } public function getlastcommand() { return $this->lastcommand; } /** * * @param unknown_type $name * @param unknown_type $object */ public function setobject($name, $object) { $this->objects[$name] = $object; } public function getobject($name) { if (isset($this->objects[$name])) { return $this->objects[$name]; } return null; }}
现在已经添加了能够保存映射关系的类controllermap,修改了request、command、applicationhelper、applicationregistry,主要是添加,少量修改之前的代码。
上面这些类已经能够完成系统的初始化了,包括读取xml配置(applicationhelper)、全局变量访问(registry);
之后就是核心部分了:controller和appcontroller两个类了。