Passed
Push — master ( b4f6c4...81b9da )
by Thomas
02:51 queued 10s
created

code/Middleware/DebugBarMiddleware.php (7 issues)

1
<?php
2
3
namespace LeKoala\DebugBar\Middleware;
4
5
use LeKoala\DebugBar\DebugBar;
6
use LeKoala\DebugBar\Extension\ProxyDBExtension;
7
use LeKoala\DebugBar\Proxy\SSViewerProxy;
8
use SilverStripe\Control\Middleware\HTTPMiddleware;
9
use SilverStripe\Control\Director;
10
use SilverStripe\Control\HTTPRequest;
11
use SilverStripe\Control\HTTPResponse;
12
use SilverStripe\View\Requirements;
13
14
class DebugBarMiddleware implements HTTPMiddleware
15
{
16
    public function process(HTTPRequest $request, callable $delegate)
17
    {
18
        $this->beforeRequest($request);
19
        $response = $delegate($request);
20
        if ($response) {
21
            $this->afterRequest($request, $response);
22
        }
23
        return $response;
24
    }
25
26
    /**
27
     * Track the start up of the framework boot
28
     *
29
     * @param HTTPRequest $request
30
     */
31
    protected function beforeRequest(HTTPRequest $request)
0 ignored issues
show
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

31
    protected function beforeRequest(/** @scrutinizer ignore-unused */ HTTPRequest $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
    {
33
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) {
34
            /** @var DebugBar\DataCollector\TimeDataCollector $timeData */
35
            $timeData = $debugbar->getCollector('time');
36
37
            if (!$timeData) {
0 ignored issues
show
$timeData is of type LeKoala\DebugBar\DebugBa...ector\TimeDataCollector, thus it always evaluated to true.
Loading history...
38
                return;
39
            }
40
41
            if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
42
                $timeData = $debugbar['time'];
43
                $timeData->addMeasure(
0 ignored issues
show
The method addMeasure() does not exist on DebugBar\DataCollector\DataCollectorInterface. It seems like you code against a sub-type of DebugBar\DataCollector\DataCollectorInterface such as DebugBar\DataCollector\TimeDataCollector. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
                $timeData->/** @scrutinizer ignore-call */ 
44
                           addMeasure(
Loading history...
44
                    'framework boot',
45
                    $_SERVER['REQUEST_TIME_FLOAT'],
46
                    microtime(true)
47
                );
48
            }
49
            $timeData->startMeasure('pre_request', 'pre request');
0 ignored issues
show
The method startMeasure() does not exist on DebugBar\DataCollector\DataCollectorInterface. It seems like you code against a sub-type of DebugBar\DataCollector\DataCollectorInterface such as DebugBar\DataCollector\TimeDataCollector. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
            $timeData->/** @scrutinizer ignore-call */ 
50
                       startMeasure('pre_request', 'pre request');
Loading history...
50
        });
51
    }
52
53
    /**
54
     * Inject DebugBar requirements for the frontend
55
     *
56
     * @param HTTPRequest  $request
57
     * @param HTTPResponse $response
58
     */
59
    protected function afterRequest(HTTPRequest $request, HTTPResponse $response)
60
    {
61
        $debugbar = DebugBar::getDebugBar();
62
        if (!$debugbar) {
0 ignored issues
show
$debugbar is of type DebugBar\DebugBar, thus it always evaluated to true.
Loading history...
63
            return;
64
        }
65
        DebugBar::setRequest($request);
66
67
        // All queries have been displayed
68
        if (DebugBar::getShowQueries()) {
69
            exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
70
        }
71
72
        $script = DebugBar::renderDebugBar();
73
74
        // If the bar is not renderable, return early
75
        if (!$script) {
76
            return;
77
        }
78
79
        // Inject init script into the HTML response
80
        $body = $response->getBody();
81
        if (strpos($body, '</body>') !== false) {
82
            if (Requirements::get_write_js_to_body()) {
83
                $body = str_replace('</body>', $script . '</body>', $body);
84
            } else {
85
                // Ensure every js script is properly loaded before firing custom script
86
                $script = strip_tags($script);
87
                $script = "window.addEventListener('DOMContentLoaded', function() { $script });";
88
                $script = '<script type="application/javascript">//<![CDATA[' . "\n" . $script . "\n</script>";
89
                $body = str_replace('</head>', $script . '</head>', $body);
90
            }
91
            $response->setBody($body);
92
        }
93
94
        // Ajax support
95
        if (Director::is_ajax() && !headers_sent()) {
96
            if (DebugBar::isAdminUrl() && !DebugBar::config()->get('enabled_in_admin')) {
97
                return;
98
            }
99
            // Skip anything that is not a GET request
100
            if (!$request->isGET()) {
101
                return;
102
            }
103
            // Always enable in admin because everything is mostly loaded through ajax
104
            if (DebugBar::config()->get('ajax') || DebugBar::isAdminUrl()) {
105
                $headers = $debugbar->getDataAsHeaders();
106
107
                // Prevent throwing js errors in case header size is too large
108
                if (is_array($headers)) {
0 ignored issues
show
The condition is_array($headers) is always true.
Loading history...
109
                    $maxHeaderLength = DebugBar::config()->get('max_header_length');
110
                    $debugbar->sendDataInHeaders(null, 'phpdebugbar', $maxHeaderLength);
111
                }
112
            }
113
        }
114
    }
115
}
116