Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions src/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions src/Support/UserAgent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);
/**
* This file is part of jet.
*
* @link https://github.com/friendsofhyperf/jet
* @document https://github.com/friendsofhyperf/jet/blob/main/README.md
* @contact huangdijia@gmail.com
* @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE
*/

namespace FriendsOfHyperf\Jet\Support;

use FriendsOfHyperf\Jet\Client;
use GuzzleHttp\ClientInterface;

class UserAgent
{
/**
* @var null|string
*/
protected static $value;

public static function set(string $value): void
{
self::$value = $value;
}

public static function get(): string
{
return self::$value ?: 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']
);
}
}
4 changes: 2 additions & 2 deletions src/Transporter/GuzzleHttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace FriendsOfHyperf\Jet\Transporter;

use FriendsOfHyperf\Jet\ClientFactory;
use FriendsOfHyperf\Jet\Support\UserAgent;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\RequestOptions;
Expand Down Expand Up @@ -43,7 +43,7 @@ public function __construct(string $host = '', int $port = 9501, array $config =
'Content-Type' => '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,
]);
Expand Down