Skip to content

Commit b2b58c3

Browse files
committed
Merge branch 'master' of github.com:beyondcode/laravel-query-detector
2 parents 308eba2 + d3763f5 commit b2b58c3

File tree

7 files changed

+119
-11
lines changed

7 files changed

+119
-11
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ composer.lock
33
docs
44
vendor
55
coverage
6-
.idea
6+
.idea
7+
nbproject

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
The Laravel N+1 query detector helps you to increase your application's performance by reducing the number of queries it executes. This package monitors your queries in real-time, while you develop your application and notify you when you should add eager loading (N+1 queries).
99

10-
![Example alert](https://beyondco.de/github/n+1/alert.png)
10+
![Example alert](https://beyondco.de/github/n+1/alert.png)
1111

1212
## Installation
1313

@@ -42,7 +42,7 @@ return [
4242
* If this is set to "null", the app.debug config value will be used.
4343
*/
4444
'enabled' => env('QUERY_DETECTOR_ENABLED', null),
45-
45+
4646
/*
4747
* Threshold level for the N+1 query detection. If a relation query will be
4848
* executed more then this amount, the detector will notify you about it.
@@ -64,7 +64,7 @@ return [
6464
],
6565

6666
/*
67-
* Define the output format that you want to use.
67+
* Define the output format that you want to use. Multiple classes are supported
6868
* Available options are:
6969
*
7070
* Alert:
@@ -75,11 +75,19 @@ return [
7575
* Writes the N+1 queries into the Laravel.log file
7676
* \BeyondCode\QueryDetector\Outputs\Log::class
7777
*/
78-
'output' => \BeyondCode\QueryDetector\Outputs\Alert::class,
78+
'output' => [
79+
\BeyondCode\QueryDetector\Outputs\Alert::class
80+
]
7981

8082
];
8183
```
8284

85+
If you use **Lumen**, you need to copy the config file manually and register the Lumen Service Provider in `bootstrap/app.php` file
86+
87+
```php
88+
$this->app->register(\BeyondCode\QueryDetector\LumenQueryDetectorServiceProvider::class);
89+
```
90+
8391
### Testing
8492

8593
``` bash

config/config.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,23 @@
2222
],
2323

2424
/*
25-
* Define the output format that you want to use.
25+
* Define the output formats that you want to use.
2626
* Available options are:
2727
*
2828
* Alert:
2929
* Displays an alert on the website
3030
* \BeyondCode\QueryDetector\Outputs\Alert::class
3131
*
32+
* Debugbar: (make sure you have the barryvdh/laravel-debugbar package installed)
33+
* Writes the N+1 queries into a custom messages collector of Debugbar
34+
* \BeyondCode\QueryDetector\Outputs\Debugbar::class
35+
*
3236
* Log:
3337
* Writes the N+1 queries into the Laravel.log file
3438
* \BeyondCode\QueryDetector\Outputs\Log::class
3539
*/
36-
'output' => \BeyondCode\QueryDetector\Outputs\Alert::class,
37-
38-
];
40+
'output' => [
41+
\BeyondCode\QueryDetector\Outputs\Log::class,
42+
\BeyondCode\QueryDetector\Outputs\Alert::class,
43+
]
44+
];
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace BeyondCode\QueryDetector;
3+
4+
use Illuminate\Support\ServiceProvider;
5+
6+
class LumenQueryDetectorServiceProvider extends ServiceProvider
7+
{
8+
9+
public function register()
10+
{
11+
$this->app->configure('querydetector');
12+
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'querydetector');
13+
14+
$this->app->middleware([
15+
QueryDetectorMiddleware::class
16+
]);
17+
18+
$this->app->singleton(QueryDetector::class);
19+
$this->app->alias(QueryDetector::class, 'querydetector');
20+
}
21+
}

src/Outputs/Console.php

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

src/Outputs/Debugbar.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace BeyondCode\QueryDetector\Outputs;
4+
5+
use Illuminate\Support\Collection;
6+
use Symfony\Component\HttpFoundation\Response;
7+
8+
use Debugbar as LaravelDebugbar;
9+
use DebugBar\DataCollector\MessagesCollector;
10+
11+
class Debugbar implements Output
12+
{
13+
public function output(Collection $detectedQueries, Response $response)
14+
{
15+
$collector = new MessagesCollector('N+1 Queries');
16+
17+
foreach ($detectedQueries as $detectedQuery) {
18+
$collector->addMessage(sprintf('Model: %s => Relation: %s - You should add with(%s) to eager-load this relation.',
19+
$detectedQuery['model'],
20+
$detectedQuery['relation'],
21+
$detectedQuery['relation']
22+
));
23+
}
24+
25+
LaravelDebugbar::addCollector($collector);
26+
}
27+
}

src/QueryDetector.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,15 @@ public function getDetectedQueries(): Collection
172172

173173
protected function applyOutput(Response $response)
174174
{
175-
$outputType = app(config('querydetector.output'));
176-
$outputType->output($this->getDetectedQueries(), $response);
175+
$outputTypes = app(config('querydetector.output'));
176+
177+
if (! is_array($outputTypes)) {
178+
$types = [$outputTypes];
179+
}
180+
181+
foreach ($outputTypes as $type) {
182+
$type->output($this->getDetectedQueries(), $response);
183+
}
177184
}
178185

179186
public function output($request, $response)

0 commit comments

Comments
 (0)