Skip to content

Commit 4312b3e

Browse files
committed
update: 标准化Guzzle/Client
1 parent 4897eb5 commit 4312b3e

6 files changed

Lines changed: 161 additions & 120 deletions

File tree

src/Client.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,40 @@
3636
*/
3737
class Client
3838
{
39+
/**
40+
* @var Client
41+
*/
42+
private static Client $instance;
43+
3944
/*** @var ConnectionPool|null */
4045
private ConnectionPool|null $connectionPool = null;
4146

4247
/*** @var bool */
4348
private bool $pool;
4449

4550
/*** @param array $config */
46-
public function __construct(private readonly array $config = [])
51+
private function __construct(private readonly array $config = [])
4752
{
48-
$pool = $this->config['pool'] ?? 'off';
53+
$pool = $this->config['enable_connection_pool'] ?? false;
4954
$this->pool = in_array($pool, [true, 1, 'on'], true);
5055
if ($this->pool) {
5156
$this->connectionPool = new ConnectionPool();
5257
}
5358
}
5459

60+
/**
61+
* @param array $config
62+
* @return Client
63+
*/
64+
public static function instance(array $config = []): Client
65+
{
66+
if (!isset(self::$instance)) {
67+
self::$instance = new Client($config);
68+
}
69+
70+
return self::$instance;
71+
}
72+
5573
/**
5674
* @param RequestInterface $request
5775
* @param array $option

src/Guzzle.php

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/Guzzle/Client.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Ripple\Http\Guzzle;
4+
5+
use GuzzleHttp\ClientInterface;
6+
use GuzzleHttp\ClientTrait;
7+
use GuzzleHttp\Exception\GuzzleException;
8+
use GuzzleHttp\Promise\PromiseInterface;
9+
use Psr\Http\Client\ClientExceptionInterface;
10+
use Psr\Http\Message\RequestInterface;
11+
use Psr\Http\Message\ResponseInterface;
12+
13+
use function array_merge;
14+
use function call_user_func_array;
15+
16+
/**
17+
*
18+
*/
19+
class Client implements ClientInterface, \Psr\Http\Client\ClientInterface
20+
{
21+
use ClientTrait;
22+
23+
/**
24+
* @var \GuzzleHttp\Client
25+
*/
26+
private \GuzzleHttp\Client $guzzleClient;
27+
28+
/**
29+
* @param array $config
30+
*/
31+
public function __construct(array $config = [])
32+
{
33+
$this->guzzleClient = new \GuzzleHttp\Client(array_merge(
34+
[
35+
'handler' => new RippleInvoke()
36+
],
37+
$config
38+
));
39+
}
40+
41+
/**
42+
* @param RequestInterface $request
43+
* @param array $options
44+
* @return ResponseInterface
45+
* @throws GuzzleException
46+
*/
47+
public function send(RequestInterface $request, array $options = []): ResponseInterface
48+
{
49+
return $this->guzzleClient->send($request, $options);
50+
}
51+
52+
/**
53+
* @param RequestInterface $request
54+
* @param array $options
55+
* @return PromiseInterface
56+
*/
57+
public function sendAsync(RequestInterface $request, array $options = []): PromiseInterface
58+
{
59+
return $this->guzzleClient->sendAsync($request, $options);
60+
}
61+
62+
/**
63+
* @param string $method
64+
* @param $uri
65+
* @param array $options
66+
* @return ResponseInterface
67+
* @throws GuzzleException
68+
*/
69+
public function request(string $method, $uri, array $options = []): ResponseInterface
70+
{
71+
return $this->guzzleClient->request($method, $uri, $options);
72+
}
73+
74+
/**
75+
* @param string $method
76+
* @param $uri
77+
* @param array $options
78+
* @return PromiseInterface
79+
*/
80+
public function requestAsync(string $method, $uri, array $options = []): PromiseInterface
81+
{
82+
return $this->guzzleClient->requestAsync($method, $uri, $options);
83+
}
84+
85+
/**
86+
* @param RequestInterface $request
87+
* @return ResponseInterface
88+
* @throws ClientExceptionInterface
89+
*/
90+
public function sendRequest(RequestInterface $request): ResponseInterface
91+
{
92+
return $this->guzzleClient->sendRequest($request);
93+
}
94+
95+
/**
96+
* @param string|null $option
97+
* @return void
98+
*/
99+
public function getConfig(?string $option = null): void
100+
{
101+
$this->guzzleClient->getConfig($option);
102+
}
103+
104+
/**
105+
* @param string $name
106+
* @param array $arguments
107+
* @return mixed
108+
*/
109+
public function __call(string $name, array $arguments)
110+
{
111+
return call_user_func_array([$this->guzzleClient, $name], $arguments);
112+
}
113+
}
Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@
2020
use Ripple\Http\Client;
2121
use Throwable;
2222

23+
use const PHP_SAPI;
24+
2325
/**
2426
*
2527
*/
26-
class RippleHandler
28+
class RippleInvoke
2729
{
2830
/**
29-
* @param Client $httpClient
31+
* @param bool $enableConnectionPool
3032
*/
31-
public function __construct(private readonly Client $httpClient)
33+
public function __construct(public readonly bool $enableConnectionPool = PHP_SAPI === 'cli')
3234
{
3335
}
3436

@@ -40,27 +42,19 @@ public function __construct(private readonly Client $httpClient)
4042
*/
4143
public function __invoke(RequestInterface $request, array $options): PromiseInterface
4244
{
43-
$promise = new Promise(function () use ($request, $options, &$promise) {
44-
try {
45-
$response = $this->httpClient->request($request, $options);
46-
$promise->resolve($response);
47-
} catch (GuzzleException $exception) {
48-
$promise->reject($exception);
49-
} catch (Throwable $exception) {
50-
$promise->reject(new TransferException($exception->getMessage()));
51-
}
52-
});
45+
$promise = new Promise();
5346

54-
return $promise;
55-
}
47+
try {
48+
$response = Client::instance([
49+
'enable_connection_pool' => $this->enableConnectionPool
50+
])->request($request, $options);
51+
$promise->resolve($response);
52+
} catch (GuzzleException $exception) {
53+
$promise->reject($exception);
54+
} catch (Throwable $exception) {
55+
$promise->reject(new TransferException($exception->getMessage()));
56+
}
5657

57-
/**
58-
* @Author cclilshy
59-
* @Date 2024/8/31 14:31
60-
* @return Client
61-
*/
62-
public function getHttpClient(): Client
63-
{
64-
return $this->httpClient;
58+
return $promise;
6559
}
6660
}

tests/HttpAsyncTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use GuzzleHttp\Exception\GuzzleException;
44
use PHPUnit\Framework\Attributes\Test;
55
use PHPUnit\Framework\TestCase;
6-
use Ripple\Http\Guzzle;
6+
use Ripple\Http\Guzzle\Client;
77
use Ripple\Http\Server\Request;
88
use Ripple\Stream\Exception\ConnectionException;
99

@@ -45,7 +45,7 @@ public function testStreamDownload(): void
4545
});
4646
$server->listen();
4747
$tempFile = \tempnam(\sys_get_temp_dir(), 'download');
48-
$client = Guzzle::newClient();
48+
$client = new Client();
4949
$client->get($this->testServer, [
5050
'sink' => $tempFile,
5151
]);
@@ -107,9 +107,7 @@ public function testSlowBandwidthTransfer(): void
107107
$server->listen();
108108
$tempFile = \tempnam(\sys_get_temp_dir(), 'slow_bandwidth_test');
109109

