Skip to content

Commit 6280912

Browse files
authored
plugin works with Laravel 10 (#9)
1 parent 2bbd033 commit 6280912

File tree

3 files changed

+110
-15
lines changed

3 files changed

+110
-15
lines changed

src/AdapterManager.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Cosmastech\LaravelStatsDAdapter;
44

5-
use Carbon\FactoryImmutable;
6-
use Carbon\WrapperClock;
5+
use Cosmastech\LaravelStatsDAdapter\Concerns\Laravel10AdapterTrait;
6+
use Cosmastech\LaravelStatsDAdapter\Utility\ClockWrapper;
77
use Cosmastech\StatsDClientAdapter\Adapters\Datadog\DatadogStatsDClientAdapter;
88
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\InMemoryClientAdapter;
99
use Cosmastech\StatsDClientAdapter\Adapters\League\LeagueStatsDClientAdapter;
@@ -23,6 +23,8 @@
2323
*/
2424
class AdapterManager extends MultipleInstanceManager
2525
{
26+
use Laravel10AdapterTrait;
27+
2628
protected $driverKey = 'adapter';
2729

2830
protected string $defaultInstanceName;
@@ -80,7 +82,7 @@ public function setDefaultInstance($name)
8082
*/
8183
public function getInstanceConfig($name)
8284
{
83-
return $this->config->get("statsd-adapter.channels.{$name}");
85+
return $this->setDriverKeyInConfig($this->config->get("statsd-adapter.channels.{$name}"));
8486
}
8587

8688
/**
@@ -124,17 +126,6 @@ protected function createMemoryAdapter(array $config): InMemoryClientAdapter
124126
);
125127
}
126128

127-
/**
128-
* @param array<string, mixed> $config
129-
* @return DatadogStatsDClientAdapter
130-
*
131-
* @throws BindingResolutionException
132-
*/
133-
protected function createLog_datadogAdapter(array $config): DatadogStatsDClientAdapter
134-
{
135-
return $this->createLogDatadogAdapter($config);
136-
}
137-
138129
/**
139130
* @param array<string, mixed> $config
140131
* @return DatadogStatsDClientAdapter
@@ -186,6 +177,6 @@ protected function createLeagueAdapter(array $config): LeagueStatsDClientAdapter
186177

187178
protected function getClockImplementation(): ClockInterface
188179
{
189-
return new WrapperClock(FactoryImmutable::getDefaultInstance());
180+
return new ClockWrapper();
190181
}
191182
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Cosmastech\LaravelStatsDAdapter\Concerns;
4+
5+
use Cosmastech\StatsDClientAdapter\Adapters\Datadog\DatadogStatsDClientAdapter;
6+
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\InMemoryClientAdapter;
7+
use Cosmastech\StatsDClientAdapter\Adapters\League\LeagueStatsDClientAdapter;
8+
use Illuminate\Contracts\Container\BindingResolutionException;
9+
use League\StatsD\Exception\ConfigurationException;
10+
11+
/**
12+
* This trait serves as a bridge between Laravel 11's MultipleInstanceManager and Laravel 10's version of the class.
13+
* - Unique driver keys were added in Laravel 11. Prior to that, the config key was required to be named "driver"
14+
* - Method names for creating the adapter were not studly cased.
15+
*/
16+
trait Laravel10AdapterTrait
17+
{
18+
/**
19+
* @param array<string, mixed> $config
20+
* @return InMemoryClientAdapter
21+
*/
22+
protected function createMemoryDriver(array $config): InMemoryClientAdapter
23+
{
24+
return $this->createMemoryAdapter($config);
25+
}
26+
27+
/**
28+
* @param array<string, mixed> $config
29+
* @return DatadogStatsDClientAdapter
30+
*
31+
* @throws BindingResolutionException
32+
*/
33+
protected function createLog_datadogDriver(array $config): DatadogStatsDClientAdapter
34+
{
35+
return $this->createLogDatadogAdapter($config);
36+
}
37+
38+
/**
39+
* @param array<string, mixed> $config
40+
* @return DatadogStatsDClientAdapter
41+
*
42+
* @throws BindingResolutionException
43+
*/
44+
protected function createLog_datadogAdapter(array $config): DatadogStatsDClientAdapter
45+
{
46+
return $this->createLogDatadogAdapter($config);
47+
}
48+
49+
/**
50+
* @param array<string, mixed> $config
51+
* @return DatadogStatsDClientAdapter
52+
*/
53+
protected function createDatadogDriver(array $config): DatadogStatsDClientAdapter
54+
{
55+
return $this->createDatadogAdapter($config);
56+
}
57+
58+
/**
59+
* @param array<string, mixed> $config
60+
* @return LeagueStatsDClientAdapter
61+
*
62+
* @throws ConfigurationException
63+
*/
64+
protected function createLeagueDriver(array $config): LeagueStatsDClientAdapter
65+
{
66+
return $this->createLeagueAdapter($config);
67+
}
68+
69+
/**
70+
* @param array<string, mixed>|null $config
71+
* @return array<string, mixed>|null
72+
*/
73+
protected function setDriverKeyInConfig(?array $config): ?array
74+
{
75+
if (empty($config)) {
76+
return $config;
77+
}
78+
79+
if (array_key_exists("driver", $config)) {
80+
return $config;
81+
}
82+
83+
$config["driver"] = $config["adapter"] ?? null;
84+
85+
return $config;
86+
}
87+
}

src/Utility/ClockWrapper.php

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\Utility;
4+
5+
use DateTimeImmutable;
6+
use Psr\Clock\ClockInterface;
7+
8+
class ClockWrapper implements ClockInterface
9+
{
10+
/**
11+
* @inheritDoc
12+
*/
13+
public function now(): DateTimeImmutable
14+
{
15+
return now()->toDateTimeImmutable();
16+
}
17+
}

0 commit comments

Comments
 (0)