1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Manticoresearch; |
5
|
|
|
|
6
|
|
|
use Manticoresearch\Exceptions\RuntimeException; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Connection |
11
|
|
|
* @package Manticoresearch |
12
|
|
|
*/ |
13
|
|
|
class Connection |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $config; |
19
|
|
|
/** |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
protected $alive; |
23
|
|
|
/* |
24
|
|
|
* $params['transport'] = transport class name |
25
|
|
|
* $params['host'] = hostname |
26
|
|
|
* $params['path'] = path |
27
|
|
|
* $params['port'] = port number |
28
|
|
|
* $params['timeout'] = connection timeout |
29
|
|
|
* $params['connect_timeout'] = connection connect timeout |
30
|
|
|
* $params['proxy'] = proxy host:port string |
31
|
|
|
* $params['username'] = username for http auth |
32
|
|
|
* $params['password'] = password for http auth |
33
|
|
|
* $params['headers'] = array of custom headers |
34
|
|
|
* $params['curl'] = array of pairs of curl option=>value |
35
|
|
|
* $params['persistent'] = bool if connection is persistent |
36
|
|
|
*/ |
37
|
|
|
/** |
38
|
|
|
* Connection constructor. |
39
|
|
|
* @param array $params |
40
|
|
|
*/ |
41
|
|
|
public function __construct(array $params) |
42
|
|
|
{ |
43
|
|
|
$this->config = array( |
44
|
|
|
'transport' => 'Http', |
45
|
|
|
'host' => '127.0.0.1', |
46
|
|
|
'scheme' => 'http', |
47
|
|
|
'path' => '', |
48
|
|
|
'port' => '9308', |
49
|
|
|
'timeout' => 300, |
50
|
|
|
'connect_timeout' => 0, |
51
|
|
|
'proxy' => null, |
52
|
|
|
'username' => null, |
53
|
|
|
'password' => null, |
54
|
|
|
'headers' => [], |
55
|
|
|
'curl' => [], |
56
|
|
|
'persistent' => true |
57
|
|
|
); |
58
|
|
|
$this->config = array_merge($this->config, $params); |
59
|
|
|
$this->alive = true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $host |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function setHost($host): self |
67
|
|
|
{ |
68
|
|
|
$this->config['host'] = $host; |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
|
|
public function getHost() |
76
|
|
|
{ |
77
|
|
|
return $this->config['host']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $path |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function setPath(string $path): self |
85
|
|
|
{ |
86
|
|
|
$this->config['path'] = $path; |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
|
|
public function getPath() |
94
|
|
|
{ |
95
|
|
|
return $this->config['path']; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string|integer $port |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function setPort($port): self |
103
|
|
|
{ |
104
|
|
|
$this->config['port'] = (int)$port; |
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
public function getPort() |
112
|
|
|
{ |
113
|
|
|
return $this->config['port']; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param integer $timeout |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
|
|
public function setTimeout($timeout): self |
121
|
|
|
{ |
122
|
|
|
$this->config['timeout'] = (int)$timeout; |
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
|
|
public function getHeaders() |
130
|
|
|
{ |
131
|
|
|
return $this->config['headers']; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param array $headers |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
|
|
public function setheaders($headers): self |
139
|
|
|
{ |
140
|
|
|
$this->config['headers'] = $headers; |
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return mixed |
146
|
|
|
*/ |
147
|
|
|
public function getTimeout() |
148
|
|
|
{ |
149
|
|
|
return $this->config['timeout']; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param integer $connect_timeout |
154
|
|
|
* @return $this |
155
|
|
|
*/ |
156
|
|
|
public function setConnectTimeout($connect_timeout): self |
157
|
|
|
{ |
158
|
|
|
$this->config['connect_timeout'] = (int)$connect_timeout; |
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return mixed |
164
|
|
|
*/ |
165
|
|
|
public function getConnectTimeout() |
166
|
|
|
{ |
167
|
|
|
return $this->config['connect_timeout']; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param Transport $transport |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
|
|
public function setTransport($transport): self |
175
|
|
|
{ |
176
|
|
|
$this->config['transport'] = $transport; |
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return mixed |
182
|
|
|
*/ |
183
|
|
|
public function getTransport() |
184
|
|
|
{ |
185
|
|
|
return $this->config['transport']; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param LoggerInterface $logger |
190
|
|
|
* @return mixed |
191
|
|
|
* @throws \Exception |
192
|
|
|
*/ |
193
|
|
|
public function getTransportHandler(LoggerInterface $logger) |
194
|
|
|
{ |
195
|
|
|
return Transport::create($this->getTransport(), $this, $logger); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param array $config |
200
|
|
|
* @return $this |
201
|
|
|
*/ |
202
|
|
|
public function setConfig($config): self |
203
|
|
|
{ |
204
|
|
|
foreach ($config as $ckey => $cvalue) { |
205
|
|
|
$this->config[$ckey] = $cvalue; |
206
|
|
|
} |
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string|null $key |
212
|
|
|
* @return mixed|null |
213
|
|
|
* |
214
|
|
|
*/ |
215
|
|
|
public function getConfig($key = null) |
216
|
|
|
{ |
217
|
|
|
if ($key === null) { |
218
|
|
|
return $this->config; |
219
|
|
|
} |
220
|
|
|
return $this->config[$key] ?? null; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param array|Connection $params|self |
225
|
|
|
* @return Connection |
226
|
|
|
*/ |
227
|
|
|
public static function create($params) |
228
|
|
|
{ |
229
|
|
|
if (is_array($params)) { |
230
|
|
|
return new self($params); |
231
|
|
|
} |
232
|
|
|
if ($params instanceof self) { |
233
|
|
|
return $params; |
234
|
|
|
} |
235
|
|
|
throw new RuntimeException('connection must receive array of parameters or self'); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return bool |
240
|
|
|
*/ |
241
|
|
|
public function isAlive(): bool |
242
|
|
|
{ |
243
|
|
|
return $this->alive; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param bool $state |
248
|
|
|
*/ |
249
|
|
|
public function mark(bool $state) |
250
|
|
|
{ |
251
|
|
|
$this->alive = $state; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|