PageRenderTime 78ms CodeModel.GetById 44ms RepoModel.GetById 0ms app.codeStats 0ms

/xkshop/ThinkPHP/Mode/Lite/App.class.php

https://github.com/cdjflxk/ecshop_flxk
PHP | 225 lines | 111 code | 9 blank | 105 comment | 18 complexity | 108e6aa59cfdf6cfb0ceb518e59e65bd MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2010 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * ThinkPHP 精简模式应用程序类
  15. +------------------------------------------------------------------------------
  16. */
  17. class App
  18. {//类定义开始
  19. /**
  20. +----------------------------------------------------------
  21. * 应用程序初始化
  22. +----------------------------------------------------------
  23. * @access public
  24. +----------------------------------------------------------
  25. * @return void
  26. +----------------------------------------------------------
  27. */
  28. static public function run()
  29. {
  30. // 设定错误和异常处理
  31. set_error_handler(array('App',"appError"));
  32. set_exception_handler(array('App',"appException"));
  33. //[RUNTIME]
  34. // 检查项目是否编译过
  35. // 在部署模式下会自动在第一次执行的时候编译项目
  36. if(defined('RUNTIME_MODEL')){
  37. // 运行模式无需载入项目编译缓存
  38. }elseif(is_file(RUNTIME_PATH.'~app.php') && (!is_file(CONFIG_PATH.'config.php') || filemtime(RUNTIME_PATH.'~app.php')>filemtime(CONFIG_PATH.'config.php'))) {
  39. // 直接读取编译后的项目文件
  40. C(include RUNTIME_PATH.'~app.php');
  41. }else{
  42. // 预编译项目
  43. App::build();
  44. }
  45. //[/RUNTIME]
  46. // 应用调度过滤器
  47. // 如果没有加载任何URL调度器
  48. // 默认只支持 QUERY_STRING 方式
  49. Dispatcher::dispatch();
  50. // 取得模块和操作名称
  51. // 可以在Dispatcher中定义获取规则
  52. if(!defined('MODULE_NAME')) define('MODULE_NAME', App::getModule()); // Module名称
  53. if(!defined('ACTION_NAME')) define('ACTION_NAME', App::getAction()); // Action操作
  54. // 记录应用初始化时间
  55. if(C('SHOW_RUN_TIME')) $GLOBALS['_initTime'] = microtime(TRUE);
  56. // 执行操作
  57. R(MODULE_NAME,ACTION_NAME);
  58. // 保存日志记录
  59. if(C('LOG_RECORD')) Log::save();
  60. return ;
  61. }
  62. //[RUNTIME]
  63. /**
  64. +----------------------------------------------------------
  65. * 读取配置信息 编译项目
  66. +----------------------------------------------------------
  67. * @access private
  68. +----------------------------------------------------------
  69. * @return string
  70. +----------------------------------------------------------
  71. */
  72. static private function build()
  73. {
  74. // 加载惯例配置文件
  75. C(include THINK_PATH.'/Common/convention.php');
  76. // 加载项目配置文件
  77. if(is_file(CONFIG_PATH.'config.php')) {
  78. C(include CONFIG_PATH.'config.php');
  79. }
  80. $common = '';
  81. $debug = C('APP_DEBUG'); // 是否调试模式
  82. // 加载项目公共文件
  83. if(is_file(COMMON_PATH.'common.php')) {
  84. include COMMON_PATH.'common.php';
  85. if(!$debug) { // 编译文件
  86. $common .= compile(COMMON_PATH.'common.php');
  87. }
  88. }
  89. // 加载项目编译文件列表
  90. if(is_file(CONFIG_PATH.'app.php')) {
  91. $list = include CONFIG_PATH.'app.php';
  92. foreach ($list as $key=>$file){
  93. // 加载并编译文件
  94. require $file;
  95. if(!$debug) {
  96. $common .= compile($file);
  97. }
  98. }
  99. }
  100. // 如果是调试模式加载调试模式配置文件
  101. if($debug) {
  102. // 加载系统默认的开发模式配置文件
  103. C(include THINK_PATH.'/Common/debug.php');
  104. if(is_file(CONFIG_PATH.'debug.php')) {
  105. // 允许项目增加开发模式配置定义
  106. C(include CONFIG_PATH.'debug.php');
  107. }
  108. }else{
  109. // 部署模式下面生成编译文件
  110. // 下次直接加载项目编译文件
  111. if(defined('RUNTIME_ALLINONE')) {
  112. // 获取用户自定义变量
  113. $defs = get_defined_constants(TRUE);
  114. $content = array_define($defs['user']);
  115. $content .= substr(file_get_contents(RUNTIME_PATH.'~runtime.php'),5);
  116. $content .= $common."\nreturn ".var_export(C(),true).';';
  117. file_put_contents(RUNTIME_PATH.'~allinone.php',strip_whitespace('<?php '.$content));
  118. }else{
  119. $content = "<?php ".$common."\nreturn ".var_export(C(),true).";\n?>";
  120. file_put_contents(RUNTIME_PATH.'~app.php',strip_whitespace($content));
  121. }
  122. }
  123. return ;
  124. }
  125. //[/RUNTIME]
  126. /**
  127. +----------------------------------------------------------
  128. * 获得实际的模块名称
  129. +----------------------------------------------------------
  130. * @access private
  131. +----------------------------------------------------------
  132. * @return string
  133. +----------------------------------------------------------
  134. */
  135. static private function getModule()
  136. {
  137. $var = C('VAR_MODULE');
  138. $module = !empty($_POST[$var]) ?
  139. $_POST[$var] :
  140. (!empty($_GET[$var])? $_GET[$var]:C('DEFAULT_MODULE'));
  141. if(C('URL_CASE_INSENSITIVE')) {
  142. // URL地址不区分大小写
  143. define('P_MODULE_NAME',strtolower($module));
  144. // 智能识别方式 index.php/user_type/index/ 识别到 UserTypeAction 模块
  145. $module = ucfirst(parse_name(strtolower($module),1));
  146. }
  147. unset($_POST[$var],$_GET[$var]);
  148. return $module;
  149. }
  150. /**
  151. +----------------------------------------------------------
  152. * 获得实际的操作名称
  153. +----------------------------------------------------------
  154. * @access private
  155. +----------------------------------------------------------
  156. * @return string
  157. +----------------------------------------------------------
  158. */
  159. static private function getAction()
  160. {
  161. $var = C('VAR_ACTION');
  162. $action = !empty($_POST[$var]) ?
  163. $_POST[$var] :
  164. (!empty($_GET[$var])?$_GET[$var]:C('DEFAULT_ACTION'));
  165. unset($_POST[$var],$_GET[$var]);
  166. return $action;
  167. }
  168. /**
  169. +----------------------------------------------------------
  170. * 自定义异常处理
  171. +----------------------------------------------------------
  172. * @access public
  173. +----------------------------------------------------------
  174. * @param mixed $e 异常对象
  175. +----------------------------------------------------------
  176. */
  177. static public function appException($e)
  178. {
  179. halt($e->__toString());
  180. }
  181. /**
  182. +----------------------------------------------------------
  183. * 自定义错误处理
  184. +----------------------------------------------------------
  185. * @access public
  186. +----------------------------------------------------------
  187. * @param int $errno 错误类型
  188. * @param string $errstr 错误信息
  189. * @param string $errfile 错误文件
  190. * @param int $errline 错误行数
  191. +----------------------------------------------------------
  192. * @return void
  193. +----------------------------------------------------------
  194. */
  195. static public function appError($errno, $errstr, $errfile, $errline)
  196. {
  197. switch ($errno) {
  198. case E_ERROR:
  199. case E_USER_ERROR:
  200. $errorStr = "[$errno] $errstr ".basename($errfile)." 第 $errline 行.";
  201. if(C('LOG_RECORD')){
  202. Log::write($errorStr,Log::ERR);
  203. }
  204. halt($errorStr);
  205. break;
  206. case E_STRICT:
  207. case E_USER_WARNING:
  208. case E_USER_NOTICE:
  209. default:
  210. $errorStr = "[$errno] $errstr ".basename($errfile)." 第 $errline 行.";
  211. Log::record($errorStr,Log::NOTICE);
  212. break;
  213. }
  214. }
  215. };//类定义结束
  216. ?>