|
3 | 3 | namespace Typesense\Lib;
|
4 | 4 |
|
5 | 5 | use Http\Client\Common\HttpMethodsClient;
|
| 6 | +use Http\Client\HttpClient; |
6 | 7 | use Http\Discovery\Psr17FactoryDiscovery;
|
7 | 8 | use Http\Discovery\Psr18ClientDiscovery;
|
8 | 9 | use Monolog\Handler\StreamHandler;
|
@@ -56,9 +57,9 @@ class Configuration
|
56 | 57 | private LoggerInterface $logger;
|
57 | 58 |
|
58 | 59 | /**
|
59 |
| - * @var null|ClientInterface |
| 60 | + * @var HttpMethodsClient|ClientInterface|null |
60 | 61 | */
|
61 |
| - private ?ClientInterface $client = null; |
| 62 | + private $client = null; |
62 | 63 |
|
63 | 64 | /**
|
64 | 65 | * @var int
|
@@ -103,8 +104,18 @@ public function __construct(array $config)
|
103 | 104 | $this->logger = new Logger('typesense');
|
104 | 105 | $this->logger->pushHandler(new StreamHandler('php://stdout', $this->logLevel));
|
105 | 106 |
|
106 |
| - if (true === \array_key_exists('client', $config) && $config['client'] instanceof ClientInterface) { |
107 |
| - $this->client = $config['client']; |
| 107 | + if (true === \array_key_exists('client', $config)) { |
| 108 | + if ($config['client'] instanceof HttpMethodsClient) { |
| 109 | + $this->client = $config['client']; |
| 110 | + } elseif ($config['client'] instanceof HttpClient || $config['client'] instanceof ClientInterface) { |
| 111 | + $this->client = new HttpMethodsClient( |
| 112 | + $config['client'], |
| 113 | + Psr17FactoryDiscovery::findRequestFactory(), |
| 114 | + Psr17FactoryDiscovery::findStreamFactory() |
| 115 | + ); |
| 116 | + } else { |
| 117 | + throw new ConfigError('Client must implement PSR-18 ClientInterface or Http\Client\HttpClient'); |
| 118 | + } |
108 | 119 | }
|
109 | 120 | }
|
110 | 121 |
|
@@ -216,10 +227,14 @@ public function getLogger(): LoggerInterface
|
216 | 227 | */
|
217 | 228 | public function getClient(): ClientInterface
|
218 | 229 | {
|
219 |
| - return new HttpMethodsClient( |
220 |
| - $this->client ?? Psr18ClientDiscovery::find(), |
221 |
| - Psr17FactoryDiscovery::findRequestFactory(), |
222 |
| - Psr17FactoryDiscovery::findStreamFactory(), |
223 |
| - ); |
| 230 | + if ($this->client === null) { |
| 231 | + $discoveredClient = Psr18ClientDiscovery::find(); |
| 232 | + $this->client = new HttpMethodsClient( |
| 233 | + $discoveredClient, |
| 234 | + Psr17FactoryDiscovery::findRequestFactory(), |
| 235 | + Psr17FactoryDiscovery::findStreamFactory() |
| 236 | + ); |
| 237 | + } |
| 238 | + return $this->client; |
224 | 239 | }
|
225 | 240 | }
|
0 commit comments