-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from cosmastech/service-provider-for-stats-cli…
…ent-aware Automatically set the stats client when class implements `StatsClientAwareInterface`
- Loading branch information
Showing
5 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Cosmastech\LaravelStatsDAdapter; | ||
|
||
use Cosmastech\StatsDClientAdapter\Adapters\Contracts\StatsClientAwareInterface; | ||
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter; | ||
use Illuminate\Contracts\Foundation\Application; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class StatsClientAwareServiceProvider extends ServiceProvider | ||
{ | ||
public function register(): void | ||
{ | ||
// | ||
} | ||
|
||
public function boot(): void | ||
{ | ||
$this->app->afterResolving( | ||
StatsClientAwareInterface::class, | ||
function (StatsClientAwareInterface $wantsStatsClient, Application $application): void { | ||
$wantsStatsClient->setStatsClient($application->make(StatsDClientAdapter::class)); | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Cosmastech\LaravelStatsDAdapter\Tests\Fixtures; | ||
|
||
use Cosmastech\StatsDClientAdapter\Adapters\Concerns\StatsClientAwareTrait; | ||
use Cosmastech\StatsDClientAdapter\Adapters\Contracts\StatsClientAwareInterface; | ||
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter; | ||
|
||
class WantsStatsClient implements StatsClientAwareInterface | ||
{ | ||
use StatsClientAwareTrait; | ||
|
||
public function getStatsClient(): StatsDClientAdapter | ||
{ | ||
return $this->statsClient; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Cosmastech\LaravelStatsDAdapter\Tests; | ||
|
||
use Cosmastech\LaravelStatsDAdapter\Tests\Fixtures\WantsStatsClient; | ||
use Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter; | ||
use Illuminate\Support\Facades\Config; | ||
use PHPUnit\Framework\Attributes\Test; | ||
|
||
class StatsClientAwareServiceProviderTest extends AbstractTestCase | ||
{ | ||
#[Test] | ||
public function appResolvesStatsClientAwareInterfaceClass_classHasStatsClientSet(): void | ||
{ | ||
// Given | ||
Config::set("statsd-adapter.default", "memory"); | ||
|
||
// When | ||
$wantsStatsClient = $this->app->make(WantsStatsClient::class); | ||
|
||
// Then | ||
self::assertSame( | ||
$this->app->make(StatsDClientAdapter::class), | ||
$wantsStatsClient->getStatsClient() | ||
); | ||
} | ||
} |