Error.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace \Qii\Exceptions;
  3. /**
  4. *
  5. * Class Qii_Exceptions_Error
  6. * @package Qii
  7. */
  8. class Error
  9. {
  10. const VERSION = '1.2';
  11. public function __construct()
  12. {
  13. }
  14. public static function index()
  15. {
  16. $args = func_get_args();
  17. echo \Qii::i('1108', $args[0], $args[1]);
  18. }
  19. /**
  20. * 返回程序的调用栈
  21. */
  22. static public function getTrace()
  23. {
  24. return debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  25. }
  26. /**
  27. * 程序的调用栈生成字符串
  28. */
  29. static public function getTraceAsString()
  30. {
  31. $e = new \Exception();
  32. $trace = explode("\n", $e->getTraceAsString());
  33. $trace = array_reverse($trace);
  34. array_shift($trace);
  35. array_pop($trace);
  36. $length = count($trace);
  37. $result = array();
  38. for ($i = 0; $i < $length; $i++) {
  39. $result[] = ($i + 1) . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering
  40. }
  41. return "\t" . implode("\n\t", $result);
  42. }
  43. /**
  44. * 错误描述
  45. * @return string
  46. */
  47. static public function getMessage()
  48. {
  49. $e = new \Exception();
  50. return $e->getMessage();
  51. }
  52. /**
  53. * 错误码
  54. * @return int
  55. */
  56. static public function getCode()
  57. {
  58. return __LINE__;
  59. }
  60. /**
  61. * 错误设置,如果满足给定的条件就直接返回false,否则在设置了错误页面的情况下返回true
  62. * 如果出错后要终止的话,需要自行处理,此方法不错停止不执行
  63. *
  64. * @param bool $condition
  65. * @param int $line 出错的行数,这样以便轻松定位错误
  66. * @param string $msg
  67. * @param int|string $code
  68. * @param string $args ...
  69. * @return bool
  70. */
  71. public static function setError($condition, $line = 0, $code, $args = null, $msg = null)
  72. {
  73. if ($condition) {
  74. return false;
  75. }
  76. $appConfigure = \Qii_Config_Register::AppConfigure();
  77. //如果是调试模式就直接抛出异常
  78. $isDebug = $appConfigure['debug'];
  79. $message = array();
  80. $message[] = explode("\n", self::getTraceAsString());
  81. if (\Qii::getInstance()->logerWriter != null) {
  82. $message[] = 'Referer:' . (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : \Qii::getInstance()->request->url->getCurrentURL());
  83. \Qii::getInstance()->logerWriter->writeLog($message);
  84. }
  85. if ($isDebug) {
  86. $args = array_slice(func_get_args(), 2);
  87. throw new \Exception(call_user_func_array(array('\Qii', 'i'), $args), $line);
  88. }
  89. $errorPage = $appConfigure['errorPage'];
  90. $env = \Qii_Config_Register::get(\Qii_Const_Config::APP_ENVIRON, 'dev');
  91. if ($env != 'product' && $errorPage != null) {
  92. list($controller, $action) = explode(':', $appConfigure['errorPage']);
  93. $controllerCls = \Qii_Config_Register::get(\Qii_Const_Config::APP_DEFAULT_CONTROLLER_PREFIX) . '_' . $controller;
  94. $action = preg_replace('/(Action)$/i', "", $action);
  95. $filePath = \Qii_Autoloader_Import::requireByClass($controllerCls);
  96. if (!is_file($filePath)) {
  97. if ($env == 'product') return '';
  98. \Qii_Autoloader_Import::requires(Qii_DIR . DS . 'Exceptions' . DS . 'Error.php');
  99. call_user_func_array(array('\Qii_Exceptions_Error', 'index'), array($controller, $action));
  100. die();
  101. } else {
  102. \Qii::getInstance()->request->setControllerName($controller);
  103. \Qii::getInstance()->request->setActionName($action);
  104. \Qii::getInstance()->dispatcher->setRequest(\Qii::getInstance()->request);
  105. \Qii::getInstance()->dispatcher->dispatch($controller, $action, new \Exception($msg ? $msg : self::getTraceAsString(), $code));
  106. die();
  107. }
  108. return;
  109. }
  110. return true;
  111. }
  112. /**
  113. * 显示错误信息
  114. * @param Array $message
  115. */
  116. public static function showError($message)
  117. {
  118. include(join(DS, array(Qii_DIR ,'Exceptions', 'view', 'error.php')));
  119. }
  120. /**
  121. * 显示错误信息
  122. * @param Array $message
  123. */
  124. public static function showMessage($message)
  125. {
  126. include(join(DS, array(Qii_DIR, 'Exceptions', 'view', 'message.php')));
  127. }
  128. public function __call($method, $args)
  129. {
  130. if (method_exists(self, $method)) return call_user_func_array(array('Qii_Exceptions_Error', $method), $args);
  131. }
  132. }