Skip to content

Commit d9b1f6c

Browse files
authored
Merge pull request #11 from cosmastech/service-provider-for-stats-client-aware
Automatically set the stats client when class implements `StatsClientAwareInterface`
2 parents 3ecf2f8 + ac70a46 commit d9b1f6c

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"require": {
2626
"php": "^8.2",
27-
"cosmastech/statsd-client-adapter": "^0.2.0",
27+
"cosmastech/statsd-client-adapter": "^0.3",
2828
"illuminate/support": "^10.0|^11.0",
2929
"illuminate/contracts": "^10.0|^11.0"
3030
},
@@ -45,7 +45,8 @@
4545
"extra": {
4646
"laravel": {
4747
"providers": [
48-
"Cosmastech\\LaravelStatsDAdapter\\StatsDAdapterServiceProvider"
48+
"Cosmastech\\LaravelStatsDAdapter\\StatsDAdapterServiceProvider",
49+
"Cosmastech\\LaravelStatsDAdapter\\StatsClientAwareServiceProvider"
4950
],
5051
"aliases": {
5152
"Stats": "Cosmastech\\LaravelStatsDAdapter\\Stats"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Cosmastech\LaravelStatsDAdapter;
4+
5+
use Cosmastech\StatsDClientAdapter\Adapters\Contracts\StatsClientAwareInterface;
6+
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
7+
use Illuminate\Contracts\Foundation\Application;
8+
use Illuminate\Support\ServiceProvider;
9+
10+
class StatsClientAwareServiceProvider extends ServiceProvider
11+
{
12+
public function register(): void
13+
{
14+
//
15+
}
16+
17+
public function boot(): void
18+
{
19+
$this->app->afterResolving(
20+
StatsClientAwareInterface::class,
21+
function (StatsClientAwareInterface $wantsStatsClient, Application $application): void {
22+
$wantsStatsClient->setStatsClient($application->make(StatsDClientAdapter::class));
23+
}
24+
);
25+
}
26+
}

tests/AbstractTestCase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Cosmastech\LaravelStatsDAdapter\Tests;
44

55
use Cosmastech\LaravelStatsDAdapter\AdapterManager;
6+
use Cosmastech\LaravelStatsDAdapter\StatsClientAwareServiceProvider;
67
use Illuminate\Config\Repository;
78
use Orchestra\Testbench\Concerns\WithWorkbench;
89
use Orchestra\Testbench\TestCase;
@@ -11,6 +12,13 @@ class AbstractTestCase extends TestCase
1112
{
1213
use WithWorkbench;
1314

15+
protected $enablesPackageDiscoveries = true;
16+
17+
protected function getPackageProviders($app)
18+
{
19+
return [StatsClientAwareServiceProvider::class];
20+
}
21+
1422
protected function getEnvironmentSetUp($app)
1523
{
1624
/** @var Repository $config */
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Cosmastech\LaravelStatsDAdapter\Tests\Fixtures;
4+
5+
use Cosmastech\StatsDClientAdapter\Adapters\Concerns\StatsClientAwareTrait;
6+
use Cosmastech\StatsDClientAdapter\Adapters\Contracts\StatsClientAwareInterface;
7+
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
8+
9+
class WantsStatsClient implements StatsClientAwareInterface
10+
{
11+
use StatsClientAwareTrait;
12+
13+
public function getStatsClient(): StatsDClientAdapter
14+
{
15+
return $this->statsClient;
16+
}
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Cosmastech\LaravelStatsDAdapter\Tests;
4+
5+
use Cosmastech\LaravelStatsDAdapter\Tests\Fixtures\WantsStatsClient;
6+
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter;
7+
use Illuminate\Support\Facades\Config;
8+
use PHPUnit\Framework\Attributes\Test;
9+
10+
class StatsClientAwareServiceProviderTest extends AbstractTestCase
11+
{
12+
#[Test]
13+
public function appResolvesStatsClientAwareInterfaceClass_classHasStatsClientSet(): void
14+
{
15+
// Given
16+
Config::set("statsd-adapter.default", "memory");
17+
18+
// When
19+
$wantsStatsClient = $this->app->make(WantsStatsClient::class);
20+
21+
// Then
22+
self::assertSame(
23+
$this->app->make(StatsDClientAdapter::class),
24+
$wantsStatsClient->getStatsClient()
25+
);
26+
}
27+
}

0 commit comments

Comments
 (0)