Skip to content

Commit 2c04eb4

Browse files
committed
Better config + support for multiple outputters
1 parent 8b7e0e6 commit 2c04eb4

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

README.md

Lines changed: 6 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,7 +75,9 @@ 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
```

config/config.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
return [
44
/*
55
* Enable or disable the query detection.
6-
* If this is set to "null", the app.debug config value will be used.
76
*/
8-
'enabled' => env('QUERY_DETECTOR_ENABLED', null),
7+
'enabled' => env('QUERY_DETECTOR_ENABLED', false),
98

109
/*
1110
* Here you can whitelist model relations.
@@ -35,4 +34,4 @@
3534
*/
3635
'output' => \BeyondCode\QueryDetector\Outputs\Alert::class,
3736

38-
];
37+
];

src/QueryDetector.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,9 @@ public function boot()
2929

3030
public function isEnabled(): bool
3131
{
32-
$configEnabled = value(config('querydetector.enabled'));
32+
$config = config('querydetector.enabled');
3333

34-
if ($configEnabled === null) {
35-
$configEnabled = config('app.debug');
36-
}
37-
38-
return $configEnabled;
34+
return is_null($config) ? false : $config;
3935
}
4036

4137
public function logQuery($query, Collection $backtrace)
@@ -172,8 +168,15 @@ public function getDetectedQueries(): Collection
172168

173169
protected function applyOutput(Response $response)
174170
{
175-
$outputType = app(config('querydetector.output'));
176-
$outputType->output($this->getDetectedQueries(), $response);
171+
$outputTypes = app(config('querydetector.output'));
172+
173+
if (! is_array($outputTypes)) {
174+
$types = [$outputTypes];
175+
}
176+
177+
foreach ($outputTypes as $type) {
178+
$type->output($this->getDetectedQueries(), $response);
179+
}
177180
}
178181

179182
public function output($request, $response)

0 commit comments

Comments
 (0)