|
| 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 | +} |
0 commit comments