1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Http Adapter package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\HttpAdapter; |
13
|
|
|
|
14
|
|
|
use http\Client; |
15
|
|
|
use http\Client\Request; |
16
|
|
|
use http\Message\Body; |
17
|
|
|
use Ivory\HttpAdapter\Message\InternalRequestInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author GeLo <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class PeclHttpAdapter extends AbstractHttpAdapter |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Client |
26
|
|
|
*/ |
27
|
|
|
private $client; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Client $client |
31
|
|
|
* @param ConfigurationInterface $configuration |
32
|
|
|
*/ |
33
|
616 |
|
public function __construct(Client $client = null, ConfigurationInterface $configuration = null) |
34
|
|
|
{ |
35
|
616 |
|
parent::__construct($configuration); |
36
|
|
|
|
37
|
616 |
|
$this->client = $client ?: new Client(); |
38
|
616 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
588 |
|
protected function sendInternalRequest(InternalRequestInterface $internalRequest) |
44
|
|
|
{ |
45
|
588 |
|
$body = new Body(); |
46
|
588 |
|
$body->append($this->prepareBody($internalRequest)); |
47
|
|
|
|
48
|
588 |
|
$request = new Request( |
49
|
588 |
|
$internalRequest->getMethod(), |
50
|
588 |
|
$uri = (string) $internalRequest->getUri(), |
51
|
588 |
|
$this->prepareHeaders($internalRequest), |
52
|
|
|
$body |
53
|
588 |
|
); |
54
|
|
|
|
55
|
588 |
|
$httpVersion = $internalRequest->getProtocolVersion() === InternalRequestInterface::PROTOCOL_VERSION_1_0 |
56
|
588 |
|
? \http\Client\Curl\HTTP_VERSION_1_0 |
57
|
588 |
|
: \http\Client\Curl\HTTP_VERSION_1_1; |
58
|
|
|
|
59
|
588 |
|
$request->setOptions([ |
60
|
588 |
|
'protocol' => $httpVersion, |
61
|
588 |
|
'timeout' => $this->getConfiguration()->getTimeout(), |
62
|
588 |
|
]); |
63
|
|
|
|
64
|
|
|
try { |
65
|
588 |
|
$this->client->reset()->enqueue($request)->send(); |
66
|
588 |
|
} catch (\Exception $e) { |
67
|
28 |
|
throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage()); |
68
|
|
|
} |
69
|
|
|
|
70
|
567 |
|
$response = $this->client->getResponse(); |
71
|
|
|
|
72
|
567 |
|
return $this->getConfiguration()->getMessageFactory()->createResponse( |
73
|
567 |
|
$response->getResponseCode(), |
74
|
567 |
|
$response->getHttpVersion(), |
75
|
588 |
|
$response->getHeaders(), |
76
|
567 |
|
$response->getBody()->getResource() |
77
|
567 |
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
35 |
|
public function getName() |
84
|
|
|
{ |
85
|
35 |
|
return 'pecl_http'; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|