110-
$client = Guzzle::newClient([
111-
'timeout' => 0,
112-
]);
110+
$client = new Client(['timeout' => 0]);
113111

114112
$response = $client->get($this->testServer, [
115113
'sink' => $tempFile,

tests/HttpTest.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
use GuzzleHttp\Exception\GuzzleException;
55
use PHPUnit\Framework\Attributes\Test;
66
use PHPUnit\Framework\TestCase;
7-
use Ripple\Http\Guzzle;
7+
use Ripple\Http\Client;
88
use Ripple\Http\Server\Request;
99
use Ripple\Promise;
1010
use Ripple\Utils\Output;
1111
use Symfony\Component\HttpFoundation\File\UploadedFile;
12+
use Ripple\Http\Guzzle\Client as GuzzleClient;
1213

1314
use function Co\async;
1415
use function Co\cancelAll;
@@ -123,10 +124,7 @@ public function test_httpServer(): void
123124
}
124125
}
125126

126-
Guzzle::getInstance()
127-
->getHttpClient()
128-
->getConnectionPool()
129-
->clearConnectionPool();
127+
Client::instance()->getConnectionPool()->clearConnectionPool();
130128
\gc_collect_cycles();
131129

132130
if ($baseMemory !== \memory_get_usage()) {
@@ -144,7 +142,7 @@ public function test_httpServer(): void
144142
private function httpGet(): void
145143
{
146144
$hash = \md5(\uniqid());
147-
$client = Guzzle::newClient();
145+
$client = new GuzzleClient();
148146
$response = $client->get('http://127.0.0.1:8008/', [
149147
'query' => [
150148
'query' => $hash,
@@ -162,7 +160,7 @@ private function httpGet(): void
162160
private function httpPost(): void
163161
{
164162
$hash = \md5(\uniqid());
165-
$client = Guzzle::newClient();
163+
$client = new GuzzleClient();
166164
$response = $client->post('http://127.0.0.1:8008/', [
167165
'json' => [
168166
'query' => $hash,
@@ -179,7 +177,7 @@ private function httpPost(): void
179177
*/
180178
private function httpFile(): void
181179
{
182-
$client = Guzzle::newClient();
180+
$client = new GuzzleClient();
183181
$path = \tempnam(\sys_get_temp_dir(), 'test');
184182
\file_put_contents($path, \str_repeat('a', 81920));
185183
$hash = \md5_file($path);
@@ -211,7 +209,7 @@ private function httpLargePost(): void
211209
{
212210
$largeData = \str_repeat('a', 1024 * 1024); // 1MB of data
213211
$hash = \md5($largeData);
214-
$client = Guzzle::newClient();
212+
$client = new GuzzleClient();
215213
$response = $client->post('http://127.0.0.1:8008/', [
216214
'json' => [
217215
'query' => $largeData
@@ -254,7 +252,7 @@ private function httpClient(): void
254252
foreach ($urls as $url) {
255253
$list[] = async(function () use ($url) {
256254
try {
257-
return [$url, Guzzle::newClient()->get($url, ['timeout' => 10])];
255+
return [$url, (new GuzzleClient())->get($url, ['timeout' => 10])];
258256
} catch (Throwable $exception) {
259257
return [$url, $exception];
260258
}

0 commit comments

Comments
 (0)