From 17126c0f9ed364b6c73b7676724c62d62a45a56f Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 16:05:33 +0800 Subject: [PATCH] Split UserAgent --- src/ClientFactory.php | 29 ---------------- src/Support/UserAgent.php | 40 +++++++++++++++++++++++ src/Transporter/GuzzleHttpTransporter.php | 4 +-- 3 files changed, 42 insertions(+), 31 deletions(-) create mode 100644 src/Support/UserAgent.php diff --git a/src/ClientFactory.php b/src/ClientFactory.php index 8792bef..efb28ac 100644 --- a/src/ClientFactory.php +++ b/src/ClientFactory.php @@ -16,38 +16,9 @@ use FriendsOfHyperf\Jet\Contract\PackerInterface; use FriendsOfHyperf\Jet\Contract\PathGeneratorInterface; use FriendsOfHyperf\Jet\Contract\TransporterInterface; -use GuzzleHttp\ClientInterface; class ClientFactory { - /** - * User agent. - * @var string - */ - protected static $userAgent; - - /** - * Set user agent. - */ - public static function setUserAgent(string $userAgent): void - { - self::$userAgent = $userAgent; - } - - /** - * Get user agent. - */ - public static function getUserAgent(): string - { - return self::$userAgent ?: sprintf( - 'jet/%s php/%s guzzle/%s curl/%s', - Client::MAJOR_VERSION, - PHP_VERSION, - defined(ClientInterface::class . '::VERSION') ? constant(ClientInterface::class . '::VERSION') : constant(ClientInterface::class . '::MAJOR_VERSION'), - curl_version()['version'] - ); - } - /** * Create a client. * @param null|int|string|TransporterInterface $transporter transporter, protocol, timeout or null diff --git a/src/Support/UserAgent.php b/src/Support/UserAgent.php new file mode 100644 index 0000000..8313c2d --- /dev/null +++ b/src/Support/UserAgent.php @@ -0,0 +1,40 @@ + 'application/json', 'X-Real-Ip' => $_SERVER['SERVER_ADDR'] ?? '', 'X-Forwarded-For' => $_SERVER['REMOTE_ADDR'] ?? '', - 'User-Agent' => ClientFactory::getUserAgent(), + 'User-Agent' => UserAgent::get(), ], 'http_errors' => false, ]);