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 <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
declare (strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace think\middleware; |
14
|
|
|
|
15
|
|
|
use Closure; |
16
|
|
|
use think\App; |
17
|
|
|
use think\Config; |
18
|
|
|
use think\Request; |
19
|
|
|
use think\Response; |
20
|
|
|
|
21
|
|
|
class TraceDebug |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* 配置参数 |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $config = []; |
29
|
|
|
|
30
|
|
|
/** @var App */ |
|
|
|
|
31
|
|
|
protected $app; |
32
|
|
|
|
33
|
|
|
public function __construct(App $app, Config $config) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$this->app = $app; |
36
|
|
|
$this->config = $config->get('trace'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* 页面Trace调试 |
41
|
|
|
* @access public |
42
|
|
|
* @param Request $request |
|
|
|
|
43
|
|
|
* @param Closure $next |
|
|
|
|
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function handle($request, Closure $next) |
47
|
|
|
{ |
48
|
|
|
$response = $next($request); |
49
|
|
|
|
50
|
|
|
// Trace调试注入 |
51
|
|
|
if ('cli' != PHP_SAPI && $this->app->env->get('app_trace', $this->app->config->get('app.app_trace'))) { |
52
|
|
|
$data = $response->getContent(); |
53
|
|
|
$this->traceDebug($response, $data); |
54
|
|
|
$response->content($data); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $response; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function traceDebug(Response $response, &$content) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$config = $this->config; |
63
|
|
|
$type = $config['type'] ?? 'Html'; |
64
|
|
|
|
65
|
|
|
unset($config['type']); |
66
|
|
|
|
67
|
|
|
$trace = App::factory($type, '\\think\\debug\\', $config); |
68
|
|
|
|
69
|
|
|
if ($response instanceof Redirect) { |
|
|
|
|
70
|
|
|
//TODO 记录 |
71
|
|
|
} else { |
72
|
|
|
$output = $trace->output($response, $this->app->log->getLog()); |
73
|
|
|
if (is_string($output)) { |
74
|
|
|
// trace调试信息注入 |
75
|
|
|
$pos = strripos($content, '</body>'); |
76
|
|
|
if (false !== $pos) { |
77
|
|
|
$content = substr($content, 0, $pos) . $output . substr($content, $pos); |
78
|
|
|
} else { |
79
|
|
|
$content = $content . $output; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|