forked from typesense/typesense-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfiguration.php
More file actions
263 lines (229 loc) · 6.49 KB
/
Configuration.php
File metadata and controls
263 lines (229 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
namespace Typesense\Lib;
use Http\Client\Common\HttpMethodsClient;
use Http\Client\HttpClient;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Http\Client\ClientInterface;
use Psr\Log\LoggerInterface;
use Typesense\Exceptions\ConfigError;
/**
* Class Configuration
*
* @package \Typesense
* @date 4/5/20
* @author Abdullah Al-Faqeir <abdullah@devloops.net>
*/
class Configuration
{
/**
* @var Node[]
*/
private array $nodes;
/**
* @var Node|null
*/
private ?Node $nearestNode;
/**
* @var string
*/
private string $apiKey;
/**
* @var float
*/
private float $numRetries;
/**
* @var float
*/
private float $retryIntervalSeconds;
/**
* @var int
*/
private int $healthCheckIntervalSeconds;
/**
* @var LoggerInterface
*/
private LoggerInterface $logger;
/**
* @var HttpMethodsClient|ClientInterface|null
*/
private $client = null;
/**
* @var int
*/
private int $logLevel;
/**
* @var bool
*/
private bool $randomizeNodes;
/**
* Configuration constructor.
*
* @param array $config
*
* @throws ConfigError
*/
public function __construct(array $config)
{
$this->validateConfigArray($config);
$nodes = $config['nodes'] ?? [];
foreach ($nodes as $node) {
$this->nodes[] = new Node($node['host'], $node['port'], $node['path'] ?? '', $node['protocol']);
}
$this->randomizeNodes = $config['randomize_nodes'] ?? true;
if ($this->randomizeNodes) {
shuffle($this->nodes);
}
$nearestNode = $config['nearest_node'] ?? null;
$this->nearestNode = null;
if (null !== $nearestNode) {
$this->nearestNode =
new Node(
$nearestNode['host'],
$nearestNode['port'],
$nearestNode['path'] ?? '',
$nearestNode['protocol']
);
}
$this->apiKey = $config['api_key'] ?? '';
$this->healthCheckIntervalSeconds = (int)($config['healthcheck_interval_seconds'] ?? 60);
$this->numRetries = (float)($config['num_retries'] ?? 3);
$this->retryIntervalSeconds = (float)($config['retry_interval_seconds'] ?? 1.0);
// Allow custom logger injection
if (isset($config['logger'])) {
if (!$config['logger'] instanceof LoggerInterface) {
throw new ConfigError('Logger must implement Psr\Log\LoggerInterface');
}
if (isset($config['log_level'])) {
throw new \InvalidArgumentException('Setting log_level is not allowed when a custom logger is provided.');
}
$this->logger = $config['logger'];
} else {
$this->logLevel = $config['log_level'] ?? Logger::WARNING;
$this->logger = new Logger('typesense');
$this->logger->pushHandler(new StreamHandler('php://stdout', $this->logLevel));
}
if (isset($config['client'])) {
if ($config['client'] instanceof HttpMethodsClient || $config['client'] instanceof ClientInterface) {
$this->client = $config['client'];
} elseif ($config['client'] instanceof HttpClient) {
$this->client = new HttpMethodsClient(
$config['client'],
Psr17FactoryDiscovery::findRequestFactory(),
Psr17FactoryDiscovery::findStreamFactory()
);
} else {
throw new ConfigError('Client must implement PSR-18 ClientInterface or Http\Client\HttpClient');
}
}
}
/**
* @param array $config
*
* @throws ConfigError
*/
private function validateConfigArray(array $config): void
{
$nodes = $config['nodes'] ?? false;
if (!$nodes) {
throw new ConfigError('`nodes` is not defined.');
}
$apiKey = $config['api_key'] ?? false;
if (!$apiKey) {
throw new ConfigError('`api_key` is not defined.');
}
foreach ($nodes as $node) {
if (!$this->validateNodeFields($node)) {
throw new ConfigError(
'`node` entry be a dictionary with the following required keys: host, port, protocol, api_key'
);
}
}
$nearestNode = $config['nearest_node'] ?? [];
if (!empty($nearestNode) && !$this->validateNodeFields($nearestNode)) {
throw new ConfigError(
'`nearest_node` entry be a dictionary with the following required keys: host, port, protocol, api_key'
);
}
}
/**
* @param array $node
*
* @return bool
*/
public function validateNodeFields(array $node): bool
{
$keys = [
'host',
'port',
'protocol',
];
return !array_diff_key(array_flip($keys), $node);
}
/**
* @return Node[]
*/
public function getNodes(): array
{
return $this->nodes;
}
/**
* @return Node
*/
public function getNearestNode(): ?Node
{
return $this->nearestNode;
}
/**
* @return mixed|string
*/
public function getApiKey()
{
return $this->apiKey;
}
/**
* @return float
*/
public function getNumRetries(): float
{
return $this->numRetries;
}
/**
* @return float
*/
public function getRetryIntervalSeconds(): float
{
return $this->retryIntervalSeconds;
}
/**
* @return float|mixed
*/
public function getHealthCheckIntervalSeconds()
{
return $this->healthCheckIntervalSeconds;
}
/**
* @return LoggerInterface
*/
public function getLogger(): LoggerInterface
{
return $this->logger;
}
/**
* @return ClientInterface | HttpMethodsClient
*/
public function getClient()
{
if ($this->client === null) {
$discoveredClient = Psr18ClientDiscovery::find();
$this->client = new HttpMethodsClient(
$discoveredClient,
Psr17FactoryDiscovery::findRequestFactory(),
Psr17FactoryDiscovery::findStreamFactory()
);
}
return $this->client;
}
}