-
Notifications
You must be signed in to change notification settings - Fork 730
/
Copy pathClientConfiguration.php
214 lines (187 loc) · 6.03 KB
/
ClientConfiguration.php
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
<?php
namespace Elastica;
use Elastica\Exception\InvalidException;
use Nyholm\Dsn\Configuration\Url;
use Nyholm\Dsn\DsnParser;
use Nyholm\Dsn\Exception\ExceptionInterface as DsnException;
use Nyholm\Dsn\Exception\FunctionNotSupportedException;
/**
* Elastica client configuration.
*
* @author Antoine Lamirault <lamiraultantoine@gmail.com>
*/
class ClientConfiguration
{
/**
* Config with defaults.
*
* retryOnConflict: Use in \Elastica\Client::updateDocument
* bigintConversion: Set to true to enable the JSON bigint to string conversion option (see issue #717)
*
* @var array
*/
protected $configuration = [
'host' => null,
'port' => null,
'path' => null,
'url' => null,
'proxy' => null,
'transport' => null,
'persistent' => true,
'timeout' => null,
'connections' => [], // host, port, path, transport, compression, persistent, timeout, username, password, auth_type, config -> (curl, headers, url)
'roundRobin' => false,
'retryOnConflict' => 0,
'bigintConversion' => false,
'username' => null,
'password' => null,
'auth_type' => null, // basic, digest, gssnegotiate, ntlm
];
/**
* Create configuration.
*
* @param array $config Additional config
*
* @return ClientConfiguration
*/
public static function fromArray(array $config): self
{
$clientConfiguration = new static();
foreach ($config as $key => $value) {
$clientConfiguration->set($key, $value);
}
return $clientConfiguration;
}
/**
* Create configuration from Dsn string. Example of valid DSN strings:
* - http://localhost
* - http://foo:bar@localhost:1234?timeout=4&persistant=false
* - pool(http://127.0.0.1 http://127.0.0.2/bar?timeout=4).
*
* @return ClientConfiguration
*/
public static function fromDsn(string $dsnString): self
{
try {
$func = DsnParser::parseFunc($dsnString);
} catch (DsnException $e) {
throw new InvalidException(\sprintf('DSN "%s" is invalid.', $dsnString), 0, $e);
}
if ('dsn' === $func->getName()) {
/** @var Url $dsn */
$dsn = $func->first();
$clientConfiguration = self::fromArray(self::parseDsn($dsn));
} elseif ('pool' === $func->getName()) {
$connections = [];
$clientConfiguration = new static();
/** @var Url $arg */
foreach ($func->getArguments() as $arg) {
$connections[] = self::parseDsn($arg);
}
$clientConfiguration->set('connections', $connections);
} else {
throw new FunctionNotSupportedException($dsnString, $func->getName());
}
foreach ($func->getParameters() as $optionName => $optionValue) {
if ('false' === $optionValue) {
$optionValue = false;
} elseif ('true' === $optionValue) {
$optionValue = true;
} elseif (\is_numeric($optionValue)) {
$optionValue = (int) $optionValue;
}
$clientConfiguration->set($optionName, $optionValue);
}
return $clientConfiguration;
}
/**
* Returns a specific config key or the whole config array if not set.
*
* @throws InvalidException if the given key is not found in the configuration
*
* @return mixed Config value
*/
public function get(string $key)
{
if ('' === $key) {
return $this->configuration;
}
if (!$this->has($key)) {
throw new InvalidException('Config key is not set: '.$key);
}
return $this->configuration[$key];
}
/**
* Returns boolean indicates if configuration has key.
*/
public function has(string $key): bool
{
return \array_key_exists($key, $this->configuration);
}
/**
* Return all configuration.
*/
public function getAll(): array
{
return $this->configuration;
}
/**
* @param string $key Key to set
* @param mixed $value Value
*/
public function set(string $key, $value): void
{
$this->configuration[$key] = $value;
}
/**
* Add value to a key. If original value is not an array, value is wrapped.
*
* @param string $key Key to add
* @param mixed $value Value
*/
public function add(string $key, $value): void
{
if (!\array_key_exists($key, $this->configuration)) {
$this->configuration[$key] = [$value];
} else {
if (\is_array($this->configuration[$key])) {
$this->configuration[$key][] = $value;
} else {
$this->configuration[$key] = [$this->configuration[$key], $value];
}
}
}
private static function parseDsn(Url $dsn): array
{
$data = ['host' => $dsn->getHost()];
if (null !== $dsn->getScheme()) {
$data['transport'] = $dsn->getScheme();
}
if (null !== $dsn->getUser()) {
$data['username'] = $dsn->getUser();
}
if (null !== $dsn->getPassword()) {
$data['password'] = $dsn->getPassword();
}
if (null !== $dsn->getUser() && null !== $dsn->getPassword()) {
$data['auth_type'] = 'basic';
}
if (null !== $dsn->getPort()) {
$data['port'] = $dsn->getPort();
}
if (null !== $dsn->getPath()) {
$data['path'] = $dsn->getPath();
}
foreach ($dsn->getParameters() as $optionName => $optionValue) {
if ('false' === $optionValue) {
$optionValue = false;
} elseif ('true' === $optionValue) {
$optionValue = true;
} elseif (\is_numeric($optionValue)) {
$optionValue = (int) $optionValue;
}
$data[$optionName] = $optionValue;
}
return $data;
}
}