赛亿官网

Error.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. use think\console\Output as ConsoleOutput;
  13. use think\exception\ErrorException;
  14. use think\exception\Handle;
  15. use think\exception\ThrowableError;
  16. class Error
  17. {
  18. /**
  19. * 注册异常处理
  20. * @return void
  21. */
  22. public static function register()
  23. {
  24. error_reporting(E_ALL);
  25. set_error_handler([__CLASS__, 'appError']);
  26. set_exception_handler([__CLASS__, 'appException']);
  27. register_shutdown_function([__CLASS__, 'appShutdown']);
  28. }
  29. /**
  30. * Exception Handler
  31. * @param \Exception|\Throwable $e
  32. */
  33. public static function appException($e)
  34. {
  35. if (!$e instanceof \Exception) {
  36. $e = new ThrowableError($e);
  37. }
  38. self::getExceptionHandler()->report($e);
  39. if (IS_CLI) {
  40. self::getExceptionHandler()->renderForConsole(new ConsoleOutput, $e);
  41. } else {
  42. self::getExceptionHandler()->render($e)->send();
  43. }
  44. }
  45. /**
  46. * Error Handler
  47. * @param integer $errno 错误编号
  48. * @param integer $errstr 详细错误信息
  49. * @param string $errfile 出错的文件
  50. * @param integer $errline 出错行号
  51. * @param array $errcontext
  52. * @throws ErrorException
  53. */
  54. public static function appError($errno, $errstr, $errfile = '', $errline = 0, $errcontext = [])
  55. {
  56. $exception = new ErrorException($errno, $errstr, $errfile, $errline, $errcontext);
  57. if (error_reporting() & $errno) {
  58. // 将错误信息托管至 think\exception\ErrorException
  59. throw $exception;
  60. } else {
  61. self::getExceptionHandler()->report($exception);
  62. }
  63. }
  64. /**
  65. * Shutdown Handler
  66. */
  67. public static function appShutdown()
  68. {
  69. if (!is_null($error = error_get_last()) && self::isFatal($error['type'])) {
  70. // 将错误信息托管至think\ErrorException
  71. $exception = new ErrorException($error['type'], $error['message'], $error['file'], $error['line']);
  72. self::appException($exception);
  73. }
  74. // 写入日志
  75. Log::save();
  76. }
  77. /**
  78. * 确定错误类型是否致命
  79. *
  80. * @param int $type
  81. * @return bool
  82. */
  83. protected static function isFatal($type)
  84. {
  85. return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]);
  86. }
  87. /**
  88. * Get an instance of the exception handler.
  89. *
  90. * @return Handle
  91. */
  92. public static function getExceptionHandler()
  93. {
  94. static $handle;
  95. if (!$handle) {
  96. // 异常处理handle
  97. $class = Config::get('exception_handle');
  98. if ($class && class_exists($class) && is_subclass_of($class, "\\think\\exception\\Handle")) {
  99. $handle = new $class;
  100. } else {
  101. $handle = new Handle;
  102. }
  103. }
  104. return $handle;
  105. }
  106. }