Skip to content

Commit 48583d2

Browse files
authored
Merge pull request #13 from pyr0hu/master
Fire events when queries are found
2 parents 14c6386 + 62f8f33 commit 48583d2

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ If you use **Lumen**, you need to copy the config file manually and register the
8888
$this->app->register(\BeyondCode\QueryDetector\LumenQueryDetectorServiceProvider::class);
8989
```
9090

91+
If you need additional logic to run when the package detects unoptimized queries, you can listen to the `\BeyondCode\QueryDetector\Events\QueryDetected` event and write a listener to run your own handler. (e.g. send warning to Sentry/Bugsnag, send Slack notification, etc.)
92+
9193
### Testing
9294

9395
``` bash

src/Events/QueryDetected.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace BeyondCode\QueryDetector\Events;
4+
5+
use Illuminate\Queue\SerializesModels;
6+
use Illuminate\Support\Collection;
7+
8+
class QueryDetected {
9+
use SerializesModels;
10+
11+
/** @var Collection */
12+
protected $queries;
13+
14+
public function __construct(Collection $queries)
15+
{
16+
$this->queries = $queries;
17+
}
18+
19+
/**
20+
* @return Collection
21+
*/
22+
public function getQueries()
23+
{
24+
return $this->queries;
25+
}
26+
}

src/QueryDetector.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Eloquent\Builder;
88
use Symfony\Component\HttpFoundation\Response;
99
use Illuminate\Database\Eloquent\Relations\Relation;
10+
use BeyondCode\QueryDetector\Events\QueryDetected;
1011

1112
class QueryDetector
1213
{
@@ -167,7 +168,13 @@ public function getDetectedQueries(): Collection
167168
}
168169
}
169170

170-
return $queries->where('count', '>', config('querydetector.threshold', 1))->values();
171+
$queries = $queries->where('count', '>', config('querydetector.threshold', 1))->values();
172+
173+
if ($queries->isNotEmpty()) {
174+
event(new QueryDetected($queries));
175+
}
176+
177+
return $queries;
171178
}
172179

173180
protected function applyOutput(Response $response)

tests/QueryDetectorTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace BeyondCode\QueryDetector\Tests;
44

55
use Route;
6+
use Illuminate\Support\Facades\Event;
67
use BeyondCode\QueryDetector\QueryDetector;
8+
use BeyondCode\QueryDetector\Events\QueryDetected;
79
use BeyondCode\QueryDetector\Tests\Models\Post;
810
use BeyondCode\QueryDetector\Tests\Models\Author;
911
use BeyondCode\QueryDetector\Tests\Models\Comment;
@@ -224,4 +226,40 @@ public function it_ignores_redirects()
224226

225227
$this->assertCount(1, $queries);
226228
}
229+
230+
/** @test */
231+
public function it_fires_an_event_if_detects_n1_query()
232+
{
233+
Event::fake();
234+
235+
Route::get('/', function (){
236+
$authors = Author::all();
237+
238+
foreach ($authors as $author) {
239+
$author->profile;
240+
}
241+
});
242+
243+
$this->get('/');
244+
245+
Event::assertDispatched(QueryDetected::class);
246+
}
247+
248+
/** @test */
249+
public function it_does_not_fire_an_event_if_there_is_no_n1_query()
250+
{
251+
Event::fake();
252+
253+
Route::get('/', function (){
254+
$authors = Author::with('profile')->get();
255+
256+
foreach ($authors as $author) {
257+
$author->profile;
258+
}
259+
});
260+
261+
$this->get('/');
262+
263+
Event::assertNotDispatched(QueryDetected::class);
264+
}
227265
}

0 commit comments

Comments
 (0)