1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Seasx\SeasLogger\Kafka; |
5
|
|
|
|
6
|
|
|
use Exception; |
7
|
|
|
use function array_shift; |
8
|
|
|
use function count; |
9
|
|
|
use function lcfirst; |
10
|
|
|
use function strpos; |
11
|
|
|
use function substr; |
12
|
|
|
use function trim; |
13
|
|
|
use function version_compare; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @method string getClientId() |
17
|
|
|
* @method string getBrokerVersion() |
18
|
|
|
* @method array getMetadataBrokerList() |
19
|
|
|
* @method int getMessageMaxBytes() |
20
|
|
|
* @method int getMetadataRequestTimeoutMs() |
21
|
|
|
* @method int getMetadataRefreshIntervalMs() |
22
|
|
|
* @method int getMetadataMaxAgeMs() |
23
|
|
|
* @method string getSecurityProtocol() |
24
|
|
|
* @method bool getSslEnable() |
25
|
|
|
* @method void setSslEnable(bool $sslEnable) |
26
|
|
|
* @method string getSslLocalCert() |
27
|
|
|
* @method string getSslLocalPk() |
28
|
|
|
* @method bool getSslVerifyPeer() |
29
|
|
|
* @method void setSslVerifyPeer(bool $sslVerifyPeer) |
30
|
|
|
* @method string getSslPassphrase() |
31
|
|
|
* @method void setSslPassphrase(string $sslPassphrase) |
32
|
|
|
* @method string getSslCafile() |
33
|
|
|
* @method string getSslPeerName() |
34
|
|
|
* @method void setSslPeerName(string $sslPeerName) |
35
|
|
|
* @method string getSaslMechanism() |
36
|
|
|
* @method string getSaslUsername() |
37
|
|
|
* @method string getSaslPassword() |
38
|
|
|
* @method string getSaslKeytab() |
39
|
|
|
* @method string getSaslPrincipal() |
40
|
|
|
*/ |
41
|
|
|
abstract class Config |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var mixed[] |
45
|
|
|
*/ |
46
|
|
|
private static $defaults = [ |
47
|
|
|
'clientId' => 'seaslog-kafka', |
48
|
|
|
'brokerVersion' => '0.10.1.0', |
49
|
|
|
'metadataBrokerList' => '', |
50
|
|
|
'messageMaxBytes' => 1000000, |
51
|
|
|
'metadataRequestTimeoutMs' => 60000, |
52
|
|
|
'metadataRefreshIntervalMs' => 300000, |
53
|
|
|
'metadataMaxAgeMs' => -1 |
54
|
|
|
]; |
55
|
|
|
/** |
56
|
|
|
* @var mixed[] |
57
|
|
|
*/ |
58
|
|
|
protected $options = []; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Config constructor. |
62
|
|
|
* @param array $configs |
63
|
|
|
*/ |
64
|
|
|
public function __construct(array $configs) |
65
|
|
|
{ |
66
|
|
|
foreach ($configs as $name => $value) { |
67
|
|
|
$method = 'set' . ucfirst($name); |
68
|
|
|
$this->$method($value); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $name |
74
|
|
|
* @param mixed[] $args |
75
|
|
|
* |
76
|
|
|
* @return bool|mixed |
77
|
|
|
*/ |
78
|
|
|
public function __call(string $name, array $args) |
79
|
|
|
{ |
80
|
|
|
$isGetter = strpos($name, 'get') === 0 || strpos($name, 'iet') === 0; |
81
|
|
|
$isSetter = strpos($name, 'set') === 0; |
82
|
|
|
|
83
|
|
|
if (!$isGetter && !$isSetter) { |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$option = lcfirst(substr($name, 3)); |
88
|
|
|
|
89
|
|
|
if ($isGetter) { |
90
|
|
|
if (isset($this->options[$option])) { |
91
|
|
|
return $this->options[$option]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (isset(self::$defaults[$option])) { |
95
|
|
|
return self::$defaults[$option]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (isset(static::$defaults[$option])) { |
|
|
|
|
99
|
|
|
return static::$defaults[$option]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (count($args) !== 1) { |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->options[$option] = array_shift($args); |
110
|
|
|
|
111
|
|
|
// check todo |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $val |
117
|
|
|
* @throws Exception |
118
|
|
|
*/ |
119
|
|
|
public function setClientId(string $val): void |
120
|
|
|
{ |
121
|
|
|
$client = trim($val); |
122
|
|
|
|
123
|
|
|
if ($client === '') { |
124
|
|
|
throw new Exception('Set clientId value is invalid, must is not empty string.'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$this->options['clientId'] = $client; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $version |
132
|
|
|
* @throws Exception |
133
|
|
|
*/ |
134
|
|
|
public function setBrokerVersion(string $version): void |
135
|
|
|
{ |
136
|
|
|
$version = trim($version); |
137
|
|
|
|
138
|
|
|
if ($version === '' || version_compare($version, '0.8.0', '<')) { |
139
|
|
|
throw new Exception('Set broker version value is invalid, must is not empty string and gt 0.8.0.'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->options['brokerVersion'] = $version; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $brokerList |
147
|
|
|
* @throws Exception |
148
|
|
|
*/ |
149
|
|
|
public function setMetadataBrokerList(string $brokerList): void |
150
|
|
|
{ |
151
|
|
|
$brokerList = trim($brokerList); |
152
|
|
|
|
153
|
|
|
$brokers = array_filter( |
154
|
|
|
explode(',', $brokerList), |
155
|
|
|
function (string $broker): bool { |
156
|
|
|
return preg_match('/^(.*:[\d]+)$/', $broker) === 1; |
157
|
|
|
} |
158
|
|
|
); |
159
|
|
|
|
160
|
|
|
if (empty($brokers)) { |
161
|
|
|
throw new Exception( |
162
|
|
|
'Broker list must be a comma-separated list of brokers (format: "host:port"), with at least one broker' |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$this->options['metadataBrokerList'] = $brokers; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function clear(): void |
170
|
|
|
{ |
171
|
|
|
$this->options = []; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param int $messageMaxBytes |
176
|
|
|
* @throws Exception |
177
|
|
|
*/ |
178
|
|
|
public function setMessageMaxBytes(int $messageMaxBytes): void |
179
|
|
|
{ |
180
|
|
|
if ($messageMaxBytes < 1000 || $messageMaxBytes > 1000000000) { |
181
|
|
|
throw new Exception('Set message max bytes value is invalid, must set it 1000 .. 1000000000'); |
182
|
|
|
} |
183
|
|
|
$this->options['messageMaxBytes'] = $messageMaxBytes; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param int $metadataRequestTimeoutMs |
188
|
|
|
* @throws Exception |
189
|
|
|
*/ |
190
|
|
|
public function setMetadataRequestTimeoutMs(int $metadataRequestTimeoutMs): void |
191
|
|
|
{ |
192
|
|
|
if ($metadataRequestTimeoutMs < 10 || $metadataRequestTimeoutMs > 900000) { |
193
|
|
|
throw new Exception('Set metadata request timeout value is invalid, must set it 10 .. 900000'); |
194
|
|
|
} |
195
|
|
|
$this->options['metadataRequestTimeoutMs'] = $metadataRequestTimeoutMs; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param int $metadataRefreshIntervalMs |
200
|
|
|
* @throws Exception |
201
|
|
|
*/ |
202
|
|
|
public function setMetadataRefreshIntervalMs(int $metadataRefreshIntervalMs): void |
203
|
|
|
{ |
204
|
|
|
if ($metadataRefreshIntervalMs < 10 || $metadataRefreshIntervalMs > 3600000) { |
205
|
|
|
throw new Exception('Set metadata refresh interval value is invalid, must set it 10 .. 3600000'); |
206
|
|
|
} |
207
|
|
|
$this->options['metadataRefreshIntervalMs'] = $metadataRefreshIntervalMs; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param int $metadataMaxAgeMs |
212
|
|
|
* @throws Exception |
213
|
|
|
*/ |
214
|
|
|
public function setMetadataMaxAgeMs(int $metadataMaxAgeMs): void |
215
|
|
|
{ |
216
|
|
|
if ($metadataMaxAgeMs < 1 || $metadataMaxAgeMs > 86400000) { |
217
|
|
|
throw new Exception('Set metadata max age value is invalid, must set it 1 .. 86400000'); |
218
|
|
|
} |
219
|
|
|
$this->options['metadataMaxAgeMs'] = $metadataMaxAgeMs; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|