凌的博客

您现在的位置是: 首页 > 学无止境 > PHP > 

PHP

自定义PHP框架

2015-11-21 PHP 765
/** * 项目核心文件 * */ class Kernel{ public $config = array(); public static $_instance = NULL; /** * 单例生成对象 * */ private function __construct($config=array()){ //加载核心函数
/**
 * 项目核心文件
 * */
class Kernel{
	public $config = array();
	public static $_instance = NULL;
	/**
	 * 单例生成对象
	 * */
	private function __construct($config=array()){		
		//加载核心函数
		$this->loadFunc();		
		//加载惯例配置
		$this->config = array_merge_recursive($config, $this->loadConventionConfig()  );		
		//加载配置
		C($this->config);
		//自动加载项目配置文件
		$this->loadAppConfig();
		
		//加载核心类
		$this->loadSysClass();
		//自动加载框架
		$this->autoloadClass();

	}
	
	/**
	 * 创建一个对象
	 * */
	public function create($config=array()){
		if(self::$_instance instanceof self){	
			//自动加载		
		}else{			
			self::$_instance = new self($config);
		}
		return self::$_instance;
	}
	
	public function run(){
		header('Content-Type: text/html; charset=utf-8;');
		G('app_start');
		//路由检查
		\Kernel\Core\Router::check();
		//通过路由 调度控制器
		\Kernel\Core\Disaptcher::disaptch();

		$_controller = ql_CONTROLLER;
		$_action      = ql_ACTION;
		$_controller = '\\'.APP_NAME.'\\Controller\\'.$_controller;
		$Controller = new $_controller();
		if(method_exists($Controller, $_action)){
			$Controller->$_action();
		}else{
			//默认空操作
			$Controller->_empty();
		}
		
		//执行任务
		G('app_end');
		if(DE_BUG == 1){
			echo '
程序运行时间:'.G('app_start','app_end')."
\n";
		}
	}
	
	/**
	 * 加载核心框架类
	 * */
	public function loadSysClass(){
		$_core_class = array(			
				'kernel/core/model.php',        //模型				
				'kernel/core/view.php',           //视图
				'kernel/core/controller.php',   //控制器
				'kernel/core/router.php',         //路由
				'kernel/core/dispatcher.php',   //调度
				'kernel/core/exception.php',    //异常
				'kernel/core/template.php',     //模板
				'kernel/core/db.php',               //数据库
				'kernel/core/cache.php',          //缓存
		);
		foreach($_core_class as $v){			
			try{
				require ROOT_PATH.$v;
			}catch (\Kernel\Core\Exception $e){
				E($e->getMessage());
			}
		}
	}
	/**
	 * 加载核心函数
	 * */
	public function loadFunc(){
		$_core_func = array(
				'kernel/functions/common.php',        //公共函数
				'kernel/functions/extend.php',           //扩展函数
		);
		foreach($_core_func as $v){
			require ROOT_PATH.$v;
		}
	}
	
	/**
	 * 加载惯例配置
	 * */
	public function loadConventionConfig(){
		return array(
			'default_controller'   => 'Index',
			'default_action'         => 'index',
			'db_fields_cache'       => 1,
			'router' => array(
				'controller'   => 'c',
				'action'        => 'a', 
			),
			'theme'            => 'default',
			'template'        => array(
				 'template_l' => '{',
				 'template_r' => '}',
				//'template'    => 
			),
				
		);
	}
	
	/**
	 * 自动加载框架类
	 * */
	public function autoloadClass(){		
		spl_autoload_register(array(self,'simpleLoader'));		
	}	
	
	/**
	 * 自动加载类
	 * //普通模式
	 * 加载app
	 * */
	public function simpleLoader($class){
		$class_file_name = strtolower($class);
		$class_file = ROOT_PATH.$class_file_name.'.php' ;
		//自动 加载 app 扩展类
	    require_once $class_file;
	}
	
	/**
	 * 自动加载项目配置文件
	 * */
	public function loadAppConfig(){
		//加载 公共config
		$common_config_file = ROOT_PATH.'config/config.php';
		if(file_exists($common_config_file)){
			C(require_once($common_config_file));
		}
		//加载APP config		
		$app_config_file = APP_PATH.'config/config.php';
		if(file_exists($app_config_file)){
			C(require_once($app_config_file));
		}
	}
	
	
}

文章评论

0条评论