Skip to content

Commit c9b557a

Browse files
committed
Fix curl-share handling on PHP < 8.5
1 parent fcf461f commit c9b557a

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/utils/HttpClient.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22

33
namespace staabm\PHPStanTodoBy\utils;
44

5+
use CurlShareHandle;
56
use RuntimeException;
67

8+
use function function_exists;
79
use function is_string;
810

911
final class HttpClient
1012
{
13+
/**
14+
* @var CurlShareHandle|resource|null
15+
*/
16+
private $shareHandle;
17+
1118
/**
1219
* @param non-empty-array<non-empty-string> $urls
1320
* @param list<string> $headers
@@ -16,15 +23,19 @@ final class HttpClient
1623
*/
1724
public function getMulti(array $urls, array $headers): array
1825
{
19-
if (function_exists('curl_share_init_persistent')) {
20-
$share = curl_share_init_persistent([
21-
CURL_LOCK_DATA_DNS,
22-
CURL_LOCK_DATA_CONNECT,
23-
]);
24-
} else {
25-
$share = curl_share_init();
26-
curl_share_setopt($share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
27-
curl_share_setopt($share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
26+
if (null === $this->shareHandle) {
27+
if (function_exists('curl_share_init_persistent')) {
28+
$this->shareHandle = curl_share_init_persistent([
29+
CURL_LOCK_DATA_DNS,
30+
CURL_LOCK_DATA_CONNECT,
31+
CURL_LOCK_DATA_SSL_SESSION,
32+
]);
33+
} else {
34+
$this->shareHandle = curl_share_init();
35+
curl_share_setopt($this->shareHandle, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
36+
curl_share_setopt($this->shareHandle, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
37+
curl_share_setopt($this->shareHandle, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
38+
}
2839
}
2940

3041
$mh = curl_multi_init();
@@ -37,7 +48,7 @@ public function getMulti(array $urls, array $headers): array
3748
throw new RuntimeException('Could not initialize cURL connection');
3849
}
3950

40-
curl_setopt($curl, CURLOPT_SHARE, $share);
51+
curl_setopt($curl, CURLOPT_SHARE, $this->shareHandle);
4152

4253
// see https://stackoverflow.com/a/27776164/1597388
4354
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);

0 commit comments

Comments
 (0)