Skip to content

Commit

Permalink
Added ability to use relative path in FileCommenter (#11)
Browse files Browse the repository at this point in the history
* Added ability to use relative path in FileCommenter

* add tests

* Update config and read.me

* Small code cleanup and simplify
  • Loading branch information
peresmishnyk authored Jan 16, 2023
1 parent 9240e76 commit 3906b60
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion config/sql-commenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 10 additions & 4 deletions src/Commenters/FileCommenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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),
];
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Commenters/FileCommenterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit 3906b60

Please sign in to comment.