Errors.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace Qii\Exceptions;
  3. class Errors extends \Exception
  4. {
  5. const VERSION = '1.2';
  6. /**
  7. * 获取两个文件的相对路径
  8. * @param String $cur
  9. * @param String $absp
  10. * @return String
  11. */
  12. public static function getRelatePath($cur, $absp)
  13. {
  14. $cur = str_replace('\\', '/', $cur);
  15. $absp = str_replace('\\', '/', $absp);
  16. $sabsp = explode('/', $absp);
  17. $scur = explode('/', $cur);
  18. $la = count($sabsp) - 1;
  19. $lb = count($scur) - 1;
  20. $l = max($la, $lb);
  21. for ($i = 0; $i <= $l; $i++) {
  22. if ($sabsp[$i] != $scur[$i])
  23. break;
  24. }
  25. $k = $i - 1;
  26. $path = "";
  27. for ($i = 1; $i <= ($lb - $k - 1); $i++)
  28. $path .= "../";
  29. for ($i = $k + 1; $i <= ($la - 1); $i++)
  30. $path .= $sabsp[$i] . "/";
  31. $path .= $sabsp[$la];
  32. return $path;
  33. }
  34. /**
  35. * 显示错误
  36. *
  37. * @param Object $e Exception
  38. */
  39. public static function getError($e)
  40. {
  41. $message = array();
  42. if (isset($_GET['isAjax']) && $_GET['isAjax'] == 1) {
  43. echo json_encode(array('code' => $e->getCode(), 'msg' => strip_tags($e->getMessage())), JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
  44. return;
  45. }
  46. $message[] = (IS_CLI ? QII_EOL : '') . \Qii::i('Error file', self::getRelatePath($_SERVER['SCRIPT_FILENAME'], $e->getFile())) . (IS_CLI ? QII_EOL : '');
  47. $message[] = \Qii::i('Error code', $e->getCode()) . (IS_CLI ? QII_EOL : '');
  48. $message[] = \Qii::i('Error description', $e->getMessage()) . (IS_CLI ? QII_EOL : '');
  49. $message[] = \Qii::i('Error line', $e->getLine() . ' on ' . self::getLineMessage($e->getFile(), $e->getLine())) . (IS_CLI ? QII_EOL : '');
  50. $traceString = \Qii::i('Trace as below') . QII_EOL;
  51. $traces = explode("\n", $e->getTraceAsString());
  52. foreach ($traces AS $trance)
  53. {
  54. $traceString .= str_repeat(QII_SPACE, 4) . $trance . QII_EOL;
  55. }
  56. $message[] = $traceString;
  57. if (\Qii::getInstance()->logerWriter != null) {
  58. $message[] = 'Source URL:' . \Qii::getInstance()->request->url->getCurrentURL();
  59. $message[] = 'Referer URL:' . (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : \Qii::getInstance()->request->url->getCurrentURL());
  60. \Qii::getInstance()->logerWriter->writeLog($message);
  61. }
  62. $appConfigure = \Qii::appConfigure();
  63. $env = \Qii\Config\Register::get(\Qii\Config\Consts::APP_ENVIRON, 'dev');
  64. if ($env == 'product' || ($appConfigure['errorPage'] && (isset($appConfigure['debug']) && $appConfigure['debug'] == 0))) {
  65. list($controller, $action) = explode(':', $appConfigure['errorPage']);
  66. $controllerCls = \Qii\Config\Register::get(\Qii\Config\Consts::APP_DEFAULT_CONTROLLER_PREFIX) . '_' . $controller;
  67. $action = preg_replace('/(Action)$/i', "", $action);
  68. $filePath = \Qii\Autoloader\Import::requireByClass($controllerCls);
  69. if (!is_file($filePath)) {
  70. if ($env == 'product') return '';
  71. \Qii\Autoloader\Import::requires(Qii_DIR . DS . 'Exceptions' . DS . 'Error.php');
  72. call_user_func_array(array('\Qii\Exceptions\Error', 'index'), array($controller, $action));
  73. die();
  74. } else {
  75. \Qii::getInstance()->request->setControllerName($controller);
  76. \Qii::getInstance()->request->setActionName($action);
  77. \Qii::getInstance()->dispatcher->setRequest(\Qii::getInstance()->request);
  78. \Qii::getInstance()->dispatcher->dispatch($controller, $action, $e);
  79. die();
  80. }
  81. }
  82. ob_start();
  83. include(join(DS, array(Qii_DIR, 'Exceptions', 'View', 'error.php')));
  84. $html = ob_get_contents();
  85. ob_clean();
  86. if(!IS_CLI)
  87. {
  88. echo $html;
  89. return;
  90. }
  91. return (new \Qii\Response\Cli())->stdout(
  92. str_replace("&nbsp;"
  93. , " "
  94. , strip_tags(join(PHP_EOL, preg_replace("/[\n|\r\n]/", PHP_EOL, $message)))
  95. )
  96. );
  97. }
  98. /**
  99. * 获取指定文件的指定行内容
  100. *
  101. * @param String $fileName 文件名
  102. * @param Int $line 行号
  103. * @return String
  104. */
  105. public static function getLineMessage($fileName, $line)
  106. {
  107. $seekline = max(0, $line - 1);
  108. $spl = new \SplFileObject($fileName);
  109. $code = array();
  110. if ($line > 1) {
  111. $maxLine = 10;
  112. $firstLine = max(0, $seekline - $maxLine);
  113. $spl->seek($firstLine);
  114. $min = $seekline - $maxLine;
  115. $max = $seekline + $maxLine + 1;
  116. for ($i = $min; $i < $max; $i++) {
  117. $currentLine = $i + ($min < 0 ? abs($min) : 0) + 1;
  118. $color = $currentLine == $line ? ' color="red"' : '';
  119. if ($spl->eof()) break;
  120. if(IS_CLI)
  121. {
  122. $code[] = $currentLine . ($color != '' ? ' 行发生错误' : '') . rtrim($spl->current());
  123. }
  124. else
  125. {
  126. $code[] = '<font ' . $color . '>' . $currentLine . ':</font>' . "\t" . '<font ' . $color . '>' . htmlspecialchars(rtrim($spl->current())) . '</font>';
  127. }
  128. $spl->next();
  129. }
  130. } else {
  131. $spl->seek($seekline);
  132. if(IS_CLI)
  133. {
  134. $code[] = rtrim($spl->current());
  135. }
  136. else
  137. {
  138. $code[] = htmlspecialchars(rtrim($spl->current()));
  139. }
  140. }
  141. return IS_CLI ? PHP_EOL . join(PHP_EOL, $code) : '<pre style="font-weight:bold;">' . join("<br />", $code) . '</pre>';
  142. }
  143. /**
  144. * sprintf 格式化语言错误信息内容
  145. *
  146. *
  147. * Qii::e($message, $argv1, $argv2, ..., $line);
  148. * $message = sprintf($message, $argv1, $argv2, ...);
  149. * throw new \Qii_Exceptions_Errors($message, $line);
  150. */
  151. public static function e()
  152. {
  153. $argvs = func_get_args();
  154. $count = count($argvs);
  155. $message = array_shift($argvs);
  156. $line = (int) array_pop($argvs);
  157. if ($count == 2) {
  158. throw new \Qii\Exceptions\Errors($message, $line);
  159. }
  160. $message = vsprintf($message, $argvs);
  161. throw new \Qii\Exceptions\Errors($message, $line);
  162. }
  163. }