App.class.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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')) {
  39. // 直接读取编译后的项目文件
  40. C(include RUNTIME_PATH.'~app.php');
  41. }else{
  42. // 预编译项目
  43. App::build();
  44. }
  45. //[/RUNTIME]
  46. // 取得模块和操作名称
  47. define('MODULE_NAME', isset($_SERVER['argv'][1])?$_SERVER['argv'][1]:C('DEFAULT_MODULE'));
  48. define('ACTION_NAME', isset($_SERVER['argv'][2])?$_SERVER['argv'][2]:C('DEFAULT_ACTION'));
  49. // 执行操作
  50. R(MODULE_NAME,ACTION_NAME);
  51. // 保存日志记录
  52. if(C('LOG_RECORD')) Log::save();
  53. return ;
  54. }
  55. //[RUNTIME]
  56. /**
  57. +----------------------------------------------------------
  58. * 读取配置信息 编译项目
  59. +----------------------------------------------------------
  60. * @access private
  61. +----------------------------------------------------------
  62. * @return string
  63. +----------------------------------------------------------
  64. */
  65. static private function build()
  66. {
  67. // 加载惯例配置文件
  68. C(include THINK_PATH.'/Common/convention.php');
  69. // 加载项目配置文件
  70. if(is_file(CONFIG_PATH.'config.php')) {
  71. C(include CONFIG_PATH.'config.php');
  72. }
  73. $common = '';
  74. $debug = C('APP_DEBUG'); // 是否调试模式
  75. // 加载项目公共文件
  76. if(is_file(COMMON_PATH.'common.php')) {
  77. include COMMON_PATH.'common.php';
  78. if(!$debug) { // 编译文件
  79. $common .= compile(COMMON_PATH.'common.php');
  80. }
  81. }
  82. // 加载项目编译文件列表
  83. if(is_file(CONFIG_PATH.'app.php')) {
  84. $list = include CONFIG_PATH.'app.php';
  85. foreach ($list as $key=>$file){
  86. // 加载并编译文件
  87. require $file;
  88. if(!$debug) {
  89. $common .= compile($file);
  90. }
  91. }
  92. }
  93. // 如果是调试模式加载调试模式配置文件
  94. if($debug) {
  95. // 加载系统默认的开发模式配置文件
  96. C(include THINK_PATH.'/Common/debug.php');
  97. if(is_file(CONFIG_PATH.'debug.php')) {
  98. // 允许项目增加开发模式配置定义
  99. C(include CONFIG_PATH.'debug.php');
  100. }
  101. }else{
  102. // 部署模式下面生成编译文件
  103. // 下次直接加载项目编译文件
  104. if(defined('RUNTIME_ALLINONE')) {
  105. // 获取用户自定义变量
  106. $defs = get_defined_constants(TRUE);
  107. $content = array_define($defs['user']);
  108. $content .= substr(file_get_contents(RUNTIME_PATH.'~runtime.php'),5);
  109. $content .= $common."\nreturn ".var_export(C(),true).';';
  110. file_put_contents(RUNTIME_PATH.'~allinone.php',strip_whitespace('<?php '.$content));
  111. }else{
  112. $content = "<?php ".$common."\nreturn ".var_export(C(),true).";\n?>";
  113. file_put_contents(RUNTIME_PATH.'~app.php',strip_whitespace($content));
  114. }
  115. }
  116. return ;
  117. }
  118. //[/RUNTIME]
  119. /**
  120. +----------------------------------------------------------
  121. * 自定义异常处理
  122. +----------------------------------------------------------
  123. * @access public
  124. +----------------------------------------------------------
  125. * @param mixed $e 异常对象
  126. +----------------------------------------------------------
  127. */
  128. static public function appException($e)
  129. {
  130. exit($e->__toString());
  131. }
  132. /**
  133. +----------------------------------------------------------
  134. * 自定义错误处理
  135. +----------------------------------------------------------
  136. * @access public
  137. +----------------------------------------------------------
  138. * @param int $errno 错误类型
  139. * @param string $errstr 错误信息
  140. * @param string $errfile 错误文件
  141. * @param int $errline 错误行数
  142. +----------------------------------------------------------
  143. * @return void
  144. +----------------------------------------------------------
  145. */
  146. static public function appError($errno, $errstr, $errfile, $errline)
  147. {
  148. switch ($errno) {
  149. case E_ERROR:
  150. case E_USER_ERROR:
  151. $errorStr = "[$errno] $errstr ".basename($errfile)." 第 $errline 行.";
  152. if(C('LOG_RECORD')){
  153. Log::write($errorStr,Log::ERR);
  154. }
  155. exit($errorStr);
  156. break;
  157. case E_STRICT:
  158. case E_USER_WARNING:
  159. case E_USER_NOTICE:
  160. default:
  161. $errorStr = "[$errno] $errstr ".basename($errfile)." 第 $errline 行.";
  162. Log::record($errorStr,Log::NOTICE);
  163. break;
  164. }
  165. }
  166. };//类定义结束
  167. ?>