| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient; |
| 13 |
|
| 14 |
use Amp\Http\Client\Request as AmpRequest; |
| 15 |
use Amp\Http\HttpMessage; |
| 16 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
final class HttpClient |
| 24 |
{ |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public static function create(array $defaultOptions = [], int $maxHostConnections = 6, int $maxPendingPushes = 50): HttpClientInterface |
| 33 |
{ |
| 34 |
if ($amp = class_exists(AmpRequest::class) && (\PHP_VERSION_ID >= 80400 || !is_subclass_of(AmpRequest::class, HttpMessage::class))) { |
| 35 |
if (!\extension_loaded('curl')) { |
| 36 |
return new AmpHttpClient($defaultOptions, null, $maxHostConnections, $maxPendingPushes); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
if (!\defined('CURLMOPT_PUSHFUNCTION')) { |
| 41 |
return new AmpHttpClient($defaultOptions, null, $maxHostConnections, $maxPendingPushes); |
| 42 |
} |
| 43 |
|
| 44 |
static $curlVersion = null; |
| 45 |
$curlVersion ??= curl_version(); |
| 46 |
|
| 47 |
|
| 48 |
if (0x073D00 > $curlVersion['version_number'] || !(\CURL_VERSION_HTTP2 & $curlVersion['features'])) { |
| 49 |
return new AmpHttpClient($defaultOptions, null, $maxHostConnections, $maxPendingPushes); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
if (\extension_loaded('curl')) { |
| 54 |
if ('\\' !== \DIRECTORY_SEPARATOR || isset($defaultOptions['cafile']) || isset($defaultOptions['capath']) || \ini_get('curl.cainfo') || \ini_get('openssl.cafile') || \ini_get('openssl.capath')) { |
| 55 |
return new CurlHttpClient($defaultOptions, $maxHostConnections, $maxPendingPushes); |
| 56 |
} |
| 57 |
|
| 58 |
@trigger_error('Configure the "curl.cainfo", "openssl.cafile" or "openssl.capath" php.ini setting to enable the CurlHttpClient', \E_USER_WARNING); |
| 59 |
} |
| 60 |
|
| 61 |
if ($amp) { |
| 62 |
return new AmpHttpClient($defaultOptions, null, $maxHostConnections, $maxPendingPushes); |
| 63 |
} |
| 64 |
|
| 65 |
@trigger_error((\extension_loaded('curl') ? 'Upgrade' : 'Install').' the curl extension or run "composer require amphp/http-client:^5" to perform async HTTP operations, including full HTTP/2 support', \E_USER_NOTICE); |
| 66 |
|
| 67 |
return new NativeHttpClient($defaultOptions, $maxHostConnections); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static function createForBaseUri(string $baseUri, array $defaultOptions = [], int $maxHostConnections = 6, int $maxPendingPushes = 50): HttpClientInterface |
| 74 |
{ |
| 75 |
$client = self::create([], $maxHostConnections, $maxPendingPushes); |
| 76 |
|
| 77 |
return ScopingHttpClient::forBaseUri($client, $baseUri, $defaultOptions); |
| 78 |
} |
| 79 |
} |