-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:beyondcode/laravel-query-detector
- Loading branch information
Showing
7 changed files
with
119 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ composer.lock | |
docs | ||
vendor | ||
coverage | ||
.idea | ||
.idea | ||
nbproject |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
namespace BeyondCode\QueryDetector; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class LumenQueryDetectorServiceProvider extends ServiceProvider | ||
{ | ||
|
||
public function register() | ||
{ | ||
$this->app->configure('querydetector'); | ||
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'querydetector'); | ||
|
||
$this->app->middleware([ | ||
QueryDetectorMiddleware::class | ||
]); | ||
|
||
$this->app->singleton(QueryDetector::class); | ||
$this->app->alias(QueryDetector::class, 'querydetector'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
namespace BeyondCode\QueryDetector\Outputs; | ||
use Illuminate\Support\Collection; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class Console implements Output | ||
{ | ||
public function output(Collection $detectedQueries, Response $response) | ||
{ | ||
if ($response->isRedirection()) { | ||
return; | ||
} | ||
$content = $response->getContent(); | ||
$outputContent = $this->getOutputContent($detectedQueries); | ||
$pos = strripos($content, '</body>'); | ||
if (false !== $pos) { | ||
$content = substr($content, 0, $pos) . $outputContent . substr($content, $pos); | ||
} else { | ||
$content = $content . $outputContent; | ||
} | ||
// Update the new content and reset the content length | ||
$response->setContent($content); | ||
$response->headers->remove('Content-Length'); | ||
} | ||
protected function getOutputContent(Collection $detectedQueries) | ||
{ | ||
$output = '<script type="text/javascript">'; | ||
$output .= "console.warn('Found the following N+1 queries in this request:\\n\\n"; | ||
foreach ($detectedQueries as $detectedQuery) { | ||
$output .= "Model: ".addslashes($detectedQuery['model']). " => Relation: ".addslashes($detectedQuery['relation']); | ||
$output .= " - You should add \"with(\'".$detectedQuery['relation']."\')\" to eager-load this relation."; | ||
$output .= "\\n"; | ||
} | ||
$output .= "')"; | ||
$output .= '</script>'; | ||
return $output; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace BeyondCode\QueryDetector\Outputs; | ||
|
||
use Illuminate\Support\Collection; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
use Debugbar as LaravelDebugbar; | ||
use DebugBar\DataCollector\MessagesCollector; | ||
|
||
class Debugbar implements Output | ||
{ | ||
public function output(Collection $detectedQueries, Response $response) | ||
{ | ||
$collector = new MessagesCollector('N+1 Queries'); | ||
|
||
foreach ($detectedQueries as $detectedQuery) { | ||
$collector->addMessage(sprintf('Model: %s => Relation: %s - You should add with(%s) to eager-load this relation.', | ||
$detectedQuery['model'], | ||
$detectedQuery['relation'], | ||
$detectedQuery['relation'] | ||
)); | ||
} | ||
|
||
LaravelDebugbar::addCollector($collector); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters