@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -require_once dirname(__DIR__) . '/vendor/autoload.php'; |
|
3 | +require_once dirname(__DIR__).'/vendor/autoload.php'; |
|
4 | 4 | |
5 | 5 | use Seasx\SeasLogger\ArrayHelper; |
6 | 6 | use Seasx\SeasLogger\ConsoleColor; |
@@ -15,11 +15,11 @@ discard block |
||
15 | 15 | use Seasx\SeasLogger\Targets\KafkaTarget; |
16 | 16 | use Seasx\SeasLogger\Targets\StyleTarget; |
17 | 17 | |
18 | -\Co\Run(function () { |
|
18 | +\Co\Run(function() { |
|
19 | 19 | $logger = new Logger( |
20 | 20 | new LoggerConfig([ |
21 | 21 | 'echo' => new StyleTarget([ |
22 | - 'info',//过滤等级,默认为[]全部输出 |
|
22 | + 'info', //过滤等级,默认为[]全部输出 |
|
23 | 23 | ]), |
24 | 24 | 'kafka' => new KafkaTarget( |
25 | 25 | new Producter( |
@@ -36,16 +36,16 @@ discard block |
||
36 | 36 | [['task_id', 'string'], ['worker_id', 'string']]//自定义模板添加的处理字段,顺序需要按照日志记录中的template数组一致 |
37 | 37 | ) |
38 | 38 | ], [ |
39 | - 'appName' => 'Seaslog',//应用名:远程发送日志的时候用于区分是哪个应用发送来的 |
|
40 | - 'bufferSize' => 1,//定量:buffer>=时会输出,默认为1,每次记录都会输出 |
|
41 | - 'tick' => 0,//定时:每tick秒输出,默认为0,不开启定时 |
|
42 | - 'recall_depth' => 2,//与Seaslog配置参数一样,默认为0 |
|
39 | + 'appName' => 'Seaslog', //应用名:远程发送日志的时候用于区分是哪个应用发送来的 |
|
40 | + 'bufferSize' => 1, //定量:buffer>=时会输出,默认为1,每次记录都会输出 |
|
41 | + 'tick' => 0, //定时:每tick秒输出,默认为0,不开启定时 |
|
42 | + 'recall_depth' => 2, //与Seaslog配置参数一样,默认为0 |
|
43 | 43 | ])); |
44 | 44 | /** |
45 | 45 | * 这里可以注册两个回调函数,会在log方法前执行,可以用来处理RequestID,Request URI,Request Method,Client IP的值 |
46 | 46 | * 下面是示例代码,具体的设置根据自己需要 |
47 | 47 | */ |
48 | - $logger->getConfig()->registerTemplate(function () { |
|
48 | + $logger->getConfig()->registerTemplate(function() { |
|
49 | 49 | $possibleStyles = (new ConsoleColor())->getPossibleStyles(); |
50 | 50 | $htmlColors = HtmlColor::getPossibleColors(); |
51 | 51 | if (($requestVar = Context::get(Logger::CONTEXT_KEY)) === null) { |
@@ -24,7 +24,7 @@ |
||
24 | 24 | |
25 | 25 | $header = $this->requestHeader('seaslog-kafka', self::METADATA_REQUEST, self::METADATA_REQUEST); |
26 | 26 | $data = self::encodeArray($payloads, [$this, 'encodeString'], self::PACK_INT16); |
27 | - $data = self::encodeString($header . $data, self::PACK_INT32); |
|
27 | + $data = self::encodeString($header.$data, self::PACK_INT32); |
|
28 | 28 | |
29 | 29 | return $data; |
30 | 30 | } |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | $body .= $options !== null ? $func($value, $options) : $func($value); |
130 | 130 | } |
131 | 131 | |
132 | - return self::pack(self::BIT_B32, (string)$arrayCount) . $body; |
|
132 | + return self::pack(self::BIT_B32, ( string ) $arrayCount).$body; |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | public static function pack(string $type, string $data): string |
@@ -138,11 +138,11 @@ discard block |
||
138 | 138 | return pack($type, $data); |
139 | 139 | } |
140 | 140 | |
141 | - if ((int)$data === -1) { // -1L |
|
141 | + if (( int ) $data === -1) { // -1L |
|
142 | 142 | return hex2bin('ffffffffffffffff'); |
143 | 143 | } |
144 | 144 | |
145 | - if ((int)$data === -2) { // -2L |
|
145 | + if (( int ) $data === -2) { // -2L |
|
146 | 146 | return hex2bin('fffffffffffffffe'); |
147 | 147 | } |
148 | 148 | |
@@ -179,9 +179,9 @@ discard block |
||
179 | 179 | public function requestHeader(string $clientId, int $correlationId, int $apiKey): string |
180 | 180 | { |
181 | 181 | // int16 -- apiKey int16 -- apiVersion int32 correlationId |
182 | - $binData = self::pack(self::BIT_B16, (string)$apiKey); |
|
183 | - $binData .= self::pack(self::BIT_B16, (string)$this->getApiVersion($apiKey)); |
|
184 | - $binData .= self::pack(self::BIT_B32, (string)$correlationId); |
|
182 | + $binData = self::pack(self::BIT_B16, ( string ) $apiKey); |
|
183 | + $binData .= self::pack(self::BIT_B16, ( string ) $this->getApiVersion($apiKey)); |
|
184 | + $binData .= self::pack(self::BIT_B32, ( string ) $correlationId); |
|
185 | 185 | |
186 | 186 | // concat client id |
187 | 187 | $binData .= self::encodeString($clientId, self::PACK_INT16); |
@@ -226,7 +226,7 @@ discard block |
||
226 | 226 | $packLen = $bytes === self::PACK_INT32 ? self::BIT_B32 : self::BIT_B16; |
227 | 227 | $string = self::compress($string, $compression); |
228 | 228 | |
229 | - return self::pack($packLen, (string)strlen($string)) . $string; |
|
229 | + return self::pack($packLen, ( string ) strlen($string)).$string; |
|
230 | 230 | } |
231 | 231 | |
232 | 232 | /** |
@@ -245,7 +245,7 @@ discard block |
||
245 | 245 | } |
246 | 246 | |
247 | 247 | if ($compression !== self::COMPRESSION_GZIP) { |
248 | - throw new BadMethodCallException('Unknown compression flag: ' . $compression); |
|
248 | + throw new BadMethodCallException('Unknown compression flag: '.$compression); |
|
249 | 249 | } |
250 | 250 | |
251 | 251 | return gzencode($string); |
@@ -272,7 +272,7 @@ discard block |
||
272 | 272 | return ['length' => $offset, 'data' => '']; |
273 | 273 | } |
274 | 274 | |
275 | - $data = (string)substr($data, $offset, $packLen); |
|
275 | + $data = ( string ) substr($data, $offset, $packLen); |
|
276 | 276 | $offset += $packLen; |
277 | 277 | |
278 | 278 | return ['length' => $offset, 'data' => self::decompress($data, $compression)]; |
@@ -341,7 +341,7 @@ discard block |
||
341 | 341 | $length = strlen($bytes); |
342 | 342 | |
343 | 343 | if ($length !== $expectedLength) { |
344 | - throw new Exception('unpack failed. string(raw) length is ' . $length . ' , TO ' . $type); |
|
344 | + throw new Exception('unpack failed. string(raw) length is '.$length.' , TO '.$type); |
|
345 | 345 | } |
346 | 346 | } |
347 | 347 | |
@@ -354,7 +354,7 @@ discard block |
||
354 | 354 | if (self::$isLittleEndianSystem === null) { |
355 | 355 | [$endianTest] = array_values(unpack('L1L', pack('V', 1))); |
356 | 356 | |
357 | - self::$isLittleEndianSystem = (int)$endianTest === 1; |
|
357 | + self::$isLittleEndianSystem = ( int ) $endianTest === 1; |
|
358 | 358 | } |
359 | 359 | |
360 | 360 | return self::$isLittleEndianSystem; |
@@ -369,7 +369,7 @@ discard block |
||
369 | 369 | */ |
370 | 370 | public static function convertSignedShortFromLittleEndianToBigEndian(array $bits): array |
371 | 371 | { |
372 | - $convert = function (int $bit): int { |
|
372 | + $convert = function(int $bit): int { |
|
373 | 373 | $lsb = $bit & 0xff; |
374 | 374 | $msb = $bit >> 8 & 0xff; |
375 | 375 | $bit = $lsb << 8 | $msb; |
@@ -395,7 +395,7 @@ discard block |
||
395 | 395 | } |
396 | 396 | |
397 | 397 | if ($compression !== self::COMPRESSION_GZIP) { |
398 | - throw new BadMethodCallException('Unknown compression flag: ' . $compression); |
|
398 | + throw new BadMethodCallException('Unknown compression flag: '.$compression); |
|
399 | 399 | } |
400 | 400 | |
401 | 401 | return gzdecode($string); |
@@ -429,7 +429,7 @@ discard block |
||
429 | 429 | if (!isset($ret['length'], $ret['data'])) { |
430 | 430 | throw new Exception('Decode array failed, given function return format is invalid'); |
431 | 431 | } |
432 | - if ((int)$ret['length'] === 0) { |
|
432 | + if (( int ) $ret['length'] === 0) { |
|
433 | 433 | continue; |
434 | 434 | } |
435 | 435 |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | ]; |
71 | 71 | |
72 | 72 | $requestData = ProtocolTool::encode(ProtocolTool::PRODUCE_REQUEST, $params); |
73 | - rgo(function () use ($connect, $requestData, $requiredAck, $callback) { |
|
73 | + rgo(function() use ($connect, $requestData, $requiredAck, $callback) { |
|
74 | 74 | if ($requiredAck !== 0) { |
75 | 75 | $connect->send($requestData); |
76 | 76 | $dataLen = Protocol::unpack(Protocol::BIT_B32, $connect->recv(4)); |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | public function syncMeta(): void |
94 | 94 | { |
95 | 95 | $socket = $this->pool->getConnection(); |
96 | - rgo(function () use ($socket) { |
|
96 | + rgo(function() use ($socket) { |
|
97 | 97 | while (true) { |
98 | 98 | try { |
99 | 99 | $params = []; |
@@ -52,15 +52,15 @@ discard block |
||
52 | 52 | } |
53 | 53 | |
54 | 54 | $header = $this->requestHeader('seaslog-kafka', 0, self::PRODUCE_REQUEST); |
55 | - $data = self::pack(self::BIT_B16, (string)($payloads['required_ack'] ?? 0)); |
|
56 | - $data .= self::pack(self::BIT_B32, (string)($payloads['timeout'] ?? 100)); |
|
55 | + $data = self::pack(self::BIT_B16, ( string ) ($payloads['required_ack'] ?? 0)); |
|
56 | + $data .= self::pack(self::BIT_B32, ( string ) ($payloads['timeout'] ?? 100)); |
|
57 | 57 | $data .= self::encodeArray( |
58 | 58 | $payloads['data'], |
59 | 59 | [$this, 'encodeProduceTopic'], |
60 | 60 | $payloads['compression'] ?? self::COMPRESSION_NONE |
61 | 61 | ); |
62 | 62 | |
63 | - return self::encodeString($header . $data, self::PACK_INT32); |
|
63 | + return self::encodeString($header.$data, self::PACK_INT32); |
|
64 | 64 | } |
65 | 65 | |
66 | 66 | /** |
@@ -101,9 +101,9 @@ discard block |
||
101 | 101 | throw new ProtocolException('given produce data invalid. `messages` is undefined.'); |
102 | 102 | } |
103 | 103 | |
104 | - $data = self::pack(self::BIT_B32, (string)$values['partition_id']); |
|
104 | + $data = self::pack(self::BIT_B32, ( string ) $values['partition_id']); |
|
105 | 105 | $data .= self::encodeString( |
106 | - $this->encodeMessageSet((array)$values['messages'], $compression), |
|
106 | + $this->encodeMessageSet(( array ) $values['messages'], $compression), |
|
107 | 107 | self::PACK_INT32 |
108 | 108 | ); |
109 | 109 | |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | foreach ($messages as $message) { |
128 | 128 | $encodedMessage = $this->encodeMessage($message); |
129 | 129 | |
130 | - $data .= self::pack(self::BIT_B64, (string)$next) |
|
130 | + $data .= self::pack(self::BIT_B64, ( string ) $next) |
|
131 | 131 | . self::encodeString($encodedMessage, self::PACK_INT32); |
132 | 132 | |
133 | 133 | ++$next; |
@@ -151,8 +151,8 @@ discard block |
||
151 | 151 | $magic = $this->computeMagicByte(); |
152 | 152 | $attributes = $this->computeAttributes($magic, $compression, $this->computeTimestampType($magic)); |
153 | 153 | |
154 | - $data = self::pack(self::BIT_B8, (string)$magic); |
|
155 | - $data .= self::pack(self::BIT_B8, (string)$attributes); |
|
154 | + $data = self::pack(self::BIT_B8, ( string ) $magic); |
|
155 | + $data .= self::pack(self::BIT_B8, ( string ) $attributes); |
|
156 | 156 | |
157 | 157 | if ($magic >= self::MESSAGE_MAGIC_VERSION1) { |
158 | 158 | $data .= self::pack(self::BIT_B64, $this->clock->now()->format('Uv')); |
@@ -171,10 +171,10 @@ discard block |
||
171 | 171 | // message value |
172 | 172 | $data .= self::encodeString($message, self::PACK_INT32, $compression); |
173 | 173 | |
174 | - $crc = (string)crc32($data); |
|
174 | + $crc = ( string ) crc32($data); |
|
175 | 175 | |
176 | 176 | // int32 -- crc code string data |
177 | - $message = self::pack(self::BIT_B32, $crc) . $data; |
|
177 | + $message = self::pack(self::BIT_B32, $crc).$data; |
|
178 | 178 | |
179 | 179 | return $message; |
180 | 180 | } |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | $topic = self::encodeString($values['topic_name'], self::PACK_INT16); |
238 | 238 | $partitions = self::encodeArray($values['partitions'], [$this, 'encodeProducePartition'], $compression); |
239 | 239 | |
240 | - return $topic . $partitions; |
|
240 | + return $topic.$partitions; |
|
241 | 241 | } |
242 | 242 | |
243 | 243 | /** |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | public function __construct(array $configs) |
65 | 65 | { |
66 | 66 | foreach ($configs as $name => $value) { |
67 | - $method = 'set' . ucfirst($name); |
|
67 | + $method = 'set'.ucfirst($name); |
|
68 | 68 | $this->$method($value); |
69 | 69 | } |
70 | 70 | } |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | |
153 | 153 | $brokers = array_filter( |
154 | 154 | explode(',', $brokerList), |
155 | - function (string $broker): bool { |
|
155 | + function(string $broker): bool { |
|
156 | 156 | return preg_match('/^(.*:[\d]+)$/', $broker) === 1; |
157 | 157 | } |
158 | 158 | ); |
@@ -60,8 +60,8 @@ |
||
60 | 60 | $maxRetry = $this->config['retry']; |
61 | 61 | $reconnectCount = 0; |
62 | 62 | while (true) { |
63 | - $isConnect = $this->config['timeout'] ? $client->connect($host, (int)$port, |
|
64 | - $this->config['timeout']) : $client->connect($host, (int)$port); |
|
63 | + $isConnect = $this->config['timeout'] ? $client->connect($host, ( int ) $port, |
|
64 | + $this->config['timeout']) : $client->connect($host, ( int ) $port); |
|
65 | 65 | if (!$isConnect) { |
66 | 66 | $reconnectCount++; |
67 | 67 | if ($maxRetry > 0 && $reconnectCount >= $maxRetry) { |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | public static function encode(int $key, array $payloads): string |
44 | 44 | { |
45 | 45 | if (!isset(self::$objects[$key])) { |
46 | - throw new NotSupportedException('Not support api key, key:' . $key); |
|
46 | + throw new NotSupportedException('Not support api key, key:'.$key); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | return self::$objects[$key]->encode($payloads); |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | public static function decode(int $key, string $data): array |
59 | 59 | { |
60 | 60 | if (!isset(self::$objects[$key])) { |
61 | - throw new NotSupportedException('Not support api key, key:' . $key); |
|
61 | + throw new NotSupportedException('Not support api key, key:'.$key); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | return self::$objects[$key]->decode($data); |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | $brokers = []; |
58 | 58 | |
59 | 59 | foreach ($brokersResult as $value) { |
60 | - $brokers[$value['nodeId']] = $value['host'] . ':' . $value['port']; |
|
60 | + $brokers[$value['nodeId']] = $value['host'].':'.$value['port']; |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | $changed = false; |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | |
71 | 71 | $newTopics = []; |
72 | 72 | foreach ($topics as $topic) { |
73 | - if ((int)$topic['errorCode'] !== Protocol::NO_ERROR) { |
|
73 | + if (( int ) $topic['errorCode'] !== Protocol::NO_ERROR) { |
|
74 | 74 | continue; |
75 | 75 | } |
76 | 76 |