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
|
|
|
|
12
|
|
|
class SlowQueryServiceProvider extends ServiceProvider |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Bootstrap the application services. |
16
|
|
|
*/ |
17
|
|
|
public function boot() |
18
|
|
|
{ |
19
|
|
|
$this->publishes([ |
20
|
|
|
__DIR__ . '/../config/slow-query.php' => config_path('slow-query.php'), |
21
|
|
|
], 'config'); |
22
|
|
|
|
23
|
|
|
$this->setupEvents($this->app->events); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function register() |
27
|
|
|
{ |
28
|
|
|
$this->mergeConfigFrom(__DIR__ . '/../config/slow-query.php', 'slow-query'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function setupEvents($events) |
32
|
|
|
{ |
33
|
|
|
$events->listen(QueryExecuted::class, function (QueryExecuted $query) { |
34
|
|
|
if ($this->slowQueryCheck($query)) { |
35
|
|
|
event(new QueryExecutedSlowly($query)); |
36
|
|
|
} |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
$events->listen(QueryExecutedSlowly::class, function (QueryExecutedSlowly $event) { |
40
|
|
|
$provider = Config::get('slow-query.notifications.default'); |
41
|
|
|
$channel = Config::get('slow-query.notifications.' . $provider . '.channel'); |
42
|
|
|
|
43
|
|
|
Notification::route($provider, $channel) |
44
|
|
|
->notify(new SlowQueryDetected($event->query)); |
45
|
|
|
}); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function slowQueryCheck(QueryExecuted $query) |
49
|
|
|
{ |
50
|
|
|
return $query->time >= Config::get('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: