ezmvc框架 软件架构 自己写mvc

    Index.php 小孟在线

<?php
	###########################################################################################
	#  EzMvc http://hi.baidu.com/mak0000
	#  小孟在线
 Mengdejun
	#  20100920
	###########################################################################################
	#引入加载器
	include_once 'Ez/Loader.php';
	#启动控制器派发工作
	#$application->run();
	$a=$application->run();
?>

    Ez/Loader.php

<?php
	include_once 'Loader/Cfl.php';
	include_once 'Loader/Funs.php';
	spl_autoload_extensions(FILE_EXTENSIONS);
	class Loader
	{
		public static $register_functions=array();
		public static function init()
		{
		}
		public static function autoload($array)
		{
			foreach($array as $key=>$value):
				if(is_array($value)):
					self::autoload($value);
				else:
					include_onceEx(ereg_replace("{l}",$key,LOADER_RULE));
					autoload_registerEx($value);
				endif;
			endforeach;
		}
		public static function register($function,$file=null)
		{
			if(!empty($file)&&file_exists($file)):include_onceEx($file);endif;
			if(function_exists($function)):autoload_registerEx($function);endif;
		}
		public static function destory()
		{
			self::$register_functions=spl_autoload_functions();
			foreach(self::$register_functions as $key=>$value):
				spl_autoload_unregister($value);
			endforeach;
		}
		public static function unRegister($fun)
		{
			spl_autoload_unregister($fun);
		}
	}
	Loader::init();
	#自动加载类注册器
	Loader::autoload($class_registers);
	#类总控制器全局实例
	global $application;
	$application=EzController::getInstance();
?>

   ez/loader/cfl.php

 

<?php
	######################################################################################
	#注:class_registers中注册的加载器会在启动时自动调用
	######################################################################################
	define("LOADER_DIR",dirname(__FILE__));#加载器目录
	define("ENABLE_SCAN_FILE",true);
	define("LOADER_RULE",LOADER_DIR."/{l}Loader.php");#加载器命名规范 {l}为占位符
	define("FILE_EXTENSIONS",".php,.inc,.php,.ez,.php3,.php4,.phpc");#加载器类文件扩展
	global $class_registers;#定义全局加载器
	$class_registers[]=array
	(
		"Core"=>"CoreRegister",#核心加载器
		"Help"=>"HelpRegister",#helper加载器
		"Actions"=>"WcRegister",#控制器加载器
		"Url"=>"UrlRegister",#URL解析器
		"Plugin"=>"PluginRegister",#插件加载器
		"Vendors"=>"VendorsRegister,SmartyRegister",#默认的模板加载器
		"EzDb"=>"EzDbRegister"
	);
	$class_registers[]=array("Vendors"=>"XingCoreRegister,XingDebugRegister");
?>

 

    ez/loader/funs.php

<?php
	function include_onceEx($str,$throwAble=true)
	{
		$str=string2Array($str);
		for($index=0;$index<count($str);$index++):
			if(!empty($str[$index])&&file_exists($str[$index])):
				include_once(trim($str[$index]));
			endif;
		endfor;
	}
	function string2Array($str,$limiter=',')
	{
		return explode($limiter,$str);
	}
	function autoload_registerEx($str,$throwAble=true)
	{
		$str=string2Array($str);
		for($index=0;$index<count($str);$index++):
			if(!empty($str[$index])&&function_exists($str[$index])):
				spl_autoload_register(trim($str[$index]));
			endif;
		endfor;
	}
?>

 

  ez/loader/ActionsLoader.php

 

<?php
	/**
	 * @desc 控制器加载器
	 */
	include_once 'Funs.php';
	include_once 'Ez/Core/Cfc.php';
	function WcRegister($c)
	{
		include_onceEx(WEB_ACTION_PATH."/{$c}.php");	
	}
?>

 

   ez/loader/CoreLoader.php

 

<?php
	/**
	 * @desc 核心加载器
	 */
	include_once 'Funs.php';
	include_once 'Ez/Core/Cfc.php';
	function CoreRegister($c)
	{
		include_onceEx(EZ_CORE_PATH."/{$c}.php");	
	}
?>

 

    Ez/Core/Controller/IController.php

<?php
/**
 * @desc 适配器接口
 * @author mengdejun
 */
interface IController 
{
	/**
	 * @desc 控制器派发 
	 * @param $cls 用户自定义派发器名 当$cls不为空时 用户指定的派发器优先于配置文件控制器
	 */
	function dispatch($cls=null);
	/**
	 * @desc 类的初始化
	 */
	function init();
}
?>

    Ez/Core/ControllerAdapter.php

<?php
require_once'Controller/IController.php';
include_once'Error/ErrorMessage.php';
/**
 * @desc 前端派发器适配器 扩展类需继承该类或实现IController接口
 * @author mengdejun
 */
