Skip to content

Commit 5fe4d89

Browse files
committed
update
1 parent 421a21e commit 5fe4d89

24 files changed

+620
-647
lines changed

src/Constants/Capabilities.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
enum Capabilities: int
88
{
9+
use EnumTraits;
10+
911
// new more secure passwords
1012
case CLIENT_LONG_PASSWORD = 1;
1113
// Found instead of affected rows

src/Constants/EnumTraits.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Workbunny\MysqlProtocol\Constants;
6+
7+
trait EnumTraits
8+
{
9+
/**
10+
* 获取名称
11+
*
12+
* @param $value
13+
* @return string|null
14+
*/
15+
public static function getName($value): ?string
16+
{
17+
if ($enum = self::tryFrom($value)) {
18+
return ucwords(strtolower(str_replace('_', ' ', $enum->name)));
19+
}
20+
return null;
21+
}
22+
}

src/Constants/Errors.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
enum Errors : int
88
{
9+
use EnumTraits;
10+
911
// cobar errorMessage code
1012
case ERR_OPEN_SOCKET = 3001;
1113
case ERR_CONNECT_SOCKET = 3002;

src/Constants/ExceptionCode.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Workbunny\MysqlProtocol\Constants;
6+
7+
enum ExceptionCode : int
8+
{
9+
use EnumTraits;
10+
11+
case ERROR = 0;
12+
case ERROR_VALUE = 1;
13+
case ERROR_TYPE = 2;
14+
15+
case ERROR_CURSOR = 3;
16+
17+
case ERROR_SUPPORT = 4;
18+
}

src/Exceptions/Exception.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Workbunny\MysqlProtocol\Exceptions;
6+
7+
use Throwable;
8+
use Workbunny\MysqlProtocol\Constants\ExceptionCode;
9+
10+
class Exception extends \RuntimeException
11+
{
12+
/** @var ExceptionCode */
13+
protected ExceptionCode $errorCode;
14+
15+
/**
16+
* @param string $message
17+
* @param ExceptionCode $code
18+
* @param Throwable|null $previous
19+
*/
20+
public function __construct(string $message, ExceptionCode $code = ExceptionCode::ERROR, ?Throwable $previous = null)
21+
{
22+
$this->errorCode = $code;
23+
parent::__construct("[{$code->getName($code->value)}] $message", $code->value, $previous);
24+
}
25+
26+
/**
27+
* @return ExceptionCode
28+
*/
29+
public function getErrorCode(): ExceptionCode
30+
{
31+
return $this->errorCode;
32+
}
33+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Workbunny\MysqlProtocol\Exceptions;
6+
7+
class InvalidArgumentException extends Exception
8+
{
9+
}

src/Exceptions/PacketException.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Workbunny\MysqlProtocol\Exceptions;
66

7-
class PacketException extends \RuntimeException
7+
class PacketException extends Exception
88
{
9-
109
}

src/Packets/AuthMoreDataRequest.php

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Workbunny\MysqlProtocol\Packets;
66

7+
use Workbunny\MysqlProtocol\Constants\ExceptionCode;
78
use Workbunny\MysqlProtocol\Exceptions\PacketException;
89
use Workbunny\MysqlProtocol\Utils\Binary;
910
use Workbunny\MysqlProtocol\Utils\Packet;
@@ -32,29 +33,24 @@ class AuthMoreDataRequest implements PacketInterface
3233
*/
3334
public static function unpack(Binary $binary): array
3435
{
35-
try {
36-
return Packet::parser(function (Binary $binary) {
37-
// 读取第 1 个字节作为标志
38-
$flag = $binary->readByte();
39-
if ($flag !== self::PACKET_FLAG) {
40-
throw new PacketException("Error: Invalid packet flag '$flag', expected 0x01");
41-
}
42-
// 如果后续有附加数据,则读取之
43-
$remainingLength = $binary->length() - $binary->getReadCursor();
44-
$extraData = '';
45-
if ($remainingLength > 0) {
46-
$extraData = Binary::BytesToString($binary->readBytes($remainingLength));
47-
}
48-
49-
return [
50-
'flag' => $flag,
51-
'extra_data' => $extraData,
52-
];
53-
}, $binary);
54-
} catch (\InvalidArgumentException $e) {
55-
throw new PacketException("Error: Failed to unpack auth more data request packet [{$e->getMessage()}]", $e->getCode(), $e);
56-
}
36+
return Packet::parser(function (Binary $binary) {
37+
// 读取第 1 个字节作为标志
38+
$flag = $binary->readByte();
39+
if ($flag !== self::PACKET_FLAG) {
40+
throw new PacketException("Invalid packet flag '$flag', expected 0x01", ExceptionCode::ERROR_VALUE);
41+
}
42+
// 如果后续有附加数据,则读取之
43+
$remainingLength = $binary->length() - $binary->getReadCursor();
44+
$extraData = '';
45+
if ($remainingLength > 0) {
46+
$extraData = Binary::BytesToString($binary->readBytes($remainingLength));
47+
}
5748

49+
return [
50+
'flag' => $flag,
51+
'extra_data' => $extraData,
52+
];
53+
}, $binary);
5854
}
5955

6056
/**
@@ -72,17 +68,12 @@ public static function unpack(Binary $binary): array
7268
public static function pack(array $data): Binary
7369
{
7470
$packetId = $data['packet_id'] ?? 0;
75-
76-
try {
77-
return Packet::binary(function (Binary $binary) use ($data) {
78-
$extraData = $data['extra_data'] ?? null;
79-
$binary->writeByte(self::PACKET_FLAG);
80-
if ($extraData) {
81-
$binary->writeBytes(Binary::StringToBytes($extraData));
82-
}
83-
}, $packetId);
84-
} catch (\InvalidArgumentException $e) {
85-
throw new PacketException("Error: Failed to pack auth more data request packet [{$e->getMessage()}]", $e->getCode(), $e);
86-
}
71+
return Packet::binary(function (Binary $binary) use ($data) {
72+
$extraData = $data['extra_data'] ?? null;
73+
$binary->writeByte(self::PACKET_FLAG);
74+
if ($extraData) {
75+
$binary->writeBytes(Binary::StringToBytes($extraData));
76+
}
77+
}, $packetId);
8778
}
8879
}

src/Packets/AuthMoreDataResponse.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Workbunny\MysqlProtocol\Packets;
66

7-
use Workbunny\MysqlProtocol\Exceptions\PacketException;
87
use Workbunny\MysqlProtocol\Utils\Binary;
98
use Workbunny\MysqlProtocol\Utils\Packet;
109

@@ -25,17 +24,13 @@ class AuthMoreDataResponse implements PacketInterface
2524
*/
2625
public static function unpack(Binary $binary): array
2726
{
28-
try {
29-
return Packet::parser(function (Binary $binary) {
30-
$remainingLength = $binary->length() - $binary->getReadCursor();
31-
$responseBytes = $binary->readBytes($remainingLength);
32-
return [
33-
'auth_response' => Binary::BytesToString($responseBytes),
34-
];
35-
}, $binary);
36-
} catch (\InvalidArgumentException $e) {
37-
throw new PacketException("Error: Failed to unpack auth more data response packet [{$e->getMessage()}]", $e->getCode(), $e);
38-
}
27+
return Packet::parser(function (Binary $binary) {
28+
$remainingLength = $binary->length() - $binary->getReadCursor();
29+
$responseBytes = $binary->readBytes($remainingLength);
30+
return [
31+
'auth_response' => Binary::BytesToString($responseBytes),
32+
];
33+
}, $binary);
3934
}
4035

4136
/**
@@ -50,13 +45,9 @@ public static function unpack(Binary $binary): array
5045
public static function pack(array $data): Binary
5146
{
5247
$packetId = $data['packet_id'] ?? 0;
53-
try {
54-
return Packet::binary(function (Binary $binary) use ($data) {
55-
$authResponse = $data['auth_response'] ?? '';
56-
$binary->writeBytes(Binary::StringToBytes($authResponse));
57-
}, $packetId);
58-
} catch (\InvalidArgumentException $e) {
59-
throw new PacketException("Error: Failed to pack auth more data response packet [{$e->getMessage()}]", $e->getCode(), $e);
60-
}
48+
return Packet::binary(function (Binary $binary) use ($data) {
49+
$authResponse = $data['auth_response'] ?? '';
50+
$binary->writeBytes(Binary::StringToBytes($authResponse));
51+
}, $packetId);
6152
}
6253
}

src/Packets/AuthSwitchRequest.php

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace Workbunny\MysqlProtocol\Packets;
66

7+
use Workbunny\MysqlProtocol\Constants\ExceptionCode;
78
use Workbunny\MysqlProtocol\Exceptions\PacketException;
89
use Workbunny\MysqlProtocol\Utils\Binary;
9-
use InvalidArgumentException;
1010
use Workbunny\MysqlProtocol\Utils\Packet;
1111

1212
class AuthSwitchRequest implements PacketInterface
@@ -21,61 +21,50 @@ class AuthSwitchRequest implements PacketInterface
2121
* @param Binary $binary
2222
* @return array 解析后的数据,包含:
2323
* - plugin_name: string
24-
* - auth_plugin_data: string(可能为空
24+
* - auth_plugin_data: array(字节组
2525
*/
2626
public static function unpack(Binary $binary): array
2727
{
28-
try {
29-
return Packet::parser(function (Binary $binary) {
30-
// 读取 1 个字节并验证标志必须为 0xFE
31-
$flag = $binary->readByte();
32-
if ($flag !== self::PACKET_FLAG) {
33-
throw new InvalidArgumentException("Error: Invalid packet flag '$flag', expected 0xFE");
34-
}
35-
// 读取 NULL 终止的认证插件名称
36-
$pluginName = Binary::BytesToString($binary->readNullTerminated());
37-
// 剩下的所有数据为附加的认证数据(可以为空)
38-
$remainingLength = $binary->length() - $binary->getReadCursor();
39-
$authPluginDataBytes = $binary->readBytes($remainingLength);
40-
$authPluginData = Binary::BytesToString($authPluginDataBytes);
41-
return [
42-
'flag' => $flag,
43-
'plugin_name' => $pluginName,
44-
'auth_plugin_data' => $authPluginData,
45-
];
46-
}, $binary);
47-
} catch (InvalidArgumentException $e) {
48-
throw new PacketException("Error: Failed to unpack auth switch request packet [{$e->getMessage()}]", $e->getCode(), $e);
49-
}
28+
return Packet::parser(function (Binary $binary) {
29+
// 读取 1 个字节并验证标志必须为 0xFE
30+
$flag = $binary->readByte();
31+
if ($flag !== self::PACKET_FLAG) {
32+
throw new PacketException("Invalid packet flag '$flag', expected 0xFE", ExceptionCode::ERROR_VALUE);
33+
}
34+
// 读取 NULL 终止的认证插件名称
35+
$pluginName = Binary::BytesToString($binary->readNullTerminated());
36+
// 剩下的所有数据为附加的认证数据(可以为空)
37+
$remainingLength = $binary->length() - $binary->getReadCursor();
38+
$authPluginData = $binary->readBytes($remainingLength);
39+
return [
40+
'flag' => $flag,
41+
'plugin_name' => $pluginName,
42+
'auth_plugin_data' => $authPluginData,
43+
];
44+
}, $binary);
5045
}
5146

5247
/**
5348
* 将 AuthSwitchRequest 数据包内容封装为 Binary 对象。
5449
*
5550
* @param array $data 数组包含:
5651
* - plugin_name: 认证插件名称(字符串,必填)
57-
* - auth_plugin_data: 附加认证数据(字符串,可选)
52+
* - auth_plugin_data: 附加认证数据(字节组,可选)
5853
* @return Binary
5954
*/
6055
public static function pack(array $data): Binary
6156
{
62-
$packetId = $data['packet_id'] ?? 0;
63-
64-
try {
65-
return Packet::binary(function (Binary $binary) use ($data) {
66-
$pluginName = $data['plugin_name'] ?? '';
67-
$authPluginData = $data['auth_plugin_data'] ?? null;
68-
// 写入标志字节 0xFE
69-
$binary->writeByte(self::PACKET_FLAG);
70-
// 写入认证插件名称(字符串转换成字节数组)及 NULL 终止符
71-
$binary->writeNullTerminated(Binary::StringToBytes($pluginName));
72-
// 如果附加认证数据存在,则写入
73-
if ($authPluginData) {
74-
$binary->writeBytes(Binary::StringToBytes($authPluginData));
75-
}
76-
}, $packetId);
77-
} catch (InvalidArgumentException $e) {
78-
throw new PacketException("Error: Failed to pack auth switch request packet [{$e->getMessage()}]", $e->getCode(), $e);
79-
}
57+
return Packet::binary(function (Binary $binary) use ($data) {
58+
$pluginName = $data['plugin_name'] ?? '';
59+
$authPluginData = $data['auth_plugin_data'] ?? [];
60+
// 写入标志字节 0xFE
61+
$binary->writeByte(self::PACKET_FLAG);
62+
// 写入认证插件名称(字符串转换成字节数组)及 NULL 终止符
63+
$binary->writeNullTerminated(Binary::StringToBytes($pluginName));
64+
// 如果附加认证数据存在,则写入
65+
if ($authPluginData and is_array($authPluginData)) {
66+
$binary->writeBytes($authPluginData);
67+
}
68+
}, $data['packet_id'] ?? 0);
8069
}
8170
}

0 commit comments

Comments
 (0)