diff --git a/README.md b/README.md index df240dc..784e823 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,11 @@ return [ Spatie\SqlCommenter\Commenters\ControllerCommenter::class => ['includeNamespace' => false], Spatie\SqlCommenter\Commenters\RouteCommenter::class, Spatie\SqlCommenter\Commenters\JobCommenter::class => ['includeNamespace' => false], - Spatie\SqlCommenter\Commenters\FileCommenter::class => ['backtraceLimit' => 20], + Spatie\SqlCommenter\Commenters\FileCommenter::class => [ + 'backtraceLimit' => 20, + 'excludePathSegments' => [], + 'useRelativePath' => false, + ], Spatie\SqlCommenter\Commenters\CurrentUserCommenter::class, // Spatie\SqlCommenter\Commenters\FrameworkVersionCommenter::class, // Spatie\SqlCommenter\Commenters\DbDriverCommenter::class, diff --git a/config/sql-commenter.php b/config/sql-commenter.php index 18a4d38..cb31e92 100644 --- a/config/sql-commenter.php +++ b/config/sql-commenter.php @@ -13,7 +13,11 @@ Spatie\SqlCommenter\Commenters\ControllerCommenter::class => ['includeNamespace' => false], Spatie\SqlCommenter\Commenters\RouteCommenter::class, Spatie\SqlCommenter\Commenters\JobCommenter::class => ['includeNamespace' => false], - Spatie\SqlCommenter\Commenters\FileCommenter::class => ['backtraceLimit' => 20, 'excludePathSegments' => []], + Spatie\SqlCommenter\Commenters\FileCommenter::class => [ + 'backtraceLimit' => 20, + 'excludePathSegments' => [], + 'useRelativePath' => false, + ], Spatie\SqlCommenter\Commenters\CurrentUserCommenter::class, // Spatie\SqlCommenter\Commenters\FrameworkVersionCommenter::class, // Spatie\SqlCommenter\Commenters\DbDriverCommenter::class, diff --git a/src/Commenters/FileCommenter.php b/src/Commenters/FileCommenter.php index af15224..ee4f903 100644 --- a/src/Commenters/FileCommenter.php +++ b/src/Commenters/FileCommenter.php @@ -10,9 +10,11 @@ class FileCommenter implements Commenter { public function __construct( - public int $backtraceLimit = 40, + public int $backtraceLimit = 40, public array $excludePathSegments = [], - ) { + public bool $useRelativePath = false, + ) + { } /** @return Comment|Comment[]|null */ @@ -44,12 +46,16 @@ public function comments(string $query, Connection $connection): Comment|array|n return true; }); - if (! $frame) { + if (!$frame) { return null; } + $filePath = $this->useRelativePath && str_starts_with($frame->file, base_path()) + ? substr($frame->file, strlen(base_path()) + 1) + : $frame->file; + return [ - Comment::make('file', $frame->file), + Comment::make('file', $filePath), Comment::make('line', $frame->lineNumber), ]; } diff --git a/tests/Commenters/FileCommenterTest.php b/tests/Commenters/FileCommenterTest.php index 6ca26f2..868f49b 100644 --- a/tests/Commenters/FileCommenterTest.php +++ b/tests/Commenters/FileCommenterTest.php @@ -27,3 +27,16 @@ User::count(); }); + +it('logs the file use relative path', function () { + $this->addCommenterToConfig(FileCommenter::class, ['useRelativePath' => true]); + + Event::listen(QueryExecuted::class, function (QueryExecuted $event) { + expect($event->sql)->not()->toContainComment('file', __FILE__); + expect($event->sql)->toContainComment('file', substr(__FILE__, strlen(__DIR__) + 1)); + }); + + app()->setBasePath(__DIR__); + User::count(); +}); +