Errors.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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[] = Qii::i('Error file', self::getRelatePath($_SERVER['SCRIPT_FILENAME'], $e->getFile()));
  47. $message[] = Qii::i('Error code', $e->getCode());
  48. $message[] = Qii::i('Error description', $e->getMessage());
  49. $message[] = Qii::i('Error line', $e->getLine() . ' on ' . self::getLineMessage($e->getFile(), $e->getLine()));
  50. $traceString = Qii::i('Trace as below') . '<br />';
  51. $traces = explode("\n", $e->getTraceAsString());
  52. foreach ($traces AS $trance) {
  53. $traceString .= "&nbsp;&nbsp;&nbsp;&nbsp;" . $trance . '<br />';
  54. }
  55. $message[] = $traceString;
  56. if (\Qii::getInstance()->logerWriter != null) {
  57. $message[] = 'Source URL:' . \Qii::getInstance()->request->url->getCurrentURL();
  58. $message[] = 'Referer URL:' . (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : \Qii::getInstance()->request->url->getCurrentURL());
  59. \Qii::getInstance()->logerWriter->writeLog($message);
  60. }
  61. $appConfigure = Qii_Config_Register::AppConfigure();
  62. $env = Qii_Config_Register::get(\Qii_Const_Config::APP_ENVIRON, 'dev');
  63. if ($env == 'product' || ($appConfigure['errorPage'] && (isset($appConfigure['debug']) && $appConfigure['debug'] == 0))) {
  64. list($controller, $action) = explode(':', $appConfigure['errorPage']);
  65. $controllerCls = \Qii_Config_Register::get(\Qii_Const_Config::APP_DEFAULT_CONTROLLER_PREFIX) . '_' . $controller;
  66. $action = preg_replace('/(Action)$/i', "", $action);
  67. $filePath = \Qii_Autoloader_Import::requireByClass($controllerCls);
  68. if (!is_file($filePath)) {
  69. if ($env == 'product') return '';
  70. \Qii_Autoloader_Import::requires(Qii_DIR . DS . 'Exceptions' . DS . 'Error.php');
  71. call_user_func_array(array('\Qii_Exceptions_Error', 'index'), array($controller, $action));
  72. die();
  73. } else {
  74. \Qii::getInstance()->request->setControllerName($controller);
  75. \Qii::getInstance()->request->setActionName($action);
  76. \Qii::getInstance()->dispatcher->setRequest(\Qii::getInstance()->request);
  77. \Qii::getInstance()->dispatcher->dispatch($controller, $action, $e);
  78. die();
  79. }
  80. }
  81. include(join(DS, array(Qii_DIR, 'Exceptions', 'View', 'error.php')));
  82. }
  83. /**
  84. * 获取指定文件的指定行内容
  85. *
  86. * @param String $fileName 文件名
  87. * @param Int $line 行号
  88. * @return String
  89. */
  90. public static function getLineMessage($fileName, $line)
  91. {
  92. $seekline = max(0, $line - 1);
  93. $spl = new \SplFileObject($fileName);
  94. $code = array();
  95. if ($line > 1) {
  96. $maxLine = 10;
  97. $firstLine = max(0, $seekline - $maxLine);
  98. $spl->seek($firstLine);
  99. $min = $seekline - $maxLine;
  100. $max = $seekline + $maxLine + 1;
  101. for ($i = $min; $i < $max; $i++) {
  102. $currentLine = $i + ($min < 0 ? abs($min) : 0) + 1;
  103. $color = $currentLine == $line ? ' color="red"' : '';
  104. if ($spl->eof()) break;
  105. $code[] = '<font ' . $color . '>' . $currentLine . ':</font>' . "\t" . '<font ' . $color . '>' . htmlspecialchars(rtrim($spl->current())) . '</font>';
  106. $spl->next();
  107. }
  108. } else {
  109. $spl->seek($seekline);
  110. $code[] = htmlspecialchars(rtrim($spl->current()));
  111. }
  112. return '<pre style="font-weight:bold;">' . join("<br />", $code) . '</pre>';
  113. }
  114. /**
  115. * sprintf 格式化语言错误信息内容
  116. *
  117. *
  118. * Qii::e($message, $argv1, $argv2, ..., $line);
  119. * $message = sprintf($message, $argv1, $argv2, ...);
  120. * throw new \Qii_Exceptions_Errors($message, $line);
  121. */
  122. public static function e()
  123. {
  124. $argvs = func_get_args();
  125. $count = count($argvs);
  126. $message = array_shift($argvs);
  127. $line = (int) array_pop($argvs);
  128. if ($count == 2) {
  129. throw new \Qii_Exceptions_Errors($message, $line);
  130. }
  131. $message = vsprintf($message, $argvs);
  132. throw new \Qii_Exceptions_Errors($message, $line);
  133. }
  134. }