php设计模式第十二章 企业模式中未编写的函数进行编写和测试,对解析xml命令的foreach进行扩展解析,视图的输出一一对应和完善

本文档展示了PHP框架的核心组件,包括请求注册类、会话注册类和应用注册类,用于处理URL参数、启动会话及存储配置。同时,解释了控制类如何获取命令、解析XML配置文件并控制视图输出。此外,还介绍了命令解析器的实现,用于根据请求获取相应命令。整个流程涉及到的类和方法协同工作,实现了框架的基础功能。
摘要由CSDN通过智能技术生成

这是wooregistry.php文件,注册类,最主要用的就是注册类获取woo_controller_Request类获取url端的cmd=login|addvenue等值,还有应用注册类获取xml文件配置,如:dsn|db数据库连接|command命令等

<?php
abstract class woo_base_Registry{
	abstract protected function set($key,$val);
	abstract protected function get($key);
}
//注册类
class woo_base_RequestRegistry extends woo_base_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 set($key,$val){
		$this->values[$key]=$val;
	}
	protected function get($key){
		if(isset($this->values[$key])){
			return $this->values[$key];
		}
		return null;
	}
	static function getRequest(){
		return self::instance()->get('request');
	}
	static function setRequest(woo_controller_Request $request){
		return self::instance()->set('request',$request);
	}
}
//session注册类
class woo_base_SessionRegistry extends woo_base_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 set($key,$val){
		$_SESSION[__CLASS__][$key]=$val;
	}
	protected function get($key){
		if(isset($_SESSION[__CLASS__][$key])){
			return $_SESSION[__CLASS__][$key];
		}
		return null;
	}
	function setComplex(Complex $complex){
		self::instance()->set('complex',$complex);
	}
	function getComplex(){
		return self::instance()->get('complex');
	}
}
//应用程序注册类
class woo_base_ApplicationRegistry extends woo_base_Registry{
	private static $instance;
	private $freezedir="data";
	private $values=array();
	private $mtimes=array();
	private function __construct(){}
	static function instance(){
		if(!isset(self::$instance)){
			self::$instance=new self();
		}
		return self::$instance;
	}
	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();
	}
	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;
	}
	static function getDsn(){
		return self::instance()->get('dsn');
	}
	static function setDsn($dsn){
		return self::instance()->set('dsn',$dsn);
	}
	static function setControllerMap(woo_controller_ControllerMap $map){
		return self::instance()->set('map',$map);
	}
	static function getControllerMap(){
		return self::instance()->get('map');
	}
}

/**
$req=new woo_controller_Request();
echo woo_base_ApplicationRegistry::getDsn();
woo_base_RequestRegistry::setRequest($req);
print_r(woo_base_RequestRegistry::getRequest());
**/
?>

第二个文件 woocontroller.php控制类
这个类主要作用是获取命令、解析命令,解析文件助手类,控制视图的输出
对第十二章企业模式中很多未写的函数进行编写和测试,对视图输出进行编写
特别是在解析xml文件的foreach进行编写,书未编写其它解析,对resolveCommand
函数解析文件命令进行了完善。

require("wooregistry.php");
class woo_base_AppException extends Exception{}
class woo_controller_Controller{
	private $applicationHelper;
	private function __construct(){}
	static function run(){
		$instance=new woo_controller_Controller();
		$instance->init();
		$instance->handleRequest();
	}
	function init(){
		$applicationHelper=woo_controller_ApplicationHelper::instance();
		$applicationHelper->init();
	}
	function handleRequest(){
		$request=new woo_controller_Request();
		//$cmd_r=new woo_command_CommandResolver();
		$map=new woo_controller_ControllerMap();
		$cmd_r=new woo_controller_AppController($map);
		$cmd=$cmd_r->getCommand($request);
		$cmd->execute($request);
		//设置当前命令为已执行过的命令
		$request->setLastCommand($cmd);
		
	}
}
//读取文件配置助手
class woo_controller_ApplicationHelper{
	private static $instance;
	private $config="data/woooptions.xml";
	private function __construct(){}
	static function instance(){
		if(!self::$instance){
			self::$instance=new self();
		}
		return self::$instance;
	}
	function init(){
		$dsn=woo_base_ApplicationRegistry::getDsn();
		return $this->getOptions();
	}
	private function getOptions(){
		$this->ensure(file_exists($this->config),"没有找到相关的xml文件");
		$file=file_get_contents($this->config);
		$options=simplexml_load_string($file);
		$this->ensure($options instanceof SimpleXMLElement,"此文件不是xml文件");
		$dsn=(string)$options->dsn;
		$this->ensure($dsn,"没有找到相关的DSN");
		woo_base_ApplicationRegistry::setDsn($dsn);
		$map=new woo_controller_ControllerMap();
		foreach($options->view as $default_view){
			$stat_str=trim($default_view['status']);
			$status=woo_command_Command::statuses($stat_str);
			$map->addView('default',$status,(string)$default_view);
		}
		foreach($options->command[0] as $view){
			$map->addClassroot('listvenues',(string)$view);
		}
		foreach($options->command[1] as $view){
			$map->addClassroot('addvenue',(string)$view);
		}
		woo_base_ApplicationRegistry::setControllerMap($map);
		return $map;
	}
	private function ensure($ex,$msg){
		if(!$ex){
			throw new woo_base_AppException($msg);
		}
	}
}

