clock-work
This commit is contained in:
4
vendor/beyondcode/laravel-query-detector/.styleci.yml
vendored
Normal file
4
vendor/beyondcode/laravel-query-detector/.styleci.yml
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
preset: laravel
|
||||
|
||||
disabled:
|
||||
- single_class_element_per_statement
|
7
vendor/beyondcode/laravel-query-detector/CHANGELOG.md
vendored
Normal file
7
vendor/beyondcode/laravel-query-detector/CHANGELOG.md
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to `laravel-query-detector` will be documented in this file
|
||||
|
||||
## 1.0.0 - 201X-XX-XX
|
||||
|
||||
- initial release
|
55
vendor/beyondcode/laravel-query-detector/CONTRIBUTING.md
vendored
Normal file
55
vendor/beyondcode/laravel-query-detector/CONTRIBUTING.md
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
# Contributing
|
||||
|
||||
Contributions are **welcome** and will be fully **credited**.
|
||||
|
||||
Please read and understand the contribution guide before creating an issue or pull request.
|
||||
|
||||
## Etiquette
|
||||
|
||||
This project is open source, and as such, the maintainers give their free time to build and maintain the source code
|
||||
held within. They make the code freely available in the hope that it will be of use to other developers. It would be
|
||||
extremely unfair for them to suffer abuse or anger for their hard work.
|
||||
|
||||
Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the
|
||||
world that developers are civilized and selfless people.
|
||||
|
||||
It's the duty of the maintainer to ensure that all submissions to the project are of sufficient
|
||||
quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used.
|
||||
|
||||
## Viability
|
||||
|
||||
When requesting or submitting new features, first consider whether it might be useful to others. Open
|
||||
source projects are used by many developers, who may have entirely different needs to your own. Think about
|
||||
whether or not your feature is likely to be used by other users of the project.
|
||||
|
||||
## Procedure
|
||||
|
||||
Before filing an issue:
|
||||
|
||||
- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident.
|
||||
- Check to make sure your feature suggestion isn't already present within the project.
|
||||
- Check the pull requests tab to ensure that the bug doesn't have a fix in progress.
|
||||
- Check the pull requests tab to ensure that the feature isn't already in progress.
|
||||
|
||||
Before submitting a pull request:
|
||||
|
||||
- Check the codebase to ensure that your feature doesn't already exist.
|
||||
- Check the pull requests to ensure that another person hasn't already submitted the feature or fix.
|
||||
|
||||
## Requirements
|
||||
|
||||
If the project maintainer has any additional requirements, you will find them listed here.
|
||||
|
||||
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer).
|
||||
|
||||
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
|
||||
|
||||
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
|
||||
|
||||
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option.
|
||||
|
||||
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
|
||||
|
||||
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
|
||||
|
||||
**Happy coding**!
|
21
vendor/beyondcode/laravel-query-detector/LICENSE.md
vendored
Normal file
21
vendor/beyondcode/laravel-query-detector/LICENSE.md
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) Beyond Code GmbH <hello@beyondco.de>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
53
vendor/beyondcode/laravel-query-detector/README.md
vendored
Normal file
53
vendor/beyondcode/laravel-query-detector/README.md
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
# Laravel N+1 Query Detector
|
||||
|
||||
[](https://packagist.org/packages/beyondcode/laravel-query-detector)
|
||||
[](https://travis-ci.org/beyondcode/laravel-query-detector)
|
||||
[](https://scrutinizer-ci.com/g/beyondcode/laravel-query-detector)
|
||||
[](https://packagist.org/packages/beyondcode/laravel-query-detector)
|
||||
|
||||
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).
|
||||
|
||||

|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
You can install the package via composer:
|
||||
|
||||
```bash
|
||||
composer require beyondcode/laravel-query-detector --dev
|
||||
```
|
||||
|
||||
The package will automatically register itself.
|
||||
|
||||
## Documentation
|
||||
|
||||
You can find the documentation on our [website](http://beyondco.de/docs/laravel-query-detector).
|
||||
|
||||
|
||||
### Testing
|
||||
|
||||
``` bash
|
||||
composer test
|
||||
```
|
||||
|
||||
### Changelog
|
||||
|
||||
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
|
||||
|
||||
## Contributing
|
||||
|
||||
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
|
||||
|
||||
### Security
|
||||
|
||||
If you discover any security related issues, please email marcel@beyondco.de instead of using the issue tracker.
|
||||
|
||||
## Credits
|
||||
|
||||
- [Marcel Pociot](https://github.com/mpociot)
|
||||
- [All Contributors](../../contributors)
|
||||
|
||||
## License
|
||||
|
||||
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
|
51
vendor/beyondcode/laravel-query-detector/composer.json
vendored
Normal file
51
vendor/beyondcode/laravel-query-detector/composer.json
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
{
|
||||
"name": "beyondcode/laravel-query-detector",
|
||||
"description": "Laravel N+1 Query Detector",
|
||||
"keywords": [
|
||||
"beyondcode",
|
||||
"laravel-query-detector"
|
||||
],
|
||||
"homepage": "https://github.com/beyondcode/laravel-query-detector",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Marcel Pociot",
|
||||
"email": "marcel@beyondco.de",
|
||||
"homepage": "https://beyondcode.de",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^7.1 || ^8.0",
|
||||
"illuminate/support": "^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0|^10.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"laravel/legacy-factories": "^1.0",
|
||||
"orchestra/testbench": "^3.0 || ^4.0 || ^5.0 || ^6.0|^8.0",
|
||||
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"BeyondCode\\QueryDetector\\": "src"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"BeyondCode\\QueryDetector\\Tests\\": "tests"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"test": "vendor/bin/phpunit",
|
||||
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
|
||||
},
|
||||
"config": {
|
||||
"sort-packages": true
|
||||
},
|
||||
"extra": {
|
||||
"laravel": {
|
||||
"providers": [
|
||||
"BeyondCode\\QueryDetector\\QueryDetectorServiceProvider"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
69
vendor/beyondcode/laravel-query-detector/config/config.php
vendored
Normal file
69
vendor/beyondcode/laravel-query-detector/config/config.php
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
/*
|
||||
* Enable or disable the query detection.
|
||||
* If this is set to "null", the app.debug config value will be used.
|
||||
*/
|
||||
'enabled' => env('QUERY_DETECTOR_ENABLED', null),
|
||||
|
||||
/*
|
||||
* Threshold level for the N+1 query detection. If a relation query will be
|
||||
* executed more then this amount, the detector will notify you about it.
|
||||
*/
|
||||
'threshold' => (int) env('QUERY_DETECTOR_THRESHOLD', 1),
|
||||
|
||||
/*
|
||||
* Here you can whitelist model relations.
|
||||
*
|
||||
* Right now, you need to define the model relation both as the class name and the attribute name on the model.
|
||||
* So if an "Author" model would have a "posts" relation that points to a "Post" class, you need to add both
|
||||
* the "posts" attribute and the "Post::class", since the relation can get resolved in multiple ways.
|
||||
*/
|
||||
'except' => [
|
||||
//Author::class => [
|
||||
// Post::class,
|
||||
// 'posts',
|
||||
//]
|
||||
],
|
||||
|
||||
/*
|
||||
* Here you can set a specific log channel to write to
|
||||
* in case you are trying to isolate queries or have a lot
|
||||
* going on in the laravel.log. Defaults to laravel.log though.
|
||||
*/
|
||||
'log_channel' => env('QUERY_DETECTOR_LOG_CHANNEL', 'daily'),
|
||||
|
||||
/*
|
||||
* Define the output format that you want to use. Multiple classes are supported.
|
||||
* Available options are:
|
||||
*
|
||||
* Alert:
|
||||
* Displays an alert on the website
|
||||
* \BeyondCode\QueryDetector\Outputs\Alert::class
|
||||
*
|
||||
* Console:
|
||||
* Writes the N+1 queries into your browsers console log
|
||||
* \BeyondCode\QueryDetector\Outputs\Console::class
|
||||
*
|
||||
* Clockwork: (make sure you have the itsgoingd/clockwork package installed)
|
||||
* Writes the N+1 queries warnings to Clockwork log
|
||||
* \BeyondCode\QueryDetector\Outputs\Clockwork::class
|
||||
*
|
||||
* Debugbar: (make sure you have the barryvdh/laravel-debugbar package installed)
|
||||
* Writes the N+1 queries into a custom messages collector of Debugbar
|
||||
* \BeyondCode\QueryDetector\Outputs\Debugbar::class
|
||||
*
|
||||
* JSON:
|
||||
* Writes the N+1 queries into the response body of your JSON responses
|
||||
* \BeyondCode\QueryDetector\Outputs\Json::class
|
||||
*
|
||||
* Log:
|
||||
* Writes the N+1 queries into the Laravel.log file
|
||||
* \BeyondCode\QueryDetector\Outputs\Log::class
|
||||
*/
|
||||
'output' => [
|
||||
\BeyondCode\QueryDetector\Outputs\Alert::class,
|
||||
\BeyondCode\QueryDetector\Outputs\Log::class,
|
||||
]
|
||||
];
|
4
vendor/beyondcode/laravel-query-detector/docs/_index.md
vendored
Normal file
4
vendor/beyondcode/laravel-query-detector/docs/_index.md
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
---
|
||||
packageName: Laravel Query Detector
|
||||
githubUrl: https://github.com/beyondcode/laravel-query-detector
|
||||
---
|
17
vendor/beyondcode/laravel-query-detector/docs/installation.md
vendored
Normal file
17
vendor/beyondcode/laravel-query-detector/docs/installation.md
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
---
|
||||
title: Installation
|
||||
order: 1
|
||||
---
|
||||
# Laravel N+1 Query Detector
|
||||
|
||||
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).
|
||||
|
||||
# Installation
|
||||
|
||||
You can install the package via composer:
|
||||
|
||||
```
|
||||
composer require beyondcode/laravel-query-detector --dev
|
||||
```
|
||||
|
||||
The package will automatically register itself.
|
92
vendor/beyondcode/laravel-query-detector/docs/usage.md
vendored
Normal file
92
vendor/beyondcode/laravel-query-detector/docs/usage.md
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
---
|
||||
title: Usage
|
||||
order: 2
|
||||
---
|
||||
|
||||
## Usage
|
||||
|
||||
If you run your application in the `debug` mode, the query monitor will be automatically active. So there is nothing you have to do.
|
||||
|
||||
By default, this package will display an `alert()` message to notify you about an N+1 query found in the current request.
|
||||
|
||||
If you rather want this information to be written to your `laravel.log` file, written to your browser's console log as a warning or listed in a new tab for the [Laravel Debugbar (barryvdh/laravel-debugbar)](https://github.com/barryvdh/laravel-debugbar), you can publish the configuration and change the output behaviour (see example below).
|
||||
|
||||
You can publish the package's configuration using this command:
|
||||
|
||||
```bash
|
||||
php artisan vendor:publish --provider="BeyondCode\QueryDetector\QueryDetectorServiceProvider"
|
||||
```
|
||||
|
||||
This will add the `querydetector.php` file in your config directory with the following contents:
|
||||
|
||||
```php
|
||||
return [
|
||||
/*
|
||||
* Enable or disable the query detection.
|
||||
* If this is set to "null", the app.debug config value will be used.
|
||||
*/
|
||||
'enabled' => env('QUERY_DETECTOR_ENABLED', null),
|
||||
|
||||
/*
|
||||
* Threshold level for the N+1 query detection. If a relation query will be
|
||||
* executed more then this amount, the detector will notify you about it.
|
||||
*/
|
||||
'threshold' => (int) env('QUERY_DETECTOR_THRESHOLD', 1),
|
||||
|
||||
/*
|
||||
* Here you can whitelist model relations.
|
||||
*
|
||||
* Right now, you need to define the model relation both as the class name and the attribute name on the model.
|
||||
* So if an "Author" model would have a "posts" relation that points to a "Post" class, you need to add both
|
||||
* the "posts" attribute and the "Post::class", since the relation can get resolved in multiple ways.
|
||||
*/
|
||||
'except' => [
|
||||
//Author::class => [
|
||||
// Post::class,
|
||||
// 'posts',
|
||||
//]
|
||||
],
|
||||
|
||||
/*
|
||||
* Define the output format that you want to use. Multiple classes are supported.
|
||||
* Available options are:
|
||||
*
|
||||
* Alert:
|
||||
* Displays an alert on the website
|
||||
* \BeyondCode\QueryDetector\Outputs\Alert::class
|
||||
*
|
||||
* Console:
|
||||
* Writes the N+1 queries into your browsers console log
|
||||
* \BeyondCode\QueryDetector\Outputs\Console::class
|
||||
*
|
||||
* Clockwork: (make sure you have the itsgoingd/clockwork package installed)
|
||||
* Writes the N+1 queries warnings to Clockwork log
|
||||
* \BeyondCode\QueryDetector\Outputs\Clockwork::class
|
||||
*
|
||||
* Debugbar: (make sure you have the barryvdh/laravel-debugbar package installed)
|
||||
* Writes the N+1 queries into a custom messages collector of Debugbar
|
||||
* \BeyondCode\QueryDetector\Outputs\Debugbar::class
|
||||
*
|
||||
* JSON:
|
||||
* Writes the N+1 queries into the response body of your JSON responses
|
||||
* \BeyondCode\QueryDetector\Outputs\Json::class
|
||||
*
|
||||
* Log:
|
||||
* Writes the N+1 queries into the Laravel.log file
|
||||
* \BeyondCode\QueryDetector\Outputs\Log::class
|
||||
*/
|
||||
'output' => [
|
||||
\BeyondCode\QueryDetector\Outputs\Log::class,
|
||||
\BeyondCode\QueryDetector\Outputs\Alert::class,
|
||||
]
|
||||
|
||||
];
|
||||
```
|
||||
|
||||
If you use **Lumen**, you need to copy the config file manually and register the Lumen Service Provider in `bootstrap/app.php` file
|
||||
|
||||
```php
|
||||
$app->register(\BeyondCode\QueryDetector\LumenQueryDetectorServiceProvider::class);
|
||||
```
|
||||
|
||||
If you need additional logic to run when the package detects unoptimized queries, you can listen to the `\BeyondCode\QueryDetector\Events\QueryDetected` event and write a listener to run your own handler. (e.g. send warning to Sentry/Bugsnag, send Slack notification, etc.)
|
26
vendor/beyondcode/laravel-query-detector/src/Events/QueryDetected.php
vendored
Normal file
26
vendor/beyondcode/laravel-query-detector/src/Events/QueryDetected.php
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace BeyondCode\QueryDetector\Events;
|
||||
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class QueryDetected {
|
||||
use SerializesModels;
|
||||
|
||||
/** @var Collection */
|
||||
protected $queries;
|
||||
|
||||
public function __construct(Collection $queries)
|
||||
{
|
||||
$this->queries = $queries;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getQueries()
|
||||
{
|
||||
return $this->queries;
|
||||
}
|
||||
}
|
21
vendor/beyondcode/laravel-query-detector/src/LumenQueryDetectorServiceProvider.php
vendored
Normal file
21
vendor/beyondcode/laravel-query-detector/src/LumenQueryDetectorServiceProvider.php
vendored
Normal file
@@ -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');
|
||||
}
|
||||
}
|
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);
|
||||
}
|
216
vendor/beyondcode/laravel-query-detector/src/QueryDetector.php
vendored
Executable file
216
vendor/beyondcode/laravel-query-detector/src/QueryDetector.php
vendored
Executable file
@@ -0,0 +1,216 @@
|
||||
<?php
|
||||
|
||||
namespace BeyondCode\QueryDetector;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||
use BeyondCode\QueryDetector\Events\QueryDetected;
|
||||
|
||||
class QueryDetector
|
||||
{
|
||||
/** @var Collection */
|
||||
private $queries;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->queries = Collection::make();
|
||||
}
|
||||
|
||||
public function boot()
|
||||
{
|
||||
DB::listen(function($query) {
|
||||
$backtrace = collect(debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 50));
|
||||
|
||||
$this->logQuery($query, $backtrace);
|
||||
});
|
||||
|
||||
foreach ($this->getOutputTypes() as $outputType) {
|
||||
app()->singleton($outputType);
|
||||
app($outputType)->boot();
|
||||
}
|
||||
}
|
||||
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
$configEnabled = value(config('querydetector.enabled'));
|
||||
|
||||
if ($configEnabled === null) {
|
||||
$configEnabled = config('app.debug');
|
||||
}
|
||||
|
||||
return $configEnabled;
|
||||
}
|
||||
|
||||
public function logQuery($query, Collection $backtrace)
|
||||
{
|
||||
$modelTrace = $backtrace->first(function ($trace) {
|
||||
return Arr::get($trace, 'object') instanceof Builder;
|
||||
});
|
||||
|
||||
// The query is coming from an Eloquent model
|
||||
if (! is_null($modelTrace)) {
|
||||
/*
|
||||
* Relations get resolved by either calling the "getRelationValue" method on the model,
|
||||
* or if the class itself is a Relation.
|
||||
*/
|
||||
$relation = $backtrace->first(function ($trace) {
|
||||
return Arr::get($trace, 'function') === 'getRelationValue' || Arr::get($trace, 'class') === Relation::class ;
|
||||
});
|
||||
|
||||
// We try to access a relation
|
||||
if (is_array($relation) && isset($relation['object'])) {
|
||||
if ($relation['class'] === Relation::class) {
|
||||
$model = get_class($relation['object']->getParent());
|
||||
$relationName = get_class($relation['object']->getRelated());
|
||||
$relatedModel = $relationName;
|
||||
} else {
|
||||
$model = get_class($relation['object']);
|
||||
$relationName = $relation['args'][0];
|
||||
$relatedModel = $relationName;
|
||||
}
|
||||
|
||||
$sources = $this->findSource($backtrace);
|
||||
|
||||
$key = md5($query->sql . $model . $relationName . $sources[0]->name . $sources[0]->line);
|
||||
|
||||
$count = Arr::get($this->queries, $key.'.count', 0);
|
||||
$time = Arr::get($this->queries, $key.'.time', 0);
|
||||
|
||||
$this->queries[$key] = [
|
||||
'count' => ++$count,
|
||||
'time' => $time + $query->time,
|
||||
'query' => $query->sql,
|
||||
'model' => $model,
|
||||
'relatedModel' => $relatedModel,
|
||||
'relation' => $relationName,
|
||||
'sources' => $sources
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function findSource($stack)
|
||||
{
|
||||
$sources = [];
|
||||
|
||||
foreach ($stack as $index => $trace) {
|
||||
$sources[] = $this->parseTrace($index, $trace);
|
||||
}
|
||||
|
||||
return array_values(array_filter($sources));
|
||||
}
|
||||
|
||||
public function parseTrace($index, array $trace)
|
||||
{
|
||||
$frame = (object) [
|
||||
'index' => $index,
|
||||
'name' => null,
|
||||
'line' => isset($trace['line']) ? $trace['line'] : '?',
|
||||
];
|
||||
|
||||
if (isset($trace['class']) &&
|
||||
isset($trace['file']) &&
|
||||
!$this->fileIsInExcludedPath($trace['file'])
|
||||
) {
|
||||
$frame->name = $this->normalizeFilename($trace['file']);
|
||||
|
||||
return $frame;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given file is to be excluded from analysis
|
||||
*
|
||||
* @param string $file
|
||||
* @return bool
|
||||
*/
|
||||
protected function fileIsInExcludedPath($file)
|
||||
{
|
||||
$excludedPaths = [
|
||||
'/vendor/laravel/framework/src/Illuminate/Database',
|
||||
'/vendor/laravel/framework/src/Illuminate/Events',
|
||||
];
|
||||
|
||||
$normalizedPath = str_replace('\\', '/', $file);
|
||||
|
||||
foreach ($excludedPaths as $excludedPath) {
|
||||
if (strpos($normalizedPath, $excludedPath) !== false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorten the path by removing the relative links and base dir
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
protected function normalizeFilename($path): string
|
||||
{
|
||||
if (file_exists($path)) {
|
||||
$path = realpath($path);
|
||||
}
|
||||
|
||||
return str_replace(base_path(), '', $path);
|
||||
}
|
||||
|
||||
public function getDetectedQueries(): Collection
|
||||
{
|
||||
$exceptions = config('querydetector.except', []);
|
||||
|
||||
$queries = $this->queries
|
||||
->values();
|
||||
|
||||
foreach ($exceptions as $parentModel => $relations) {
|
||||
foreach ($relations as $relation) {
|
||||
$queries = $queries->reject(function ($query) use ($relation, $parentModel) {
|
||||
return $query['model'] === $parentModel && $query['relatedModel'] === $relation;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
$queries = $queries->where('count', '>', config('querydetector.threshold', 1))->values();
|
||||
|
||||
if ($queries->isNotEmpty()) {
|
||||
event(new QueryDetected($queries));
|
||||
}
|
||||
|
||||
return $queries;
|
||||
}
|
||||
|
||||
protected function getOutputTypes()
|
||||
{
|
||||
$outputTypes = config('querydetector.output');
|
||||
|
||||
if (! is_array($outputTypes)) {
|
||||
$outputTypes = [$outputTypes];
|
||||
}
|
||||
|
||||
return $outputTypes;
|
||||
}
|
||||
|
||||
protected function applyOutput(Response $response)
|
||||
{
|
||||
foreach ($this->getOutputTypes() as $type) {
|
||||
app($type)->output($this->getDetectedQueries(), $response);
|
||||
}
|
||||
}
|
||||
|
||||
public function output($request, $response)
|
||||
{
|
||||
if ($this->getDetectedQueries()->isNotEmpty()) {
|
||||
$this->applyOutput($response);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
40
vendor/beyondcode/laravel-query-detector/src/QueryDetectorMiddleware.php
vendored
Normal file
40
vendor/beyondcode/laravel-query-detector/src/QueryDetectorMiddleware.php
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace BeyondCode\QueryDetector;
|
||||
|
||||
use Closure;
|
||||
|
||||
class QueryDetectorMiddleware
|
||||
{
|
||||
/** @var QueryDetector */
|
||||
private $detector;
|
||||
|
||||
public function __construct(QueryDetector $detector)
|
||||
{
|
||||
$this->detector = $detector;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
if (! $this->detector->isEnabled()) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
$this->detector->boot();
|
||||
|
||||
/** @var \Illuminate\Http\Response $response */
|
||||
$response = $next($request);
|
||||
|
||||
// Modify the response to add the Debugbar
|
||||
$this->detector->output($request, $response);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
46
vendor/beyondcode/laravel-query-detector/src/QueryDetectorServiceProvider.php
vendored
Normal file
46
vendor/beyondcode/laravel-query-detector/src/QueryDetectorServiceProvider.php
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace BeyondCode\QueryDetector;
|
||||
|
||||
use Illuminate\Contracts\Http\Kernel;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class QueryDetectorServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap the application services.
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
if ($this->app->runningInConsole()) {
|
||||
$this->publishes([
|
||||
__DIR__.'/../config/config.php' => config_path('querydetector.php'),
|
||||
], 'config');
|
||||
}
|
||||
|
||||
$this->registerMiddleware(QueryDetectorMiddleware::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the application services.
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->singleton(QueryDetector::class);
|
||||
|
||||
$this->app->alias(QueryDetector::class, 'querydetector');
|
||||
|
||||
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'querydetector');
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the middleware
|
||||
*
|
||||
* @param string $middleware
|
||||
*/
|
||||
protected function registerMiddleware($middleware)
|
||||
{
|
||||
$kernel = $this->app[Kernel::class];
|
||||
$kernel->pushMiddleware($middleware);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user