Passed
Push — 6.0 ( de6c84...b392ff )
by liu
02:34
created

TraceDebug::traceDebug()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 2
dl 0
loc 20
rs 9.8333
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
22
{
23
24
    /**
25
     * 配置参数
26
     * @var array
27
     */
28
    protected $config = [];
29
30
    /** @var App */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
31
    protected $app;
32
33
    public function __construct(App $app, Config $config)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
34
    {
35
        $this->app    = $app;
36
        $this->config = $config->get('trace');
37
    }
38
39
    /**
40
     * 页面Trace调试
41
     * @access public
42
     * @param Request $request
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
43
     * @param Closure $next
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
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) {
0 ignored issues
show
Bug introduced by
The type think\middleware\Redirect was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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