Skip to content
This repository was archived by the owner on Aug 19, 2026. It is now read-only.

Commit 018602c

Browse files
abnegateclaude
andcommitted
Add constructor config options for HTTP client customization
- Curl: Accept array of CURLOPT_* options in constructor - Swoole: Accept array of client settings in constructor - Config options are merged with defaults (user config takes precedence) - Extracted buildClientSettings() helper in Swoole adapter Example usage: // Curl with custom SSL options new Curl([CURLOPT_SSL_VERIFYPEER => false]); // Swoole with long connections new Swoole(['keep_alive' => true, 'timeout' => 60]); Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent cbacfea commit 018602c

2 files changed

Lines changed: 54 additions & 10 deletions

File tree

src/Adapter/Curl.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ class Curl implements Adapter
1919
{
2020
private ?CurlHandle $handle = null;
2121

22+
/**
23+
* @var array<int, mixed>
24+
*/
25+
private array $config;
26+
27+
/**
28+
* Create a new Curl adapter
29+
*
30+
* @param array<int, mixed> $config Custom cURL options (CURLOPT_* constants as keys)
31+
*/
32+
public function __construct(array $config = [])
33+
{
34+
$this->config = $config;
35+
}
36+
2237
/**
2338
* Get or create the cURL handle
2439
*
@@ -102,6 +117,9 @@ public function send(
102117
$curlOptions[CURLOPT_POSTFIELDS] = $body;
103118
}
104119

120+
// Merge custom config (user config takes precedence)
121+
$curlOptions = $this->config + $curlOptions;
122+
105123
foreach ($curlOptions as $option => $value) {
106124
curl_setopt($ch, $option, $value);
107125
}

src/Adapter/Swoole.php

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ class Swoole implements Adapter
2525
*/
2626
private array $clients = [];
2727

28+
/**
29+
* @var array<string, mixed>
30+
*/
31+
private array $config;
32+
33+
/**
34+
* Create a new Swoole adapter
35+
*
36+
* @param array<string, mixed> $config Custom Swoole client options (passed to $client->set())
37+
*/
38+
public function __construct(array $config = [])
39+
{
40+
$this->config = $config;
41+
}
42+
2843
/**
2944
* Check if Swoole is available
3045
*
@@ -123,6 +138,25 @@ private function configureBody(SwooleClient $client, mixed $body, array $headers
123138
}
124139
}
125140

141+
/**
142+
* Build client settings by merging defaults with custom config
143+
*
144+
* @param float $timeout
145+
* @param float $connectTimeout
146+
* @return array<string, mixed>
147+
*/
148+
private function buildClientSettings(float $timeout, float $connectTimeout): array
149+
{
150+
$defaults = [
151+
'timeout' => $timeout,
152+
'connect_timeout' => $connectTimeout,
153+
'keep_alive' => true,
154+
];
155+
156+
// User config takes precedence
157+
return array_merge($defaults, $this->config);
158+
}
159+
126160
/**
127161
* Send an HTTP request using Swoole
128162
*
@@ -183,11 +217,7 @@ public function send(
183217
$allowRedirects = $options['allowRedirects'] ?? true;
184218
$userAgent = $options['userAgent'] ?? '';
185219

186-
$client->set([
187-
'timeout' => $timeout,
188-
'connect_timeout' => $connectTimeout,
189-
'keep_alive' => true,
190-
]);
220+
$client->set($this->buildClientSettings($timeout, $connectTimeout));
191221

192222
$client->setMethod($method);
193223

@@ -248,11 +278,7 @@ public function send(
248278
$port = $newPort;
249279
$ssl = $newSsl;
250280
$client = $this->getClient($host, $port, $ssl);
251-
$client->set([
252-
'timeout' => $timeout,
253-
'connect_timeout' => $connectTimeout,
254-
'keep_alive' => true,
255-
]);
281+
$client->set($this->buildClientSettings($timeout, $connectTimeout));
256282
$client->setMethod($method);
257283
if (!empty($allHeaders)) {
258284
$client->setHeaders($allHeaders);

0 commit comments

Comments
 (0)