abstract class ControllerAdapter implements IController
{
	protected $_action=null;#请求类名
	protected $_method=null;#类方法
	protected $_arrays=null;#请求参数
	protected $_action_instance=null;#处理类实列
	protected $_aciton_reflection_class=null;#反射类
	/**
	 * @desc 类的初始化方法,该方法需初始化类变量$_action和$_method 该方法会自动被加载器调用
	 */
	public function init()
	{
		$class=URL_RULE_HANDLER;
		$ins=new $class;
		$_temp=$ins->parse($_SERVER["REQUEST_URI"],null);
		$this->_arrays=$_temp;
		$this->_action=CONTROLLER_UCFIRST?ucfirst($_temp[ARRAY_RETURN_CONTROLLER_KEY]):$_temp[ARRAY_RETURN_CONTROLLER_KEY];
		$this->_method=$_temp[ARRAY_RETURN_METHOD_KEY];
		return array(ARRAY_RETURN_CONTROLLER_KEY=>$this->_action,ARRAY_RETURN_METHOD_KEY=>$this->_method);
	}
	/**
	 * @desc 拓展方法 手动设置控制类
	 * @param unknown_type $action
	 * @param unknown_type $method
	 */
	public function initEx($action=null,$method=null)
	{
		if(empty($action)||empty($method)):
			$this->init();
		else:
			$this->_action=$action;
			$this->_method=$method;
		endif;
	}
	/**
	 * 显示错误信息
	 * @param string 错误信息
	 */
	public function error($s)
	{
		if(1):
			if(@class_exists($this->_action)):
				$_cls=$this->_aciton_reflection_class;
				$_ins=$this->_action_instance;
				if($_cls->isSubclassOf("MultiAction")):
					$_ms=$_cls->getMethod("doError");
					$_ms->invokeArgs($_ins,array("method"=>$this->_method,"sender"=>$this));
				endif;
			else:
				exit(str_replace("<!--error-->",$s,ERROR_MESSAGE));
			endif;
		else:
			exit(str_replace("<!--error-->",$s,ERROR_MESSAGE));
		endif;
	}
		
	/**
	 * @return the $_action
	 */
	public function get_action() {
		return $this->_action;
	}

	/**
	 * @return the $_method
	 */
	public function get_method()
	{
		return $this->_method;
	}
	/**
	 * @param $_action the $_action to set
	 */
	public function set_action($_action)
	{
		$this->_action = $_action;
	}

	/**
	 * @param $_method the $_method to set
	 */
	public function set_method($_method)
	{
		$this->_method = $_method;
	}
	/**
	 * @return the $_arrays
	 */
	public function get_arrays()
	{
		return $this->_arrays;
	}
	/**
	 * @return the $_action_instance
	 */
	public function get_action_instance()
	{
		return $this->_action_instance;
	}
	/**
	 * @param $_action_instance the $_action_instance to set
	 */
	public function set_action_instance($_action_instance)
	{
		$this->_action_instance = $_action_instance;
	}
	/**
	 * @return the $_aciton_reflection_class
	 */
	public function get_aciton_reflection_class()
	{
		return $this->_aciton_reflection_class;
	}

	/**
	 * @param $_aciton_reflection_class the $_aciton_reflection_class to set
	 */
	public function set_aciton_reflection_class($_aciton_reflection_class)
	{
		$this->_aciton_reflection_class = $_aciton_reflection_class;
	}

	
	
}
?>

    ez/core/EzController.php

 

<?php
include_once'Cfc.php';
require_once'ControllerAdapter.php';
/**
 * @desc ez派发控制器
 * @author mengdejun
 *
 */
class EzController extends ControllerAdapter
{
	private static $_instance=null;
	private function __construct(){}
	public static function getInstance()
	{
		if(!(self::$_instance instanceof self)):
			self::$_instance=new self();
		endif;
		return self::$_instance;
	}
	public function dispatch($cls=null)
	{
		#加载派发控制器
		$class=EZ_CONTROLLER_HANDLER;
		if(!class_exists($class)):
			$class=DEFAULT_ACTION_HANDLER;
		endif;
		if(!empty($cls)&&class_exists($cls)):
			$class=$cls;
		endif;
		$ins=new $class;
		#初始化前端派发器,并解析url请求对象
		$ins->init();
		#执行派发器派发工作
		$ins->dispatch();
	}
	public function run($cls=null)
	{
		$this->dispatch($cls);
	}
}
?>

 

    Ez/Core/ActionController.php

<?php
include_once'Cfc.php';
include_once'ControllerAdapter.php';
/**
 * @desc 默认派发器实现
 * @author mengdejun
 */
