|
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
|
|
|
protected static $defaults = [ |
|
47
|
|
|
]; |
|
48
|
|
|
/** |
|
49
|
|
|
* @var mixed[] |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $options = []; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Config constructor. |
|
55
|
|
|
* @param array $configs |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct(array $configs) |
|
58
|
|
|
{ |
|
59
|
|
|
foreach ($configs as $name => $value) { |
|
60
|
|
|
$method = 'set' . ucfirst($name); |
|
61
|
|
|
$this->$method($value); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $name |
|
67
|
|
|
* @param mixed[] $args |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool|mixed |
|
70
|
|
|
*/ |
|
71
|
|
|
public function __call(string $name, array $args) |
|
72
|
|
|
{ |
|
73
|
|
|
$isGetter = strpos($name, 'get') === 0 || strpos($name, 'iet') === 0; |
|
74
|
|
|
$isSetter = strpos($name, 'set') === 0; |
|
75
|
|
|
|
|
76
|
|
|
if (!$isGetter && !$isSetter) { |
|
77
|
|
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$option = lcfirst(substr($name, 3)); |
|
81
|
|
|
|
|
82
|
|
|
if ($isGetter) { |
|
83
|
|
|
if (isset($this->options[$option])) { |
|
84
|
|
|
return $this->options[$option]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (isset(static::$defaults[$option])) { |
|
88
|
|
|
return static::$defaults[$option]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (count($args) !== 1) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$this->options[$option] = array_shift($args); |
|
99
|
|
|
|
|
100
|
|
|
// check todo |
|
101
|
|
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $val |
|
106
|
|
|
* @throws Exception |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setClientId(string $val): void |
|
109
|
|
|
{ |
|
110
|
|
|
$client = trim($val); |
|
111
|
|
|
|
|
112
|
|
|
if ($client === '') { |
|
113
|
|
|
throw new Exception('Set clientId value is invalid, must is not empty string.'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$this->options['clientId'] = $client; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $version |
|
121
|
|
|
* @throws Exception |
|
122
|
|
|
*/ |
|
123
|
|
|
public function setBrokerVersion(string $version): void |
|
124
|
|
|
{ |
|
125
|
|
|
$version = trim($version); |
|
126
|
|
|
|
|
127
|
|
|
if ($version === '' || version_compare($version, '0.8.0', '<')) { |
|
128
|
|
|
throw new Exception('Set broker version value is invalid, must is not empty string and gt 0.8.0.'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$this->options['brokerVersion'] = $version; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param string $brokerList |
|
136
|
|
|
* @throws Exception |
|
137
|
|
|
*/ |
|
138
|
|
|
public function setMetadataBrokerList(string $brokerList): void |
|
139
|
|
|
{ |
|
140
|
|
|
$brokerList = trim($brokerList); |
|
141
|
|
|
|
|
142
|
|
|
$brokers = array_filter( |
|
143
|
|
|
explode(',', $brokerList), |
|
144
|
|
|
function (string $broker): bool { |
|
145
|
|
|
return preg_match('/^(.*:[\d]+)$/', $broker) === 1; |
|
146
|
|
|
} |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
if (empty($brokers)) { |
|
150
|
|
|
throw new Exception( |
|
151
|
|
|
'Broker list must be a comma-separated list of brokers (format: "host:port"), with at least one broker' |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$this->options['metadataBrokerList'] = $brokers; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function clear(): void |
|
159
|
|
|
{ |
|
160
|
|
|
$this->options = []; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param int $messageMaxBytes |
|
165
|
|
|
* @throws Exception |
|
166
|
|
|
*/ |
|
167
|
|
|
public function setMessageMaxBytes(int $messageMaxBytes): void |
|
168
|
|
|
{ |
|
169
|
|
|
if ($messageMaxBytes < 1000 || $messageMaxBytes > 1000000000) { |
|
170
|
|
|
throw new Exception('Set message max bytes value is invalid, must set it 1000 .. 1000000000'); |
|
171
|
|
|
} |
|
172
|
|
|
$this->options['messageMaxBytes'] = $messageMaxBytes; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param int $metadataRequestTimeoutMs |
|
177
|
|
|
* @throws Exception |
|
178
|
|
|
*/ |
|
179
|
|
|
public function setMetadataRequestTimeoutMs(int $metadataRequestTimeoutMs): void |
|
180
|
|
|
{ |
|
181
|
|
|
if ($metadataRequestTimeoutMs < 10 || $metadataRequestTimeoutMs > 900000) { |
|
182
|
|
|
throw new Exception('Set metadata request timeout value is invalid, must set it 10 .. 900000'); |
|
183
|
|
|
} |
|
184
|
|
|
$this->options['metadataRequestTimeoutMs'] = $metadataRequestTimeoutMs; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param int $metadataRefreshIntervalMs |
|
189
|
|
|
* @throws Exception |
|
190
|
|
|
*/ |
|
191
|
|
|
public function setMetadataRefreshIntervalMs(int $metadataRefreshIntervalMs): void |
|
192
|
|
|
{ |
|
193
|
|
|
if ($metadataRefreshIntervalMs < 10 || $metadataRefreshIntervalMs > 3600000) { |
|
194
|
|
|
throw new Exception('Set metadata refresh interval value is invalid, must set it 10 .. 3600000'); |
|
195
|
|
|
} |
|
196
|
|
|
$this->options['metadataRefreshIntervalMs'] = $metadataRefreshIntervalMs; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param int $metadataMaxAgeMs |
|
201
|
|
|
* @throws Exception |
|
202
|
|
|
*/ |
|
203
|
|
|
public function setMetadataMaxAgeMs(int $metadataMaxAgeMs): void |
|
204
|
|
|
{ |
|
205
|
|
|
if ($metadataMaxAgeMs < 1 || $metadataMaxAgeMs > 86400000) { |
|
206
|
|
|
throw new Exception('Set metadata max age value is invalid, must set it 1 .. 86400000'); |
|
207
|
|
|
} |
|
208
|
|
|
$this->options['metadataMaxAgeMs'] = $metadataMaxAgeMs; |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|