2015-05-27
(*暂时未拆分前端控制器和应用控制器,全部集成在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); }}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); } }}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.xmldsn DSN //框架入口index.php
1
CI框架连接数据库配置操作以及多数据库操作
09-05
2
asp 简单读取数据表并列出来 ASP如何快速从数据库读取大量数据
05-17
3
C语言关键字及其解释介绍 C语言32个关键字详解
04-05
4
C语言中sizeof是什么意思 c语言里sizeof怎样用法详解
04-26
5
PHP中的魔术方法 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep,
09-05
6
最简单的asp登陆界面代码 asp登陆界面源代码详细介绍
04-12
7
PHP中的(++i)前缀自增 和 (i++)后缀自增
09-05
8
常用dos命令及语法
09-27
PHP中include和require区别之我见
2014-09-05
将视频设置为Android手机开机动画的教程
2014-12-11
php递归返回值的问题
2014-09-05
如何安装PHPstorm并配置方法教程 phpstorm安装后要进行哪些配置
2017-05-03
PHP 教程之如何使用BLOB存取图片信息实例
2014-09-05
java中的info是什么意思
2022-03-24
单片机编程好学吗?单片机初学者怎样看懂代码
2022-03-21
学ug编程如何快速入门?
2022-03-17
PHP数组函数array
2014-09-05
IcePHP框架中的快速后台中的通用CRUD功能框架
2014-09-05
真实枪械武器模拟器游戏下载v2.177 安卓版
射击枪战 194.6M
下载果汁四溅2中文版下载v1.9.8 安卓版
休闲益智 81.7M
下载我飞刀玩得贼6手游下载v2.2.7 安卓版
休闲益智 73.9M
下载方块冒险下载v1.1.9 安卓版
休闲益智 19.7M
下载我是鸽手游戏下载v2.0.0 安卓版
休闲益智 41.3M
下载耶小兔子2最新中文版下载v1.2.9 安卓版
休闲益智 60.9M
下载弓箭大师你就是王者游戏下载v2.0.1 安卓最新版
其它手游 164.4M
下载弓箭大师你就是王者官方版下载v2.0.1 安卓版
其它手游 164.4M
下载3d狙击王者手机游戏下载v1.5 安卓版
下载
足球冲鸭手机版下载v1.0.16.404.401.0116 安卓版
下载
有种来找我手游下载v1.4.8 安卓版
下载
忍者跳跳跳官方版下载v1.0.2 安卓版
下载
百战斗斗堂拇指游戏下载v5.8 安卓版
下载
梦幻斗斗堂手游下载v5.8 安卓版
下载
百战斗斗堂4399版下载v5.8 安卓版
下载
百战斗斗堂九游手游下载v5.8 安卓版
下载