class ActionController extends ControllerAdapter 
{
	public function dispatch($cls=null)
	{
		if(@class_exists($this->_action)):
			$_target=new ReflectionClass($this->_action);
			$this->set_aciton_reflection_class($_target);
			if(!$_target->isAbstract()&&!$_target->isInterface()):
				$_ins=$_target->newInstance();
				$this->set_action_instance($_ins);
				if($_target->hasMethod($this->_method)):
					if($_target->isSubclassOf("Action")):
						$_fun=$_target->getMethod("addVars");
						$_fun->invokeArgs($_ins,(Array)$this->get_arrays());
					endif;
					$_fun=$_target->getMethod($this->_method);
					$_fun->invokeArgs($_ins,$this->get_arrays());
				else:
					$this->error("can't found any adaptive method about {$this->_method}");
				endif;
			endif;
		else:
			$this->error("can't found any adaptive action about {$this->_action}");
		endif;
	}
}

?>

 

   ez/core/url/iurl.php

<?php
	/**
	 * @desc URL解析器接口
	 * @author mengdejun
	 */
	interface IUrl
	{
		/**
		 * url解析器,返回数组array(ARRAY_RETURN_ACTION_KEY=>"",ARRAY_RETURN_METHOD_KEY=>"")
		 * @param string $queryString
		 */
		public function parse($queryString,$class=null);
	}
?>

   ez/core/url/UrlParamerAdapter.php

<?php
include_once 'IUrl.php';
include_once 'Cfu.php';
class UrlParamerAdapter implements IUrl 
{	
	public function parse($queryString,$class=null)
	{
		$_array=array();
		if(URL_RULE_HANDLER==__CLASS__):
			$_array[ARRAY_RETURN_CONTROLLER_KEY]=!empty($_REQUEST[ACTION_KEY])?str_replace("{c}",$_REQUEST[ACTION_KEY],CONTROLLER_RULE):str_replace("{c}",WEB_DEFAULT_CONTROLLER,CONTROLLER_RULE);
			$_array[ARRAY_RETURN_METHOD_KEY]=!empty($_REQUEST[METHOD_KEY])?str_replace("{m}",$_REQUEST[METHOD_KEY],METHOD_RULE):str_replace("{m}",WEB_DEFAULT_METHOD,METHOD_RULE);
			$_array+=$_GET;
		else:
			exit("can't found any rule adaptive ".URL_RULE_HANDLER);
		endif;
		return $_array;
	}
}

?>

 

   ez/core/basic.php

 

<?php
	function defineEx($key,$val){if(!defined($key)):define($key,$val);endif;}#常量定义
	function alias($name1,$name2){defineEx($name2,$name1);}#别名
	function exist($var){return !empty($var)&&isset($var);}#检查变量是否存在
	defineEx("ARRAY_RETURN_ACTION_KEY","Action");#默认控制器返回键
	defineEx("ARRAY_RETURN_METHOD_KEY","Method");#默认方法返回键
	alias(ARRAY_RETURN_ACTION_KEY,"ARRAY_RETURN_CONTROLLER_KEY");
	alias(ARRAY_RETURN_METHOD_KEY,"ARRAY_RETURN_METHOD_KEY");
	alias(null,"NULL");
	alias(true,"TRUE");
	alias(false,"FALSE");
?>

 

   ez/core/cfc.php

<?php
	include_once'Basic.php';
	include_once'Url/Cfu.php';
	defineEx("EZ_VERSION","1.2.0");
	defineEx("METHOD_RULE","{m}");#方法命名规则
	defineEx("CONTROLLER_RULE","{c}Action");#控制器命名规则
	defineEx("WEB_PATH","App");#web目录
	defineEx("EZ_PATH","Ez");#web目录
	defineEx("EZ_CORE_PATH",EZ_PATH.DIRECTORY_SEPARATOR."Core");#ez目录
	defineEx("EZ_HELPER_PATH",EZ_PATH.DIRECTORY_SEPARATOR."Helper");#ez目录
	defineEx("EZ_LOADER_PATH",EZ_PATH.DIRECTORY_SEPARATOR."Loader");#ez目录
	defineEx("EZ_PLUGIN_PATH",EZ_PATH.DIRECTORY_SEPARATOR."Plugin");#ez目录
	defineEx("EZ_VENDOR_PATH",EZ_PATH.DIRECTORY_SEPARATOR."Vendor");#ez目录
	defineEx("EZ_URL_PATH",EZ_CORE_PATH.DIRECTORY_SEPARATOR."Url");#ez目录
	defineEx("WEB_ACTION_PATH",WEB_PATH.DIRECTORY_SEPARATOR."Controller");#控制器目录
	defineEx("WEB_VIEW_PATH",WEB_PATH.DIRECTORY_SEPARATOR."View");#视图
	defineEx("DEFAULT_ACTION_HANDLER","ActionController");#默认派发器
	defineEx("MY_ACTION_HANDLER","MyFilterActionController");#默认派发器
	defineEx("EZ_CONTROLLER_HANDLER",MY_ACTION_HANDLER);#默认派发器
?>

    ezmvc框架 软件架构 自己写mvc_第1张图片

鉴于页面有限,仅奉献部门代码,更多细节见 附件 下载

你可能感兴趣的:(c,PHP,Web,mvc,框架)