标签:
App.class.php
1 <?php 2 namespace System\Core; 3 use System\Driver; 4 5 class App{ 6 public $DB; 7 public $Cache; 8 public $Session; 9 public $Controller; 10 11 public function __construct(){ 12 DBUG ? set_error_handler(array($this,‘errorHandler‘)) : NULL;//DBUG模式,设置错误处理 13 set_exception_handler(array($this,‘exceptionHandler‘));//注册异常处理函数 14 15 $appConfig = null; 16 17 /* 18 $appConfig = apc_item(‘APP_CONFIG_‘.APP); 19 if(empty($appConfig)){ 20 include APPDIR . ‘/Config/App.php‘; 21 apc_item(‘APP_CONFIG_‘.APP,$appConfig); 22 } 23 */ 24 include APPDIR . ‘/Config/‘ . (APP == APPNAME ? ‘App‘:APP) . ‘.php‘;//加载配置文件 25 26 $isAjaxRequest = ( strtoupper(getsrv(‘HTTP_X_REQUESTED_WITH‘)) == ‘XMLHTTPREQUEST‘);//判断是否ajax请求 27 28 global_item($appConfig,true);//把$appConfig加载到函数的静态变量 29 global_item(‘timestamp‘,getsrv(‘REQUEST_TIME‘)); 30 global_item(‘requestApp‘,APP); 31 global_item(‘isAjaxRequest‘,$isAjaxRequest); 32 33 $this->DB = new Driver\MySQLi($appConfig[‘db‘]); 34 35 $this->Cache = new Driver\Memcached($appConfig[‘app_cache_persistent_id‘],$appConfig[‘app_cache_servers‘]); 36 37 $this->loadSiteConfig(); 38 39 $this->Session = new Session($appConfig[‘app_session_provider‘]); 40 41 } 42 43 public function run(){ 44 $this->Session->__autoSession();//session启动 45 $controller = $method = $params = null; 46 47 $path = parse_url($this->_getRequestUrl(),PHP_URL_PATH);//解析请求的URL,获取path部分 48 extract($this->_route($path));//提取数组字段 49 50 header(‘Content-Type: text/html; charset=‘ . config_item(‘charset‘)); 51 52 $controllerName = $controller; 53 $methodName = $method.‘Method‘; 54 55 if(preg_match("/\W/",$method)){ 56 throw new \Exception($methodName,‘ does not allowed.‘, 404); 57 } 58 59 if($controllerName == ‘do‘){ 60 return $this->_invokeBundleMethod($method,$params); 61 } 62 if(preg_match(‘/\W/‘,$controllerName)){ 63 throw \Exception($controllerName . ‘ does not allowed.‘,404); 64 } 65 $controllerClass = APPNS . ‘\\Controller\\‘ . ucfirst($controllerName); 66 67 $this->Controller = new $controllerClass;//实例化控制器类 68 $this->Controller->setControllerParams($controllerName,$method);//将原始的controllername和method传给控制器类 69 70 if(!method_exists($this->Controller,$methodName)){ 71 throw new \Exception($controllerClass . ‘->‘ , $methodName . ‘ does not exists.‘, 404); 72 }//方法不存在,抛出异常 73 if(empty($params)){ 74 $this->Controller->$methodName();//无参数时直接运行函数 75 }else{ 76 call_user_func_array(array($this->Controller,$methodName), $params);//用call_user_func_array运行带参数的请求 77 } 78 } 79 80 public function loadSiteConfig($isForceReload = false){ 81 $configFile = APPDIR . ‘/Config/Site.php‘; 82 83 if(file_exists($configFile)){ 84 include $configFile; 85 }else{ 86 $tableName = global_item(‘app_config_table_name‘); 87 88 $params = array(‘indexField‘=>‘name‘,‘valueField‘=>‘value‘,‘pageSize‘=>1000); 89 $siteConfig = $this->DB->getColumn($tableName,‘‘,$params); 90 91 $strData = ‘<?php‘."\r\n" . ‘$siteConfig = ‘ . var_export($siteConfig,true); 92 file_put_contents($configFile,$strData); 93 } 94 config_item($siteConfig); 95 96 unset($siteConfig); 97 98 return $configFile; 99 } 100 101 102 public function messageHandler($message,$url=‘‘,$second = 2,$template=‘show_message‘,$code=200){ 103 $handler = $this->Controller; 104 if(!$handler){ 105 $handler = new Controller(); 106 } 107 108 $handler->showMessage($message,$url,$second,$template,$code); 109 } 110 111 public function exceptionHandler($exception){ 112 $this->messageHandler($exception->getMessage(),‘‘,-1,‘show_message‘,$exception->getCode()); 113 } 114 115 public function errorHandler($errno,$errstr,$errfile,$errline){ 116 if(!(error_reporting() & $errno)){ 117 return ; 118 } 119 120 switch($errno){ 121 case E_USER_ERROR: 122 exit(1); 123 case E_USER_WARNING: 124 break; 125 case E_USER_NOTICE: 126 break; 127 default: 128 break; 129 } 130 return true; 131 } 132 133 public function shutdownHandler(){ 134 135 } 136 137 public function _getRequestUrl(){ 138 $requestUrl = ‘‘; 139 if(defined(‘STDIN‘)){ 140 $args = array_slice(getsrv(‘argv‘),1); 141 if($args){ 142 foreach($args as $arg) { 143 $requestUrl .= urlencode($arg) . ‘/‘; 144 } 145 $requestUrl = trim($requestUrl,‘/‘); 146 } 147 } else { 148 $requestUrl = getsrv(‘REQUEST_URI‘); 149 } 150 return $requestUrl; 151 } 152 153 154 private function _route($path){ 155 $path = trim(preg_replace("/+",‘/‘,$path),‘/‘); 156 157 if(empty($path) || file_exists(RUNPATH . ‘/‘ . $path) || strstr($path,‘index.php‘)){ 158 $x = array( isset($_GET[‘c‘]) ? trim(strip_tags($_GET[‘c‘])) : ‘main‘); 159 }elseif($path == ‘do‘ || $path == ‘load‘){ 160 $x = array( isset($_GET[‘c‘]) ? trim(strip_tags($_GET[‘c‘]) : ‘do‘)); 161 }else{ 162 $routes = null; 163 164 include APPDIR . ‘/Config/Routes.php‘; 165 166 foreach($routes as $key => $val){ 167 if( strpos($key,‘(‘ === false ) { 168 if($key == $path){ 169 $path = $val; 170 break; 171 } 172 } else { 173 $key = str_replac(array(‘:num‘,‘:any‘),array(‘\d+‘,‘.+‘),$key); 174 175 if ( preg_match(‘#^‘ . $key . ‘$#‘ , $path) ) { 176 $path = preg_replace(‘#^‘.$key.‘$#‘,$val,$path); 177 break; 178 } 179 180 } 181 } 182 183 if(strpos($path,‘?‘) !== false) { 184 $urls = parse_url($path); 185 $path = $urls[‘path‘]; 186 if( !empty($urls[‘query‘])){ 187 $queries = array(); 188 parse_str($urls[‘query‘],$queries); 189 $_GET += $queries; 190 } 191 } 192 193 $x = explode(‘/‘, $path); 194 } 195 $_x = array(); 196 $_x[‘controller‘] = empty($x[0]) ? ‘main‘ : trim($x[0]); 197 $_x[‘method‘] = empty($x[1]) ? ( isset($_GET[‘m‘]) ? trim (strip_tags($_GET[‘m‘])) : ‘index‘) : trim($x[1]); 198 $_x[‘params‘] = count($x) > 2 ? array_slice($x , 2) : ( isset($_GET[‘v‘]) ? array($_GET[‘v‘] : null ); 199 return $_x; 200 } 201 202 private function _invokeBundleMethod($method,$params=NULL){ 203 $fliter_arr = array(‘status‘,‘ping‘,‘userAgent‘,‘cacheStatus‘,‘opCacheStatus‘,‘tplStatus‘,‘clearCache‘,‘clearTplCache‘,‘reloadSiteConfig‘,‘serverStatus‘,‘version‘); 204 205 if(in_array($method,$fliter_arr){ 206 $className = ‘SystemBundle‘; 207 $methodName = $method; 208 }else{ 209 $className = ucfirse($method); 210 $methodName = ‘load‘; 211 } 212 213 if( $className == ‘SystemBundle‘) { 214 $this->Session->checkWWWAuthenticate(‘Digest‘); 215 } 216 217 $handlerClass = ‘System\\Bundle\\‘ . $className; 218 $handler = new $handlerClass; 219 220 if(!method_exists($handler, $methodName)){ 221 return; 222 } 223 224 if(empty($params){ 225 $handler->$methodName(); 226 }else{ 227 call_user_method_array(array($handler,$methodName),$params); 228 } 229 230 } 231 232 public function __destruct(){ 233 if(isset($this->DB) && $this->DB->isUsing){ 234 $this->DB->close(); 235 } 236 237 } 238 }
标签:
原文地址:http://www.cnblogs.com/wuhen781/p/4335974.html