Error.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 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. * @access public
  21. * @return void
  22. */
  23. public static function register()
  24. {
  25. error_reporting(E_ALL);
  26. set_error_handler([__CLASS__, 'appError']);
  27. set_exception_handler([__CLASS__, 'appException']);
  28. register_shutdown_function([__CLASS__, 'appShutdown']);
  29. }
  30. /**
  31. * Exception Handler
  32. * @access public
  33. * @param \Exception|\Throwable $e
  34. */
  35. public static function appException($e)
  36. {
  37. if (!$e instanceof \Exception) {
  38. $e = new ThrowableError($e);
  39. }
  40. self::getExceptionHandler()->report($e);
  41. if (PHP_SAPI == 'cli') {
  42. self::getExceptionHandler()->renderForConsole(new ConsoleOutput, $e);
  43. } else {
  44. self::getExceptionHandler()->render($e)->send();
  45. }
  46. }
  47. /**
  48. * Error Handler
  49. * @access public
  50. * @param integer $errno 错误编号
  51. * @param integer $errstr 详细错误信息
  52. * @param string $errfile 出错的文件
  53. * @param integer $errline 出错行号
  54. * @throws ErrorException
  55. */
  56. public static function appError($errno, $errstr, $errfile = '', $errline = 0)
  57. {
  58. $exception = new ErrorException($errno, $errstr, $errfile, $errline);
  59. if (error_reporting() & $errno) {
  60. // 将错误信息托管至 think\exception\ErrorException
  61. throw $exception;
  62. } else {
  63. self::getExceptionHandler()->report($exception);
  64. }
  65. }
  66. /**
  67. * Shutdown Handler
  68. * @access public
  69. */
  70. public static function appShutdown()
  71. {
  72. if (!is_null($error = error_get_last()) && self::isFatal($error['type'])) {
  73. // 将错误信息托管至think\ErrorException
  74. $exception = new ErrorException($error['type'], $error['message'], $error['file'], $error['line']);
  75. self::appException($exception);
  76. }
  77. // 写入日志
  78. Container::get('log')->save();
  79. }
  80. /**
  81. * 确定错误类型是否致命
  82. *
  83. * @access protected
  84. * @param int $type
  85. * @return bool
  86. */
  87. protected static function isFatal($type)
  88. {
  89. return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]);
  90. }
  91. /**
  92. * Get an instance of the exception handler.
  93. *
  94. * @access public
  95. * @return Handle
  96. */
  97. public static function getExceptionHandler()
  98. {
  99. static $handle;
  100. if (!$handle) {
  101. // 异常处理handle
  102. $class = Container::get('config')->get('exception_handle');
  103. if ($class && is_string($class) && class_exists($class) && is_subclass_of($class, "\\think\\exception\\Handle")) {
  104. $handle = new $class;
  105. } else {
  106. $handle = new Handle;
  107. if ($class instanceof \Closure) {
  108. $handle->setRender($class);
  109. }
  110. }
  111. }
  112. return $handle;
  113. }
  114. }