TraceDebug.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think\trace;
  13. use Closure;
  14. use think\App;
  15. use think\Config;
  16. use think\event\LogWrite;
  17. use think\Request;
  18. use think\Response;
  19. use think\response\Redirect;
  20. /**
  21. * 页面Trace中间件
  22. */
  23. class TraceDebug
  24. {
  25. /**
  26. * Trace日志
  27. * @var array
  28. */
  29. protected $log = [];
  30. /**
  31. * 配置参数
  32. * @var array
  33. */
  34. protected $config = [];
  35. /** @var App */
  36. protected $app;
  37. public function __construct(App $app, Config $config)
  38. {
  39. $this->app = $app;
  40. $this->config = $config->get('trace');
  41. }
  42. /**
  43. * 页面Trace调试
  44. * @access public
  45. * @param Request $request
  46. * @param Closure $next
  47. * @return void
  48. */
  49. public function handle($request, Closure $next)
  50. {
  51. $debug = $this->app->isDebug();
  52. // 注册日志监听
  53. if ($debug) {
  54. $this->log = [];
  55. $this->app->event->listen(LogWrite::class, function ($event) {
  56. if (empty($this->config['channel']) || $this->config['channel'] == $event->channel) {
  57. $this->log = array_merge_recursive($this->log, $event->log);
  58. }
  59. });
  60. }
  61. $response = $next($request);
  62. // Trace调试注入
  63. if ($debug) {
  64. $data = $response->getContent();
  65. $this->traceDebug($response, $data);
  66. $response->content($data);
  67. }
  68. return $response;
  69. }
  70. public function traceDebug(Response $response, &$content)
  71. {
  72. $config = $this->config;
  73. $type = $config['type'] ?? 'Html';
  74. unset($config['type']);
  75. $trace = App::factory($type, '\\think\\trace\\', $config);
  76. if ($response instanceof Redirect) {
  77. //TODO 记录
  78. } else {
  79. $log = $this->app->log->getLog($config['channel'] ?? '');
  80. $log = array_merge_recursive($this->log, $log);
  81. $output = $trace->output($this->app, $response, $log);
  82. if (is_string($output)) {
  83. // trace调试信息注入
  84. $pos = strripos($content, '</body>');
  85. if (false !== $pos) {
  86. $content = substr($content, 0, $pos) . $output . substr($content, $pos);
  87. } else {
  88. $content = $content . $output;
  89. }
  90. }
  91. }
  92. }
  93. }