Errors.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. {
  56. $traceString .= str_repeat(QII_SPACE, 4) . $trance . QII_EOL;
  57. }
  58. $message[] = $traceString;
  59. if (\Qii::getInstance()->logerWriter != null) {
  60. $message[] = 'Source URL:' . \Qii::getInstance()->request->url->getCurrentURL();
  61. $message[] = 'Referer URL:' . (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : \Qii::getInstance()->request->url->getCurrentURL());
  62. \Qii::getInstance()->logerWriter->writeLog($message);
  63. }
  64. $appConfigure = \Qii::appConfigure();
  65. $env = \Qii\Config\Register::get(\Qii\Config\Consts::APP_ENVIRON, 'dev');
  66. if ($env == 'product' || ($appConfigure['errorPage'] && (isset($appConfigure['debug']) && $appConfigure['debug'] == 0))) {
  67. list($controller, $action) = explode(':', $appConfigure['errorPage']);
  68. $controllerCls = \Qii\Config\Register::get(\Qii\Config\Consts::APP_DEFAULT_CONTROLLER_PREFIX) . '_' . $controller;
  69. $action = preg_replace('/(Action)$/i', "", $action);
  70. $filePath = \Qii\Autoloader\Import::requireByClass($controllerCls);
  71. if (!is_file($filePath)) {
  72. if ($env == 'product') return '';
  73. \Qii\Autoloader\Import::requires(Qii_DIR . DS . 'Exceptions' . DS . 'Error.php');
  74. call_user_func_array(array('\Qii\Exceptions\Error', 'index'), array($controller, $action));
  75. die();
  76. } else {
  77. \Qii::getInstance()->request->setControllerName($controller);
  78. \Qii::getInstance()->request->setActionName($action);
  79. \Qii::getInstance()->dispatcher->setRequest(\Qii::getInstance()->request);
  80. \Qii::getInstance()->dispatcher->dispatch($controller, $action, $e);
  81. die();
  82. }
  83. }
  84. ob_start();
  85. include(join(DS, array(Qii_DIR, 'Exceptions', 'View', 'error.php')));
  86. $html = ob_get_contents();
  87. ob_clean();
  88. if(!IS_CLI)
  89. {
  90. echo $html;
  91. return;
  92. }
  93. return (new \Qii\Response\Cli())->stdout(
  94. str_replace("&nbsp;"
  95. , " "
  96. , strip_tags(join(PHP_EOL, preg_replace("/[\n|\r\n]/", PHP_EOL, $message)))
  97. )
  98. );
  99. }
  100. /**
  101. * 获取指定文件的指定行内容
  102. *
  103. * @param String $fileName 文件名
  104. * @param Int $line 行号
  105. * @return String
  106. */
  107. public static function getLineMessage($fileName, $line)
  108. {
  109. $seekline = max(0, $line - 1);
  110. $spl = new \SplFileObject($fileName);
  111. $code = array();
  112. if ($line > 1) {
  113. $maxLine = 10;
  114. $firstLine = max(0, $seekline - $maxLine);
  115. $spl->seek($firstLine);
  116. $min = $seekline - $maxLine;
  117. $max = $seekline + $maxLine + 1;
  118. for ($i = $min; $i < $max; $i++) {
  119. $currentLine = $i + ($min < 0 ? abs($min) : 0) + 1;
  120. $color = $currentLine == $line ? ' color="red"' : '';
  121. if ($spl->eof()) break;
  122. if(IS_CLI)
  123. {
  124. $code[] = $currentLine . ($color != '' ? ' 行发生错误' : '') . rtrim($spl->current());
  125. }
  126. else
  127. {
  128. $code[] = '<font ' . $color . '>' . $currentLine . ':</font>' . "\t" . '<font ' . $color . '>' . htmlspecialchars(rtrim($spl->current())) . '</font>';
  129. }
  130. $spl->next();
  131. }
  132. } else {
  133. $spl->seek($seekline);
  134. if(IS_CLI)
  135. {
  136. $code[] = rtrim($spl->current());
  137. }
  138. else
  139. {
  140. $code[] = htmlspecialchars(rtrim($spl->current()));
  141. }
  142. }
  143. return IS_CLI ? PHP_EOL . join(PHP_EOL, $code) : '<pre style="font-weight:bold;">' . join("<br />", $code) . '</pre>';
  144. }
  145. /**
  146. * sprintf 格式化语言错误信息内容
  147. *
  148. *
  149. * Qii::e($message, $argv1, $argv2, ..., $line);
  150. * $message = sprintf($message, $argv1, $argv2, ...);
  151. * throw new \Qii_Exceptions_Errors($message, $line);
  152. */
  153. public static function e()
  154. {
  155. $argvs = func_get_args();
  156. $count = count($argvs);
  157. $message = array_shift($argvs);
  158. $line = (int) array_pop($argvs);
  159. if ($count == 2) {
  160. throw new \Qii\Exceptions\Errors($message, $line);
  161. }
  162. $message = vsprintf($message, $argvs);
  163. throw new \Qii\Exceptions\Errors($message, $line);
  164. }
  165. }