diff --git a/README.md b/README.md index 7de270e..c70d6a3 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ If you want to add other arbitrary comments to the SqlComment, you can use the ` ```php use Spatie\SqlCommenter\SqlCommenter; -SqlCommenter::addComment('foo', 'bar'); +app(SqlCommenter::class)->addComment('foo', 'bar'); // select * from "users"/*foo='bar'*/; ``` diff --git a/src/SqlCommenter.php b/src/SqlCommenter.php index 70be2b2..2707ea4 100644 --- a/src/SqlCommenter.php +++ b/src/SqlCommenter.php @@ -11,11 +11,11 @@ class SqlCommenter { /** @var array */ - protected static array $extraComments = []; + protected array $extraComments = []; - public static function addComment(string $key, ?string $value): void + public function addComment(string $key, ?string $value): void { - static::$extraComments[$key] = Comment::make($key, $value); + $this->extraComments[$key] = Comment::make($key, $value); } public static function enable(): void @@ -96,9 +96,9 @@ protected function getCommentsFromCommenters( */ protected function addExtraComments(Collection $comments, string $query, Connection $connection): void { - $comments->push(...self::$extraComments); + $comments->push(...$this->extraComments); - self::$extraComments = []; + $this->extraComments = []; } /** diff --git a/src/SqlCommenterServiceProvider.php b/src/SqlCommenterServiceProvider.php index f24e924..8cbf616 100644 --- a/src/SqlCommenterServiceProvider.php +++ b/src/SqlCommenterServiceProvider.php @@ -20,7 +20,7 @@ public function configurePackage(Package $package): void public function packageBooted(): void { - $this->app->singleton(SqlCommenter::class, function () { + $this->app->scoped(SqlCommenter::class, function () { $commenterClass = config('sql-commenter.commenter_class'); if (! is_a($commenterClass, SqlCommenter::class, true)) { diff --git a/tests/SqlCommenterTest.php b/tests/SqlCommenterTest.php index 4effb1a..fc9f77a 100644 --- a/tests/SqlCommenterTest.php +++ b/tests/SqlCommenterTest.php @@ -16,7 +16,7 @@ expect($event->sql)->toContainComment('foo', 'bar'); }); - SqlCommenter::addComment('foo', 'bar'); + app(SqlCommenter::class)->addComment('foo', 'bar'); dispatch(new UsersJob()); }); @@ -26,7 +26,7 @@ expect($event->sql)->not()->toContainComment('foo', 'bar'); }); - SqlCommenter::addComment('foo', 'bar'); + app(SqlCommenter::class)->addComment('foo', 'bar'); DB::statement(<<addComment('foo', 'bar'); + app(SqlCommenter::class)->addComment('baz', ''); Event::listen(QueryExecuted::class, function (QueryExecuted $event) { expect($event->sql)->toContainComment('foo', 'bar');