Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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