//命令解析器
class woo_command_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 woo_command_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);
		$classname="woo_command_$cmd";
		$filepath="woo{$sep}command{$sep}{$cmd}.php";
		if(file_exists($filepath)){
			@require($filepath);
			if(class_exists($classname)){
				$cmd_class=new ReflectionClass($classname);
				if($cmd_class->isSubClassOf(self::$base_cmd)){
					return $cmd_class->newInstance();
				}else{
					$request->setFeedback("'$cmd' 不是命令");
				}
			}
		}
		$request->setFeedback("'$cmd' 没有找到");
		return clone self::$default_cmd;
	}
}
//获取命令
class woo_controller_Request{
	private $properties;
	private $feedback=array();
	private $command;
	private $obj=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 setProperty($key,$val){
		$this->properties[$key]=$val;
	}
	function getProperty($key){
		if(isset($this->properties[$key])){
			return $this->properties[$key];
		}
	}
	function setFeedback($msg){
		array_push($this->feedback,$msg);
	}
	function getFeedback(){
		return $this->feedback;
	}
	function getFeedbackString($separator="\n"){
		return implode($separator,$this->feedback);
	}
	function setLastCommand($cmd){
		$this->command=$cmd;
	}
	function getLastCommand(){
		return $this->command;
	}
	function setObject($name,$name_obj){
		$this->obj[$name]=$name_obj;
	}
}
//存储配置数据
class woo_controller_ControllerMap{
	private $viewMap=array();
	private $forwardMap=array();
	public $classrootMap=array();
	function addClassroot($command,$classroot){
		$this->classrootMap[$command]=$classroot;
	}
	function getClassroot($command){
		if(isset($this->classrootMap[$command])){
			return $this->classrootMap[$command];
		}
	}
	function addView($command='default',$status=0,$view){
		$this->viewMap[$command][$status]=$view;
	}
	function getView($command,$status){
		if(isset($this->viewMap[$command][$status])){
			return $this->viewMap[$command][$status];
		}
		return null;
	}
	function addForward($command,$status=0,$newCommand){
		$this->forwardMap[$command][$status]=$newCommand;
	}
	function getForward($command,$status){
		if(isset($this->forwardMap[$command][$status])){
			return $this->forwardMap[$command][$status];
		}
		return null;
	}
}
//应用控制器
class woo_controller_AppController{
	private static $base_cmd;
	private static $default_cmd;
	private $controllerMap;
	private $invoked=array();
	function __construct(woo_controller_ControllerMap $map){
		$this->controllerMap=$map;
		if(!self::$base_cmd){
			self::$base_cmd=new ReflectionClass("woo_command_Command");
			self::$default_cmd=new woo_command_DefaultCommand();
		}
	}
	function getView(woo_controller_Request $req){
		$view=$this->getResource($req,"View");
		return $view;
	}
	function getForward(woo_controller_Request $req){
		$forward=$this->getResource($req,"Forward");
		if($forward){
			$req->setProperty('cmd',$forward);
		}
		return $forward;
	}
	private function getResource(woo_controller_Request $req,$res){
		//得到前一个命令及其执行状态
		$cmd_str=$req->getProperty('cmd');
		$previous=$req->getLastCommand();
		$status=$previous->getStatus();
		if(!$status){$status=0;}
		$acquire="get$res";
		$resource=$this->controllerMap->$acquire($cmd_str,$status);
		//查找命令并且状态为0的资源
		if(!$resource){
			$resource=$this->controllerMap->$acquire($cmd_str,0);
		}
		//或者'default'命令和命令状态
		if(!$resource){
			$resource=$this->controllerMap->$acquire('default',$status);
		}
		//其他情况获取'default'失败,状态为0
		if(!$resource){
			$resource=$this->controllerMap->$acquire('default',0);
		}
		return $resource;
	}
	function getCommand(woo_controller_Request $req){
		$previous=$req->getLastCommand();
		if(!$previous){
			//这是本次请求调用的第一个命令
			$cmd=$req->getProperty('cmd');
			if(!$cmd){
				//如果无法得到命令,则使用默认命令
				$req->setProperty('cmd',"default");
				return self::$default_cmd;
			}
		}else{
			//之前已经执行过一个命令
			$cmd=$this->getFeedback($req);
			if(!$cmd){return null;}
		}
		
		$cmd_obj=$this->resolveCommand($cmd);
		if(!$cmd_obj){
			throw new woo_base_AppException("couldn't resolve '$cmd'");
		}
		if(@$cmd_class=get_Class($cmd_obj)){
			@$this->invoked[$cmd_class]++;
			if($this->invoked[$cmd_class]>1){
				throw new woo_base_AppException("circular forwarding");
			}
		}else{
			echo "此命令页面为空";
		}
		//返回command对象
		return $cmd_obj;
		
	}
	function resolveCommand($cmd){
			$cmd=str_replace(array('.','/'),"",$cmd);
			//下面用woo_controller_ApplicationHelper::instance()->init()->getClassroot获取数据,因为在handleRequest()又建了个空woo_controller_ControllerMap对象,
			//如果直接用$this->controllerMap->getClassroot()得到的是hnadleRequest新建的对象woo_controller_ControllerMap里面没加数据,所以得到的是空数组。
			$classroot=woo_controller_ApplicationHelper::instance()->init()->getClassroot($cmd);
			
			if(!is_null($classroot)){
				$filepath="woo/command/$classroot.php";
				$classname="woo_command_$classroot";
				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();
						}
					}
				}
			}
			
			if(!is_null($cmd)){
				$classname="woo_command_$cmd";
				$filepath="woo/command/$cmd.php";
				if(file_exists($filepath)){
					require($filepath);
					if(class_exists($classname)){
						$cmd_class=new ReflectionClass($classname);
						if($cmd_class->isSubClassOf(self::$base_cmd)){
							return $cmd_class->newInstance();
						}else{
							$request->setFeedback("'$cmd' 不是命令");
						}
					}
				}
			}
			
			return $classroot;
	}
}
abstract class woo_command_Command{
	private static $STATUS_STRINGS=array(
		'CMD_DEFAULT'=>0,
		'CMD_OK'=>1,
		'CMD_ERROR'=>2,
		'CMD_INSUFFICIENT_DATA'=>3
	);
	private $status=0;
	final function __construct(){}
	function execute(woo_controller_Request $request){
		$this->status=$this->doExecute($request);
		//$request->setCommand($this);
	}
	function getStatus(){
		return $this->status;
	}
	static function statuses($str='CMD_DEFAULT'){
		if(empty($str)){
			$str='CMD_DEFAULT';
		}
		return self::$STATUS_STRINGS[$str];
	}
	abstract function doExecute(woo_controller_Request $request);
}
class woo_command_DefaultCommand extends woo_command_Command{
	function doExecute(woo_controller_Request $request){
		$request->setFeedback('welcome to woo');
		include("woo/view/main.php");
		return self::statuses('CMD_OK');
	}
}

woo_controller_Controller::run();
//woo_controller_ApplicationHelper::instance()->init();
//print_r(woo_base_ApplicationRegistry::getControllerMap());

//$map=new woo_controller_ControllerMap();
//$appc=new woo_controller_AppController($map);
//$request=new woo_controller_Request();
//$cmd=$appc->getCommand($request);
//$request->setLastCommand($cmd);


//$cmd->execute($request);


?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值