1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vormkracht10\SlowQuery; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Events\QueryExecuted; |
6
|
|
|
use Illuminate\Support\Facades\Config; |
7
|
|
|
use Illuminate\Support\Facades\Notification; |
8
|
|
|
use Illuminate\Support\ServiceProvider; |
9
|
|
|
use Vormkracht10\SlowQuery\Events\QueryExecutedSlowly; |
10
|
|
|
use Vormkracht10\SlowQuery\Notifications\SlowQueryDetected; |
11
|
|
|
use Vormkracht10\SlowQuery\Query; |
12
|
|
|
|
13
|
|
|
class SlowQueryServiceProvider extends ServiceProvider |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Bootstrap the application services. |
17
|
|
|
*/ |
18
|
|
|
public function boot() |
19
|
|
|
{ |
20
|
|
|
$this->setupEvents($this->app->events); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function register() |
24
|
|
|
{ |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function setupEvents($events) |
28
|
|
|
{ |
29
|
|
|
$events->listen(QueryExecuted::class, function (QueryExecuted $query) { |
30
|
|
|
$query = $this->makeQuery($query->sql, $query->bindings, $query->time, $query->connectionName); |
31
|
|
|
|
32
|
|
|
if ($this->slowQueryCheck($query)) { |
33
|
|
|
event(QueryExecutedSlowly($query)); |
34
|
|
|
} |
35
|
|
|
}); |
36
|
|
|
|
37
|
|
|
$events->listen(QueryExecutedSlowly::class, function (QueryExecutedSlowly $event) { |
38
|
|
|
Notification::route('discord', '680703864172707841') |
39
|
|
|
->notify(new SlowQueryDetected($event->query)); |
40
|
|
|
}); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function makeQuery($sql, $bindings, $time, $connectionName) |
44
|
|
|
{ |
45
|
|
|
return new Query($sql, $bindings, $time, $connectionName); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function slowQueryCheck(Query $query) |
49
|
|
|
{ |
50
|
|
|
return $query->time > Config::get('slow-query.slow_query_treshold_in_ms'); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: