| 1 |
<?php |
| 2 |
|
| 3 |
namespace GuzzleHttp\Handler; |
| 4 |
|
| 5 |
use GuzzleHttp\Promise\PromiseInterface; |
| 6 |
use GuzzleHttp\TransportSharing; |
| 7 |
use Psr\Http\Message\RequestInterface; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class CurlHandler |
| 19 |
{ |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
private $factory; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
private $shareHandleState; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public function __construct(array $options = []) |
| 39 |
{ |
| 40 |
CurlShareHandleState::assertNoRequiredSharingCustomFactoryConflict($options, 'CurlHandler'); |
| 41 |
$transportSharing = $options['transport_sharing'] ?? null; |
| 42 |
$sharingMode = CurlShareHandleState::normalizeMode($transportSharing, 'transport_sharing'); |
| 43 |
|
| 44 |
if (\array_key_exists('handle_factory', $options) && $options['handle_factory'] !== null) { |
| 45 |
$this->shareHandleState = null; |
| 46 |
$this->factory = $options['handle_factory']; |
| 47 |
|
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
$this->shareHandleState = $sharingMode !== TransportSharing::NONE |
| 52 |
? CurlShareHandleState::fromOption($transportSharing) |
| 53 |
: null; |
| 54 |
|
| 55 |
$this->factory = $this->shareHandleState !== null |
| 56 |
? new CurlFactory(3, $this->shareHandleState->mode, $this->shareHandleState->handle) |
| 57 |
: new CurlFactory(3); |
| 58 |
} |
| 59 |
|
| 60 |
public function __invoke(RequestInterface $request, array $options): PromiseInterface |
| 61 |
{ |
| 62 |
if (isset($options['delay'])) { |
| 63 |
\usleep($options['delay'] * 1000); |
| 64 |
} |
| 65 |
|
| 66 |
$easy = $this->factory->create($request, $options); |
| 67 |
\curl_exec($easy->handle); |
| 68 |
$easy->errno = \curl_errno($easy->handle); |
| 69 |
|
| 70 |
return CurlFactory::finish($this, $easy, $this->factory); |
| 71 |
} |
| 72 |
} |