diff --git a/composer.json b/composer.json index 11b5590..ad0fb8c 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ }, "require": { "php": "^8.2", - "cosmastech/statsd-client-adapter": "^0.2.0", + "cosmastech/statsd-client-adapter": "^0.3", "illuminate/support": "^10.0|^11.0", "illuminate/contracts": "^10.0|^11.0" }, @@ -45,7 +45,8 @@ "extra": { "laravel": { "providers": [ - "Cosmastech\\LaravelStatsDAdapter\\StatsDAdapterServiceProvider" + "Cosmastech\\LaravelStatsDAdapter\\StatsDAdapterServiceProvider", + "Cosmastech\\LaravelStatsDAdapter\\StatsClientAwareServiceProvider" ], "aliases": { "Stats": "Cosmastech\\LaravelStatsDAdapter\\Stats" diff --git a/src/StatsClientAwareServiceProvider.php b/src/StatsClientAwareServiceProvider.php new file mode 100644 index 0000000..9fd7c4c --- /dev/null +++ b/src/StatsClientAwareServiceProvider.php @@ -0,0 +1,26 @@ +app->afterResolving( + StatsClientAwareInterface::class, + function (StatsClientAwareInterface $wantsStatsClient, Application $application): void { + $wantsStatsClient->setStatsClient($application->make(StatsDClientAdapter::class)); + } + ); + } +} diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 924f948..af849e3 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -3,6 +3,7 @@ namespace Cosmastech\LaravelStatsDAdapter\Tests; use Cosmastech\LaravelStatsDAdapter\AdapterManager; +use Cosmastech\LaravelStatsDAdapter\StatsClientAwareServiceProvider; use Illuminate\Config\Repository; use Orchestra\Testbench\Concerns\WithWorkbench; use Orchestra\Testbench\TestCase; @@ -11,6 +12,13 @@ class AbstractTestCase extends TestCase { use WithWorkbench; + protected $enablesPackageDiscoveries = true; + + protected function getPackageProviders($app) + { + return [StatsClientAwareServiceProvider::class]; + } + protected function getEnvironmentSetUp($app) { /** @var Repository $config */ diff --git a/tests/Fixtures/WantsStatsClient.php b/tests/Fixtures/WantsStatsClient.php new file mode 100644 index 0000000..d34f2ef --- /dev/null +++ b/tests/Fixtures/WantsStatsClient.php @@ -0,0 +1,17 @@ +statsClient; + } +} diff --git a/tests/StatsClientAwareServiceProviderTest.php b/tests/StatsClientAwareServiceProviderTest.php new file mode 100644 index 0000000..5d17e82 --- /dev/null +++ b/tests/StatsClientAwareServiceProviderTest.php @@ -0,0 +1,27 @@ +app->make(WantsStatsClient::class); + + // Then + self::assertSame( + $this->app->make(StatsDClientAdapter::class), + $wantsStatsClient->getStatsClient() + ); + } +}