Errors.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. $code = $e->getCode();
  44. if ($code == 0) $code = 1;
  45. echo json_encode(array('code' => $code, 'line' => $e->getFile() . ' line :' . $e->getLine(), 'msg' => strip_tags($e->getMessage())), JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
  46. return;
  47. }
  48. $message[] = (IS_CLI ? QII_EOL : '') . \Qii::i('Error file', self::getRelatePath($_SERVER['SCRIPT_FILENAME'], $e->getFile())) . (IS_CLI ? QII_EOL : '');
  49. $message[] = \Qii::i('Error code', $e->getCode()) . (IS_CLI ? QII_EOL : '');
  50. $message[] = \Qii::i('Error description', $e->getMessage()) . (IS_CLI ? QII_EOL : '');
  51. $message[] = \Qii::i('Error line', $e->getLine() . ' on ' . self::getLineMessage($e->getFile(), $e->getLine())) . (IS_CLI ? QII_EOL : '');
  52. $traceString = \Qii::i('Trace as below') . QII_EOL;
  53. $traces = explode("\n", $e->getTraceAsString());
  54. foreach ($traces AS $trance) {
  55. $traceString .= str_repeat(QII_SPACE, 4) . $trance . QII_EOL;
  56. }
  57. $message[] = $traceString;
  58. if (\Qii::getInstance()->logerWriter != null) {
  59. $message[] = 'Source URL:' . \Qii::getInstance()->request->url->getCurrentURL();
  60. $message[] = 'Referer URL:' . (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : \Qii::getInstance()->request->url->getCurrentURL());
  61. \Qii::getInstance()->logerWriter->writeLog($message);
  62. }
  63. $appConfigure = \Qii::appConfigure();
  64. $env = \Qii\Config\Register::get(\Qii\Config\Consts::APP_ENVIRON, 'dev');
  65. if ($env == 'product' || ($appConfigure['errorPage'] && (isset($appConfigure['debug']) && $appConfigure['debug'] == 0))) {
  66. list($controller, $action) = explode(':', $appConfigure['errorPage']);
  67. $controllerCls = \Qii\Config\Register::get(\Qii\Config\Consts::APP_DEFAULT_CONTROLLER_PREFIX) . '\\' . $controller;
  68. $action = preg_replace('/(Action)$/i', "", $action);
  69. $filePath = \Qii\Autoloader\Psr4::getInstance()->searchMappedFile($controllerCls);
  70. if (!is_file($filePath)) {
  71. if ($env == 'product') return '';
  72. \Qii\Autoloader\Import::requires(Qii_DIR . DS . 'Exceptions' . DS . 'Error.php');
  73. call_user_func_array(array('\Qii\Exceptions\Error', 'index'), array($controller, $action));
  74. die();
  75. } else {
  76. \Qii::getInstance()->request->setControllerName($controller);
  77. \Qii::getInstance()->request->setActionName($action);
  78. \Qii::getInstance()->dispatcher->setRequest(\Qii::getInstance()->request);
  79. \Qii::getInstance()->dispatcher->dispatch($controller, $action, $e);
  80. die();
  81. }
  82. }
  83. ob_start();
  84. include(join(DS, array(Qii_DIR, 'Exceptions', 'View', 'error.php')));
  85. $html = ob_get_contents();
  86. ob_clean();
  87. if (!IS_CLI) {
  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. $code[] = $currentLine . ($color != '' ? ' 行发生错误' : '') . rtrim($spl->current());
  122. } else {
  123. $code[] = '<font ' . $color . '>' . $currentLine . ':</font>' . "\t" . '<font ' . $color . '>' . htmlspecialchars(rtrim($spl->current())) . '</font>';
  124. }
  125. $spl->next();
  126. }
  127. } else {
  128. $spl->seek($seekline);
  129. if (IS_CLI) {
  130. $code[] = rtrim($spl->current());
  131. } else {
  132. $code[] = htmlspecialchars(rtrim($spl->current()));
  133. }
  134. }
  135. return IS_CLI ? PHP_EOL . join(PHP_EOL, $code) : '<pre style="font-weight:bold;">' . join("<br />", $code) . '</pre>';
  136. }
  137. /**
  138. * sprintf 格式化语言错误信息内容
  139. *
  140. *
  141. * Qii::e($message, $argv1, $argv2, ..., $line);
  142. * $message = sprintf($message, $argv1, $argv2, ...);
  143. * throw new \Qii_Exceptions_Errors($message, $line);
  144. */
  145. public static function e()
  146. {
  147. $argvs = func_get_args();
  148. $count = count($argvs);
  149. $message = array_shift($argvs);
  150. $line = (int)array_pop($argvs);
  151. if ($count == 2) {
  152. throw new \Qii\Exceptions\Errors($message, $line);
  153. }
  154. $message = vsprintf($message, $argvs);
  155. throw new \Qii\Exceptions\Errors($message, $line);
  156. }
  157. }