|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BeyondCode\QueryDetector\Outputs; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
7
|
|
|
|
|
8
|
|
|
class Alert implements Output |
|
9
|
|
|
{ |
|
10
|
|
|
public function boot() |
|
11
|
|
|
{ |
|
12
|
|
|
// |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
public function output(Collection $detectedQueries, Response $response) |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
$contentType = $response->headers->get('Content-Type'); |
|
19
|
|
|
|
|
20
|
|
|
$validateContentTypeHTML = true; |
|
21
|
|
|
|
|
22
|
|
|
if (env('QUERY_DETECTOR_AJAX', false)) { |
|
23
|
|
|
if (request()->ajax()) { |
|
24
|
|
|
if (stripos($contentType, 'application/json') === false || $response->isRedirection()) { |
|
25
|
|
|
return; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$validateContentTypeHTML = false; |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if ($validateContentTypeHTML) { |
|
33
|
|
|
if (stripos($contentType, 'text/html') !== 0 || $response->isRedirection()) { |
|
34
|
|
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$content = $response->getContent(); |
|
39
|
|
|
|
|
40
|
|
|
$outputContent = $this->getOutputContent($detectedQueries); |
|
41
|
|
|
|
|
42
|
|
|
$pos = strripos($content, '</body>'); |
|
43
|
|
|
|
|
44
|
|
|
if (false !== $pos) { |
|
45
|
|
|
$content = substr($content, 0, $pos) . $outputContent . substr($content, $pos); |
|
46
|
|
|
} else { |
|
47
|
|
|
if (!request()->ajax()) { |
|
48
|
|
|
$content = $content . $outputContent; |
|
49
|
|
|
} else { |
|
50
|
|
|
$jsonResponseContent = json_decode($content); |
|
51
|
|
|
|
|
52
|
|
|
$jsonResponseContent->laravelQueryDetector = $outputContent; |
|
53
|
|
|
|
|
54
|
|
|
$content = json_encode($jsonResponseContent); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// Update the new content and reset the content length |
|
59
|
|
|
$response->setContent($content); |
|
60
|
|
|
|
|
61
|
|
|
$response->headers->remove('Content-Length'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function getOutputContent(Collection $detectedQueries) |
|
65
|
|
|
{ |
|
66
|
|
|
if (!request()->ajax()) { |
|
67
|
|
|
$output = '<script type="text/javascript">'; |
|
68
|
|
|
$output .= "alert('Found the following N+1 queries in this request:\\n\\n"; |
|
69
|
|
|
|
|
70
|
|
|
foreach ($detectedQueries as $detectedQuery) { |
|
71
|
|
|
$output .= "Model: " . addslashes($detectedQuery['model']) . " => Relation: " . addslashes($detectedQuery['relation']); |
|
72
|
|
|
$output .= " - You should add \"with(\'" . addslashes($detectedQuery['relation']) . "\')\" to eager-load this relation."; |
|
73
|
|
|
$output .= "\\n"; |
|
74
|
|
|
} |
|
75
|
|
|
$output .= "')"; |
|
76
|
|
|
$output .= '</script>'; |
|
77
|
|
|
} else { |
|
78
|
|
|
$output = "Found the following N+1 queries in this request:\n\n"; |
|
79
|
|
|
|
|
80
|
|
|
foreach ($detectedQueries as $detectedQuery) { |
|
81
|
|
|
$output .= "Model: " . $detectedQuery['model'] . " => Relation: " . $detectedQuery['relation']; |
|
82
|
|
|
$output .= " - You should add \"with('" . $detectedQuery['relation'] . "')\" to eager-load this relation."; |
|
83
|
|
|
$output .= "\n"; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $output; |
|
88
|
|
|
} |
|
89
|
|
|
} |