|
5 | 5 | use Closure; |
6 | 6 | use Illuminate\Contracts\Http\Kernel; |
7 | 7 | use Illuminate\Http\Request; |
| 8 | +use Illuminate\Http\Response; |
8 | 9 | use Illuminate\Support\Facades\Route; |
9 | | -use Symfony\Component\HttpFoundation\Response; |
10 | 10 |
|
11 | 11 | class DebuggerMiddleware |
12 | 12 | { |
@@ -88,24 +88,36 @@ protected function storeProfilerTrace(string $url, Debugger $debugger, bool $isB |
88 | 88 | } |
89 | 89 |
|
90 | 90 | /** |
91 | | - * Inject the debug bar HTML after the opening <body> tag. |
| 91 | + * Inject the debug bar HTML before the closing </body> tag. |
| 92 | + * |
| 93 | + * Based on https://github.com/fruitcake/laravel-debugbar/blob/master/src/LaravelDebugbar.php |
92 | 94 | */ |
93 | 95 | protected function injectDebugger(Response $response, Debugger $debugger): void |
94 | 96 | { |
95 | | - if (! method_exists($response, 'getContent')) { |
96 | | - return; |
97 | | - } |
98 | | - |
99 | 97 | $content = $response->getContent(); |
100 | 98 |
|
101 | | - if (! $content || ! preg_match('/<body[^>]*>/i', $content, $matches, PREG_OFFSET_CAPTURE)) { |
102 | | - return; |
| 99 | + $widget = "<!-- Blaze Widget -->\n" . $debugger->render(); |
| 100 | + |
| 101 | + // Try to put the widget at the end, directly before the </body> |
| 102 | + $pos = strripos($content, '</body>'); |
| 103 | + if (false !== $pos) { |
| 104 | + $content = substr($content, 0, $pos) . $widget . substr($content, $pos); |
| 105 | + } else { |
| 106 | + $content = $content . $widget; |
| 107 | + } |
| 108 | + |
| 109 | + $original = null; |
| 110 | + if ($response->getOriginalContent()) { |
| 111 | + $original = $response->getOriginalContent(); |
103 | 112 | } |
104 | 113 |
|
105 | | - $insertPos = $matches[0][1] + strlen($matches[0][0]); |
| 114 | + // Update the new content and reset the content length |
| 115 | + $response->setContent($content); |
| 116 | + $response->headers->remove('Content-Length'); |
106 | 117 |
|
107 | | - $response->setContent( |
108 | | - substr($content, 0, $insertPos) . "\n" . $debugger->render() . substr($content, $insertPos) |
109 | | - ); |
| 118 | + // Restore original response (e.g. the View or Ajax data) |
| 119 | + if ($original) { |
| 120 | + $response->original = $original; |
| 121 | + } |
110 | 122 | } |
111 | 123 | } |
0 commit comments