Skip to content

Commit

Permalink
feat(src): add ClientOsTrait for operating system detection
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
zds-s committed Oct 23, 2024
1 parent 8665f29 commit a2a35c5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Support/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
37 changes: 37 additions & 0 deletions src/Support/Request/ClientOsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact [email protected]
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/

namespace Mine\Support\Request;

use Mine\Support\Request;

/**
* @mixin Request
*/
trait ClientOsTrait
{
public function os(): string
{
$userAgent = $this->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',
};
}
}

0 comments on commit a2a35c5

Please sign in to comment.