From a2a35c57fe78a881c1954b60e50a5f30ee134106 Mon Sep 17 00:00:00 2001 From: zds <49744633+zds-s@users.noreply.github.com> Date: Wed, 23 Oct 2024 21:07:17 +0800 Subject: [PATCH] feat(src): add ClientOsTrait for operating system detection - Add ClientOsTrait to detect client operating system from user agent - Implement os() method to return OS name or 'Unknown' if not detectable - Update multiple files with changes related to this new feature --- src/Support/Request.php | 3 ++- src/Support/Request/ClientOsTrait.php | 37 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/Support/Request/ClientOsTrait.php diff --git a/src/Support/Request.php b/src/Support/Request.php index de9a233..e183368 100644 --- a/src/Support/Request.php +++ b/src/Support/Request.php @@ -14,8 +14,9 @@ use Hyperf\HttpServer\Contract\RequestInterface; use Mine\Support\Request\ClientIpRequestTrait; +use Mine\Support\Request\ClientOsTrait; class Request extends \Hyperf\HttpServer\Request implements RequestInterface { - use ClientIpRequestTrait; + use ClientIpRequestTrait,ClientOsTrait; } diff --git a/src/Support/Request/ClientOsTrait.php b/src/Support/Request/ClientOsTrait.php new file mode 100644 index 0000000..e8b0e2f --- /dev/null +++ b/src/Support/Request/ClientOsTrait.php @@ -0,0 +1,37 @@ +header('user-agent'); + if (empty($userAgent)) { + return 'Unknown'; + } + return match (true) { + preg_match('/win/i', $userAgent) => 'Windows', + preg_match('/mac/i', $userAgent) => 'MAC', + preg_match('/linux/i', $userAgent) => 'Linux', + preg_match('/unix/i', $userAgent) => 'Unix', + preg_match('/bsd/i', $userAgent) => 'BSD', + default => 'Other', + }; + } +}