| @@ 8-48 (lines=41) @@ | ||
| 5 | use Illuminate\Support\Collection; |
|
| 6 | use Symfony\Component\HttpFoundation\Response; |
|
| 7 | ||
| 8 | class Alert implements Output |
|
| 9 | { |
|
| 10 | public function output(Collection $detectedQueries, Response $response) |
|
| 11 | { |
|
| 12 | if ($response->isRedirection()) { |
|
| 13 | return; |
|
| 14 | } |
|
| 15 | ||
| 16 | $content = $response->getContent(); |
|
| 17 | ||
| 18 | $outputContent = $this->getOutputContent($detectedQueries); |
|
| 19 | ||
| 20 | $pos = strripos($content, '</body>'); |
|
| 21 | ||
| 22 | if (false !== $pos) { |
|
| 23 | $content = substr($content, 0, $pos) . $outputContent . substr($content, $pos); |
|
| 24 | } else { |
|
| 25 | $content = $content . $outputContent; |
|
| 26 | } |
|
| 27 | ||
| 28 | // Update the new content and reset the content length |
|
| 29 | $response->setContent($content); |
|
| 30 | ||
| 31 | $response->headers->remove('Content-Length'); |
|
| 32 | } |
|
| 33 | ||
| 34 | protected function getOutputContent(Collection $detectedQueries) |
|
| 35 | { |
|
| 36 | $output = '<script type="text/javascript">'; |
|
| 37 | $output .= "alert('Found the following N+1 queries in this request:\\n\\n"; |
|
| 38 | foreach ($detectedQueries as $detectedQuery) { |
|
| 39 | $output .= "Model: ".addslashes($detectedQuery['model']). " => Relation: ".addslashes($detectedQuery['relation']); |
|
| 40 | $output .= " - You should add \"with(\'".$detectedQuery['relation']."\')\" to eager-load this relation."; |
|
| 41 | $output .= "\\n"; |
|
| 42 | } |
|
| 43 | $output .= "')"; |
|
| 44 | $output .= '</script>'; |
|
| 45 | ||
| 46 | return $output; |
|
| 47 | } |
|
| 48 | } |
|
| @@ 5-37 (lines=33) @@ | ||
| 2 | namespace BeyondCode\QueryDetector\Outputs; |
|
| 3 | use Illuminate\Support\Collection; |
|
| 4 | use Symfony\Component\HttpFoundation\Response; |
|
| 5 | class Console implements Output |
|
| 6 | { |
|
| 7 | public function output(Collection $detectedQueries, Response $response) |
|
| 8 | { |
|
| 9 | if ($response->isRedirection()) { |
|
| 10 | return; |
|
| 11 | } |
|
| 12 | $content = $response->getContent(); |
|
| 13 | $outputContent = $this->getOutputContent($detectedQueries); |
|
| 14 | $pos = strripos($content, '</body>'); |
|
| 15 | if (false !== $pos) { |
|
| 16 | $content = substr($content, 0, $pos) . $outputContent . substr($content, $pos); |
|
| 17 | } else { |
|
| 18 | $content = $content . $outputContent; |
|
| 19 | } |
|
| 20 | // Update the new content and reset the content length |
|
| 21 | $response->setContent($content); |
|
| 22 | $response->headers->remove('Content-Length'); |
|
| 23 | } |
|
| 24 | protected function getOutputContent(Collection $detectedQueries) |
|
| 25 | { |
|
| 26 | $output = '<script type="text/javascript">'; |
|
| 27 | $output .= "console.warn('Found the following N+1 queries in this request:\\n\\n"; |
|
| 28 | foreach ($detectedQueries as $detectedQuery) { |
|
| 29 | $output .= "Model: ".addslashes($detectedQuery['model']). " => Relation: ".addslashes($detectedQuery['relation']); |
|
| 30 | $output .= " - You should add \"with(\'".$detectedQuery['relation']."\')\" to eager-load this relation."; |
|
| 31 | $output .= "\\n"; |
|
| 32 | } |
|
| 33 | $output .= "')"; |
|
| 34 | $output .= '</script>'; |
|
| 35 | return $output; |
|
| 36 | } |
|
| 37 | } |
|
| 38 | ||