Skip to content

Commit

Permalink
Support Laravel 9
Browse files Browse the repository at this point in the history
  • Loading branch information
onlime committed Feb 10, 2022
1 parent c8dd058 commit 3035fa0
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 127 deletions.
12 changes: 7 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# CHANGELOG

## [v0.9.2 (Unreleased)](https://github.com/onlime/laravel-sql-reporter/compare/v0.9.1...main)
## [1.0.x (Unreleased)](https://github.com/onlime/laravel-sql-reporter/compare/v1.0.0...main)

## [v0.9.1 (2021-06-03)](https://github.com/onlime/laravel-sql-reporter/releases/tag/v0.9.1)
## [v1.0.0 (2022-02-10)](https://github.com/onlime/laravel-sql-reporter/releases/tag/compare/v0.9.1...v1.0.0)

- Support Laravel 9
- Added some function return types and cleaned up phpdoc comments.

## [v0.9.1 (2021-06-03)](https://github.com/onlime/laravel-sql-reporter/releases/tag/compare/v0.9...v0.9.1)

### Added
- Added new config param `queries.exclude_pattern` (env var `SQL_REPORTER_QUERIES_EXCLUDE_PATTERN`) to narrow down queries to be logged without bloating include pattern regex.
- Added unit tests for `Writer`, testing query include/exclude patterns and min exec time.

