clock-work
This commit is contained in:
53
vendor/beyondcode/laravel-query-detector/src/Outputs/Alert.php
vendored
Normal file
53
vendor/beyondcode/laravel-query-detector/src/Outputs/Alert.php
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace BeyondCode\QueryDetector\Outputs;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class Alert implements Output
|
||||
{
|
||||
public function boot()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function output(Collection $detectedQueries, Response $response)
|
||||
{
|
||||
if (stripos($response->headers->get('Content-Type'), 'text/html') !== 0 || $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 .= "alert('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(\'".addslashes($detectedQuery['relation'])."\')\" to eager-load this relation.";
|
||||
$output .= "\\n";
|
||||
}
|
||||
$output .= "')";
|
||||
$output .= '</script>';
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
19
vendor/beyondcode/laravel-query-detector/src/Outputs/Clockwork.php
vendored
Normal file
19
vendor/beyondcode/laravel-query-detector/src/Outputs/Clockwork.php
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace BeyondCode\QueryDetector\Outputs;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class Clockwork implements Output
|
||||
{
|
||||
public function boot()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function output(Collection $detectedQueries, Response $response)
|
||||
{
|
||||
clock()->warning("{$detectedQueries->count()} N+1 queries detected:", $detectedQueries->toArray());
|
||||
}
|
||||
}
|
||||
62
vendor/beyondcode/laravel-query-detector/src/Outputs/Console.php
vendored
Normal file
62
vendor/beyondcode/laravel-query-detector/src/Outputs/Console.php
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace BeyondCode\QueryDetector\Outputs;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class Console implements Output
|
||||
{
|
||||
public function boot()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function output(Collection $detectedQueries, Response $response)
|
||||
{
|
||||
if (stripos($response->headers->get('Content-Type'), 'text/html') !== 0 || $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\\n";
|
||||
$output .= "Model: ".addslashes($detectedQuery['model'])."\\n";
|
||||
$output .= "Relation: ".$detectedQuery['relation']."\\n";
|
||||
$output .= "Num-Called: ".$detectedQuery['count']."\\n";
|
||||
$output .= "\\n";
|
||||
$output .= 'Call-Stack:\\n';
|
||||
|
||||
foreach ($detectedQuery['sources'] as $source) {
|
||||
$output .= "#$source->index $source->name:$source->line\\n";
|
||||
}
|
||||
}
|
||||
$output .= "')";
|
||||
$output .= '</script>';
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
34
vendor/beyondcode/laravel-query-detector/src/Outputs/Debugbar.php
vendored
Normal file
34
vendor/beyondcode/laravel-query-detector/src/Outputs/Debugbar.php
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace BeyondCode\QueryDetector\Outputs;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
use Barryvdh\Debugbar\Facade as LaravelDebugbar;
|
||||
use DebugBar\DataCollector\MessagesCollector;
|
||||
|
||||
class Debugbar implements Output
|
||||
{
|
||||
protected $collector;
|
||||
|
||||
public function boot()
|
||||
{
|
||||
$this->collector = new MessagesCollector('N+1 Queries');
|
||||
|
||||
if (!LaravelDebugbar::hasCollector($this->collector->getName())) {
|
||||
LaravelDebugbar::addCollector($this->collector);
|
||||
}
|
||||
}
|
||||
|
||||
public function output(Collection $detectedQueries, Response $response)
|
||||
{
|
||||
foreach ($detectedQueries as $detectedQuery) {
|
||||
$this->collector->addMessage(sprintf('Model: %s => Relation: %s - You should add `with(%s)` to eager-load this relation.',
|
||||
$detectedQuery['model'],
|
||||
$detectedQuery['relation'],
|
||||
$detectedQuery['relation']
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
27
vendor/beyondcode/laravel-query-detector/src/Outputs/Json.php
vendored
Normal file
27
vendor/beyondcode/laravel-query-detector/src/Outputs/Json.php
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace BeyondCode\QueryDetector\Outputs;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class Json implements Output
|
||||
{
|
||||
public function boot()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function output(Collection $detectedQueries, Response $response)
|
||||
{
|
||||
if ($response instanceof JsonResponse) {
|
||||
$data = $response->getData(true);
|
||||
if (! is_array($data)){
|
||||
$data = [ $data ];
|
||||
}
|
||||
|
||||
$data['warning_queries'] = $detectedQueries;
|
||||
$response->setData($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
41
vendor/beyondcode/laravel-query-detector/src/Outputs/Log.php
vendored
Normal file
41
vendor/beyondcode/laravel-query-detector/src/Outputs/Log.php
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace BeyondCode\QueryDetector\Outputs;
|
||||
|
||||
use Illuminate\Support\Facades\Log as LaravelLog;
|
||||
use Illuminate\Support\Collection;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class Log implements Output
|
||||
{
|
||||
public function boot()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function output(Collection $detectedQueries, Response $response)
|
||||
{
|
||||
$this->log('Detected N+1 Query');
|
||||
|
||||
foreach ($detectedQueries as $detectedQuery) {
|
||||
$logOutput = 'Model: '.$detectedQuery['model'] . PHP_EOL;
|
||||
|
||||
$logOutput .= 'Relation: '.$detectedQuery['relation'] . PHP_EOL;
|
||||
|
||||
$logOutput .= 'Num-Called: '.$detectedQuery['count'] . PHP_EOL;
|
||||
|
||||
$logOutput .= 'Call-Stack:' . PHP_EOL;
|
||||
|
||||
foreach ($detectedQuery['sources'] as $source) {
|
||||
$logOutput .= '#'.$source->index.' '.$source->name.':'.$source->line . PHP_EOL;
|
||||
}
|
||||
|
||||
$this->log($logOutput);
|
||||
}
|
||||
}
|
||||
|
||||
private function log(string $message)
|
||||
{
|
||||
LaravelLog::channel(config('querydetector.log_channel'))->info($message);
|
||||
}
|
||||
}
|
||||
13
vendor/beyondcode/laravel-query-detector/src/Outputs/Output.php
vendored
Normal file
13
vendor/beyondcode/laravel-query-detector/src/Outputs/Output.php
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace BeyondCode\QueryDetector\Outputs;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
interface Output
|
||||
{
|
||||
public function boot();
|
||||
|
||||
public function output(Collection $detectedQueries, Response $response);
|
||||
}
|
||||
Reference in New Issue
Block a user