1 | <?php |
||||||
2 | |||||||
3 | namespace LeKoala\DebugBar\Middleware; |
||||||
4 | |||||||
5 | use LeKoala\DebugBar\DebugBar; |
||||||
6 | use SilverStripe\Control\Middleware\HTTPMiddleware; |
||||||
7 | use SilverStripe\Control\Director; |
||||||
8 | use SilverStripe\Control\HTTPRequest; |
||||||
9 | use SilverStripe\Control\HTTPResponse; |
||||||
10 | use SilverStripe\View\Requirements; |
||||||
11 | |||||||
12 | class DebugBarMiddleware implements HTTPMiddleware |
||||||
13 | { |
||||||
14 | public function process(HTTPRequest $request, callable $delegate) |
||||||
15 | { |
||||||
16 | $this->beforeRequest($request); |
||||||
17 | $response = $delegate($request); |
||||||
18 | if ($response) { |
||||||
19 | $this->afterRequest($request, $response); |
||||||
20 | } |
||||||
21 | return $response; |
||||||
22 | } |
||||||
23 | |||||||
24 | /** |
||||||
25 | * Track the start up of the framework boot |
||||||
26 | * |
||||||
27 | * @param HTTPRequest $request |
||||||
28 | * @return void |
||||||
29 | */ |
||||||
30 | protected function beforeRequest(HTTPRequest $request) |
||||||
0 ignored issues
–
show
|
|||||||
31 | { |
||||||
32 | DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) { |
||||||
33 | /** @var \DebugBar\DataCollector\TimeDataCollector|null $timeData */ |
||||||
34 | $timeData = $debugbar->getCollector('time'); |
||||||
35 | |||||||
36 | if (!$timeData) { |
||||||
0 ignored issues
–
show
|
|||||||
37 | return; |
||||||
38 | } |
||||||
39 | |||||||
40 | // Allows a more realistic view |
||||||
41 | // Define FRAMEWORK_BOOT_TIME in your index.php after composer autoload |
||||||
42 | if (defined('FRAMEWORK_BOOT_TIME')) { |
||||||
43 | if (isset($_SERVER['REQUEST_TIME_FLOAT'])) { |
||||||
44 | $timeData = $debugbar['time']; |
||||||
45 | $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
![]() |
|||||||
46 | 'php boot', |
||||||
47 | $_SERVER['REQUEST_TIME_FLOAT'], |
||||||
48 | constant('FRAMEWORK_BOOT_TIME') |
||||||
49 | ); |
||||||
50 | $timeData->addMeasure( |
||||||
51 | 'framework boot', |
||||||
52 | constant('FRAMEWORK_BOOT_TIME'), |
||||||
53 | microtime(true) |
||||||
54 | ); |
||||||
55 | } else { |
||||||
56 | $timeData = $debugbar['time']; |
||||||
57 | $timeData->addMeasure( |
||||||
58 | 'framework boot', |
||||||
59 | constant('FRAMEWORK_BOOT_TIME'), |
||||||
60 | microtime(true) |
||||||
61 | ); |
||||||
62 | } |
||||||
63 | } elseif (isset($_SERVER['REQUEST_TIME_FLOAT'])) { |
||||||
64 | $timeData = $debugbar['time']; |
||||||
65 | $timeData->addMeasure( |
||||||
66 | 'framework boot', |
||||||
67 | $_SERVER['REQUEST_TIME_FLOAT'], |
||||||
68 | microtime(true) |
||||||
69 | ); |
||||||
70 | } |
||||||
71 | |||||||
72 | DebugBar::closeExtraTime(); |
||||||
73 | $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
![]() |
|||||||
74 | }); |
||||||
75 | } |
||||||
76 | |||||||
77 | /** |
||||||
78 | * Inject DebugBar requirements for the frontend |
||||||
79 | * |
||||||
80 | * @param HTTPRequest $request |
||||||
81 | * @param HTTPResponse $response |
||||||
82 | * @return void |
||||||
83 | */ |
||||||
84 | protected function afterRequest(HTTPRequest $request, HTTPResponse $response) |
||||||
85 | { |
||||||
86 | $debugbar = DebugBar::getDebugBar(); |
||||||
87 | if (!$debugbar) { |
||||||
0 ignored issues
–
show
|
|||||||
88 | return; |
||||||
89 | } |
||||||
90 | |||||||
91 | // Don't apply to assets |
||||||
92 | $dir = defined('ASSETS_DIR') ? ASSETS_DIR : 'assets'; |
||||||
93 | if (strpos($request->getURL(), "$dir/") === 0) { |
||||||
94 | return; |
||||||
95 | } |
||||||
96 | |||||||
97 | DebugBar::setRequest($request); |
||||||
98 | |||||||
99 | // All queries have been displayed |
||||||
100 | if (DebugBar::getShowQueries()) { |
||||||
101 | exit(); |
||||||
0 ignored issues
–
show
|
|||||||
102 | } |
||||||
103 | $script = DebugBar::renderDebugBar(); |
||||||
104 | |||||||
105 | // If the bar is not renderable, return early |
||||||
106 | if (!$script) { |
||||||
107 | return; |
||||||
108 | } |
||||||
109 | |||||||
110 | // Use module to make sure it's deferred and does not cause side effects |
||||||
111 | $script = str_replace('<script type="text/javascript">', '<script type="module">', $script); |
||||||
112 | |||||||
113 | // Inject init script into the HTML response |
||||||
114 | $body = (string)$response->getBody(); |
||||||
115 | |||||||
116 | if (strpos($body, '</body>') !== false) { |
||||||
117 | $customScripts = ''; |
||||||
118 | if (DebugBar::$suppressJquery) { |
||||||
119 | // Move scripts after the latest script |
||||||
120 | $matches = []; |
||||||
121 | preg_match_all('/<script(.*) src="(.*)\/debugbar\/(.*)"><\/script>/', $body, $matches); |
||||||
122 | $body = preg_replace('/<script(.*) src="(.*)\/debugbar\/(.*)"><\/script>\n/', '', $body); |
||||||
123 | $customScripts = implode("\n", $matches[0]); |
||||||
124 | } |
||||||
125 | |||||||
126 | $scriptsToInsert = $customScripts . "\n" . $script; |
||||||
127 | // You can use a placeholder if somehow you have a </body> string somewhere else in the body |
||||||
128 | // @link https://github.com/lekoala/silverstripe-debugbar/issues/154 |
||||||
129 | $debugbarPlaceholder = "<!-- debugbar -->"; |
||||||
130 | if (strpos((string) $body, $debugbarPlaceholder) !== false) { |
||||||
131 | $body = str_replace($debugbarPlaceholder, $scriptsToInsert, $body); |
||||||
132 | } else { |
||||||
133 | if (Requirements::get_write_js_to_body()) { |
||||||
134 | // Replace the last occurence of </body> |
||||||
135 | $pos = strrpos((string) $body, '</body>'); |
||||||
136 | if ($pos !== false) { |
||||||
137 | $body = substr_replace($body, $scriptsToInsert . '</body>', $pos, strlen('</body>')); |
||||||
138 | } |
||||||
139 | } else { |
||||||
140 | // Replace the first occurence of </head> |
||||||
141 | $pos = strpos((string) $body, '</head>'); |
||||||
142 | if ($pos !== false) { |
||||||
143 | $body = substr_replace($body, $scriptsToInsert . '</head>', $pos, strlen('</head>')); |
||||||
144 | } |
||||||
145 | } |
||||||
146 | } |
||||||
147 | $response->setBody($body); |
||||||
0 ignored issues
–
show
It seems like
$body can also be of type array ; however, parameter $body of SilverStripe\Control\HTTPResponse::setBody() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
148 | } |
||||||
149 | |||||||
150 | // Ajax support |
||||||
151 | if (Director::is_ajax() && !headers_sent()) { |
||||||
152 | if (DebugBar::isAdminUrl() && !DebugBar::config()->get('enabled_in_admin')) { |
||||||
153 | return; |
||||||
154 | } |
||||||
155 | // Skip anything that is not a GET request |
||||||
156 | if (!$request->isGET()) { |
||||||
157 | return; |
||||||
158 | } |
||||||
159 | // Always enable in admin because everything is mostly loaded through ajax |
||||||
160 | if (DebugBar::config()->get('ajax') || DebugBar::isAdminUrl()) { |
||||||
161 | $headers = $debugbar->getDataAsHeaders(); |
||||||
162 | |||||||
163 | // Prevent throwing js errors in case header size is too large |
||||||
164 | if (is_array($headers)) { |
||||||
0 ignored issues
–
show
|
|||||||
165 | $maxHeaderLength = DebugBar::config()->get('max_header_length'); |
||||||
166 | $debugbar->sendDataInHeaders(null, 'phpdebugbar', $maxHeaderLength); |
||||||
167 | } |
||||||
168 | } |
||||||
169 | } |
||||||
170 | } |
||||||
171 | } |
||||||
172 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.