### Changed
- Renamed `SQL_REPORTER_QUERIES_PATTERN` env var to `SQL_REPORTER_QUERIES_INCLUDE_PATTERN`
- Renamed methods in `Writer` for clarity.
- Improved testability of `Writer::writeQuery()` by returning true if query was written to log.
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
],
"require": {
"php": "^8.0",
"illuminate/support": "8.*",
"nesbot/carbon": "^2.0",
"illuminate/filesystem": "8.*",
"illuminate/container": "8.*"
"illuminate/support": "^8.0|^9.0",
"illuminate/filesystem": "^8.0|^9.0",
"illuminate/container": "^8.0|^9.0",
"nesbot/carbon": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"mockery/mockery": "^1.0",
"laravel/framework": "8.*"
"laravel/framework": "^8.0|^9.0"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 2 additions & 2 deletions src/Concerns/ReplacesBindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait ReplacesBindings
*
* @return string
*/
protected function replaceBindings($sql, array $bindings)
protected function replaceBindings(string $sql, array $bindings): string
{
$generalRegex = $this->getRegex();

Expand Down Expand Up @@ -53,7 +53,7 @@ protected function value($value)
*
* @return string
*/
protected function getNamedParameterRegex($name)
protected function getNamedParameterRegex($name): string
{
if (mb_substr($name, 0, 1) == ':') {
$name = mb_substr($name, 1);
Expand Down
38 changes: 7 additions & 31 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ class Config
{
/**
* Config constructor.
*
* @param ConfigRepository $repository
*/
public function __construct(protected ConfigRepository $repository) {}
public function __construct(
protected ConfigRepository $repository
) {}

/**
* Get directory where log files should be saved.
*
* @return string
*/
public function logDirectory(): string
{
Expand All @@ -25,28 +23,22 @@ public function logDirectory(): string

/**
* Whether query execution time should be converted to seconds.
*
* @return bool
*/
public function useSeconds(): bool
{
return (bool) $this->repository->get('sql-reporter.general.use_seconds');
return (bool)$this->repository->get('sql-reporter.general.use_seconds');
}

/**
* Get suffix for console logs.
*
* @return string
*/
public function consoleSuffix(): string
{
return (string) $this->repository->get('sql-reporter.general.console_log_suffix');
return (string)$this->repository->get('sql-reporter.general.console_log_suffix');
}

/**
* Get file extension for logs.
*
* @return string
*/
public function fileExtension(): string
{
Expand All @@ -55,18 +47,14 @@ public function fileExtension(): string

/**
* Whether all queries should be logged.
*
* @return bool
*/
public function queriesEnabled(): bool
{
return (bool) $this->repository->get('sql-reporter.queries.enabled');
return (bool)$this->repository->get('sql-reporter.queries.enabled');
}

/**
* Minimum execution time (in milliseconds) for queries to be logged.
*
* @return float
*/
public function queriesMinExecTime(): float
{
Expand All @@ -75,18 +63,14 @@ public function queriesMinExecTime(): float

/**
* Whether SQL log should be overridden for each request.
*
* @return bool
*/
public function queriesOverrideLog(): bool
{
return (bool) $this->repository->get('sql-reporter.queries.override_log');
return (bool)$this->repository->get('sql-reporter.queries.override_log');
}

/**
* Get include pattern for queries.
*
* @return string
*/
public function queriesIncludePattern(): string
{
Expand All @@ -95,8 +79,6 @@ public function queriesIncludePattern(): string

/**
* Get exclude pattern for queries.
*
* @return string
*/
public function queriesExcludePattern(): string
{
Expand All @@ -105,8 +87,6 @@ public function queriesExcludePattern(): string

/**
* Get file name (without extension) for all queries.
*
* @return string
*/
public function queriesFileName(): string
{
Expand All @@ -115,8 +95,6 @@ public function queriesFileName(): string

/**
* Get header fields that should be printed in header before query loglines.
*
* @return array
*/
public function headerFields(): array
{
Expand All @@ -125,8 +103,6 @@ public function headerFields(): array

/**
* Get query format that should be used to save query.
*
* @return string
*/
public function entryFormat(): string
{
Expand Down
12 changes: 3 additions & 9 deletions src/FileName.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
/**
* Create file name for query log.
*/
public function getLogfile()
public function getLogfile(): string
{
return
$this->parseFileName($this->config->queriesFileName()) .
Expand All @@ -31,22 +31,16 @@ public function getLogfile()

/**
* Get file suffix.
*
* @return string
*/
protected function suffix()
protected function suffix(): string
{
return $this->app->runningInConsole() ? $this->config->consoleSuffix() : '';
}

/**
* Parse file name to include date in it.
*
* @param string $fileName
*
* @return string
*/
protected function parseFileName($fileName)
protected function parseFileName(string $fileName): string
{
return preg_replace_callback('#(\[.*\])#U', function ($matches) {
$format = str_replace(['[',']'], [], $matches[1]);
Expand Down
36 changes: 8 additions & 28 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Carbon\Carbon;
use Illuminate\Container\Container;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;
Expand All @@ -29,11 +28,8 @@ public function __construct(

/**
* Get formatted single query line(s).
*
* @param SqlQuery $query
* @return string
*/
public function getLine(SqlQuery $query)
public function getLine(SqlQuery $query): string
{
$replace = [
'[query_nr]' => $query->number(),
Expand All @@ -48,10 +44,8 @@ public function getLine(SqlQuery $query)

/**
* Get formatted header lines.
*
* @return string
*/
public function getHeader()
public function getHeader(): string
{
$headerFields = $this->config->headerFields();
if (empty($headerFields)) {
Expand Down Expand Up @@ -97,21 +91,16 @@ public function getHeader()

/**
* Format time.
*
* @param float $time
* @return string
*/
protected function time($time)
protected function time(float $time): string
{
return $this->config->useSeconds() ? ($time / 1000.0) . 's' : $time . 'ms';
}

/**
* Get origin line.
*
* @return string
*/
protected function originLine()
protected function originLine(): string
{
return $this->app->runningInConsole()
? '(console) ' . $this->getArtisanLine()
Expand All @@ -120,21 +109,16 @@ protected function originLine()

/**
* Get query line.
*
* @param SqlQuery $query
* @return string
*/
protected function getQueryLine(SqlQuery $query)
protected function getQueryLine(SqlQuery $query): string
{
return $query->get() . ';';
}

/**
* Get Artisan line.
*
* @return string
*/
protected function getArtisanLine()
protected function getArtisanLine(): string
{
$command = $this->app['request']->server('argv', []);

Expand All @@ -147,20 +131,16 @@ protected function getArtisanLine()

/**
* Get request line.
*
* @return string
*/
protected function getRequestLine()
protected function getRequestLine(): string
{
return $this->app['request']->method() . ' ' . $this->app['request']->fullUrl();
}

/**
* Get separator line.
*
* @return string
*/
protected function separatorLine()
protected function separatorLine(): string
{
return '-- ' . str_repeat('-', 50);
}
Expand Down
8 changes: 3 additions & 5 deletions src/Listeners/SqlQueryLogSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Onlime\LaravelSqlReporter\Listeners;

use Illuminate\Console\Events\CommandFinished;
use Illuminate\Events\Dispatcher;
use Illuminate\Foundation\Http\Events\RequestHandled;
use Onlime\LaravelSqlReporter\SqlLogger;

Expand All @@ -20,10 +21,10 @@ public function __construct(
/**
* Register the listeners for the subscriber.
*
* @param \Illuminate\Events\Dispatcher $events
* @param Dispatcher $events
* @return void
*/
public function subscribe($events)
public function subscribe(Dispatcher $events)
{
$events->listen(
[
Expand All @@ -36,9 +37,6 @@ public function subscribe($events)

/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
{
Expand Down
4 changes: 1 addition & 3 deletions src/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ public function boot()

/**
* Get package config file location.
*
* @return bool|string
*/
protected function configFileLocation()
protected function configFileLocation(): string
{
return realpath(__DIR__ . '/../../config/sql-reporter.php');
}
Expand Down
2 changes: 0 additions & 2 deletions src/SqlLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class SqlLogger
{
/**
* Number of executed queries.
*
* @var int
*/
private int $queryNumber = 0;

Expand Down
Loading

0 comments on commit 3035fa0

Please sign in to comment.