App.class.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 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. define('MODULE_NAME', App::getModule()); // Module名称
  48. define('ACTION_NAME', App::getAction()); // Action操作
  49. // 记录应用初始化时间
  50. if(C('SHOW_RUN_TIME')) $GLOBALS['_initTime'] = microtime(TRUE);
  51. // 执行操作
  52. R(MODULE_NAME,ACTION_NAME);
  53. // 保存日志记录
  54. if(C('LOG_RECORD')) Log::save();
  55. return ;
  56. }
  57. //[RUNTIME]
  58. /**
  59. +----------------------------------------------------------
  60. * 读取配置信息 编译项目
  61. +----------------------------------------------------------
  62. * @access private
  63. +----------------------------------------------------------
  64. * @return string
  65. +----------------------------------------------------------
  66. */
  67. static private function build()
  68. {
  69. // 加载惯例配置文件
  70. C(include THINK_PATH.'/Common/convention.php');
  71. // 加载项目配置文件
  72. if(is_file(CONFIG_PATH.'config.php')) {
  73. C(include CONFIG_PATH.'config.php');
  74. }
  75. $common = '';
  76. $debug = C('APP_DEBUG'); // 是否调试模式
  77. // 加载项目公共文件
  78. if(is_file(COMMON_PATH.'common.php')) {
  79. include COMMON_PATH.'common.php';
  80. if(!$debug) { // 编译文件
  81. $common .= compile(COMMON_PATH.'common.php');
  82. }
  83. }
  84. // 加载项目编译文件列表
  85. if(is_file(CONFIG_PATH.'app.php')) {
  86. $list = include CONFIG_PATH.'app.php';
  87. foreach ($list as $key=>$file){
  88. // 加载并编译文件
  89. require $file;
  90. if(!$debug) {
  91. $common .= compile($file);
  92. }
  93. }
  94. }
  95. // 如果是调试模式加载调试模式配置文件
  96. if($debug) {
  97. // 加载系统默认的开发模式配置文件
  98. C(include THINK_PATH.'/Common/debug.php');
  99. if(is_file(CONFIG_PATH.'debug.php')) {
  100. // 允许项目增加开发模式配置定义
  101. C(include CONFIG_PATH.'debug.php');
  102. }
  103. }else{
  104. // 部署模式下面生成编译文件
  105. // 下次直接加载项目编译文件
  106. if(defined('RUNTIME_ALLINONE')) {
  107. // 获取用户自定义变量
  108. $defs = get_defined_constants(TRUE);
  109. $content = array_define($defs['user']);
  110. $content .= substr(file_get_contents(RUNTIME_PATH.'~runtime.php'),5);
  111. $content .= $common."\nreturn ".var_export(C(),true).';';
  112. file_put_contents(RUNTIME_PATH.'~allinone.php',strip_whitespace('<?php '.$content));
  113. }else{
  114. $content = "<?php ".$common."\nreturn ".var_export(C(),true).";\n?>";
  115. file_put_contents(RUNTIME_PATH.'~app.php',strip_whitespace($content));
  116. }
  117. }
  118. return ;
  119. }
  120. //[/RUNTIME]
  121. /**
  122. +----------------------------------------------------------
  123. * 获得实际的模块名称
  124. +----------------------------------------------------------
  125. * @access private
  126. +----------------------------------------------------------
  127. * @return string
  128. +----------------------------------------------------------
  129. */
  130. static private function getModule()
  131. {
  132. $var = C('VAR_MODULE');
  133. $module = !empty($_POST[$var]) ?
  134. $_POST[$var] :
  135. (!empty($_GET[$var])? $_GET[$var]:C('DEFAULT_MODULE'));
  136. if(C('URL_CASE_INSENSITIVE')) {
  137. // URL地址不区分大小写
  138. define('P_MODULE_NAME',strtolower($module));
  139. // 智能识别方式 index.php/user_type/index/ 识别到 UserTypeAction 模块
  140. $module = ucfirst(parse_name(strtolower($module),1));
  141. }
  142. unset($_POST[$var],$_GET[$var]);
  143. return $module;
  144. }
  145. /**
  146. +----------------------------------------------------------
  147. * 获得实际的操作名称
  148. +----------------------------------------------------------
  149. * @access private
  150. +----------------------------------------------------------
  151. * @return string
  152. +----------------------------------------------------------
  153. */
  154. static private function getAction()
  155. {
  156. $var = C('VAR_ACTION');
  157. $action = !empty($_POST[$var]) ?
  158. $_POST[$var] :
  159. (!empty($_GET[$var])?$_GET[$var]:C('DEFAULT_ACTION'));
  160. unset($_POST[$var],$_GET[$var]);
  161. return $action;
  162. }
  163. /**
  164. +----------------------------------------------------------
  165. * 自定义异常处理
  166. +----------------------------------------------------------
  167. * @access public
  168. +----------------------------------------------------------
  169. * @param mixed $e 异常对象
  170. +----------------------------------------------------------
  171. */
  172. static public function appException($e)
  173. {
  174. halt($e->__toString());
  175. }
  176. /**
  177. +----------------------------------------------------------
  178. * 自定义错误处理
  179. +----------------------------------------------------------
  180. * @access public
  181. +----------------------------------------------------------
  182. * @param int $errno 错误类型
  183. * @param string $errstr 错误信息
  184. * @param string $errfile 错误文件
  185. * @param int $errline 错误行数
  186. +----------------------------------------------------------
  187. * @return void
  188. +----------------------------------------------------------
  189. */
  190. static public function appError($errno, $errstr, $errfile, $errline)
  191. {
  192. switch ($errno) {
  193. case E_ERROR:
  194. case E_USER_ERROR:
  195. $errorStr = "[$errno] $errstr ".basename($errfile)." 第 $errline 行.";
  196. if(C('LOG_RECORD')){
  197. Log::write($errorStr,Log::ERR);
  198. }
  199. halt($errorStr);
  200. break;
  201. case E_STRICT:
  202. case E_USER_WARNING:
  203. case E_USER_NOTICE:
  204. default:
  205. $errorStr = "[$errno] $errstr ".basename($errfile)." 第 $errline 行.";
  206. Log::record($errorStr,Log::NOTICE);
  207. break;
  208. }
  209. }
  210. };//类定义结束
  211. ?>