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'); diff --git a/tests/TestSupport/TestCase.php b/tests/TestSupport/TestCase.php index c53ebb2..8e1744c 100644 --- a/tests/TestSupport/TestCase.php +++ b/tests/TestSupport/TestCase.php @@ -3,6 +3,8 @@ namespace Spatie\SqlCommenter\Tests\TestSupport; use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Schema; use Orchestra\Testbench\TestCase as Orchestra; use Spatie\SqlCommenter\SqlCommenterServiceProvider; @@ -28,8 +30,16 @@ public function getEnvironmentSetUp($app) { config()->set('database.default', 'testing'); - $migration = include __DIR__.'/../../vendor/orchestra/testbench-core/laravel/migrations/2014_10_12_000000_testbench_create_users_table.php'; - $migration->up(); + Schema::create('users', function (Blueprint $table) { + $table->id(); + $table->string('name'); + $table->string('email')->unique(); + $table->timestamp('email_verified_at')->nullable(); + $table->string('password'); + $table->rememberToken(); + $table->timestamps(); + $table->softDeletes(); + }); } public function addCommenterToConfig(string $commenterClass, array $options = []): self