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 Buzz\Browser; |
15
|
|
|
use Buzz\Client\AbstractCurl; |
16
|
|
|
use Buzz\Client\MultiCurl; |
17
|
|
|
use Ivory\HttpAdapter\Message\InternalRequestInterface; |
18
|
|
|
use Ivory\HttpAdapter\Normalizer\BodyNormalizer; |
19
|
|
|
use Ivory\HttpAdapter\Normalizer\HeadersNormalizer; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author GeLo <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class BuzzHttpAdapter extends AbstractCurlHttpAdapter |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Browser |
28
|
|
|
*/ |
29
|
|
|
private $browser; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Browser|null $browser |
33
|
|
|
* @param ConfigurationInterface|null $configuration |
34
|
|
|
* |
35
|
|
|
* @throws HttpAdapterException |
36
|
|
|
*/ |
37
|
1490 |
|
public function __construct(Browser $browser = null, ConfigurationInterface $configuration = null) |
38
|
|
|
{ |
39
|
1490 |
|
$browser = $browser ?: new Browser(); |
40
|
|
|
|
41
|
1490 |
|
if ($browser->getClient() instanceof MultiCurl) { |
42
|
9 |
|
throw HttpAdapterException::doesNotSupportSubAdapter( |
43
|
9 |
|
$this->getName(), |
44
|
9 |
|
get_class($browser->getClient()) |
45
|
7 |
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
1481 |
|
parent::__construct($configuration, $browser->getClient() instanceof AbstractCurl); |
49
|
|
|
|
50
|
1480 |
|
$this->browser = $browser; |
51
|
1480 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
95 |
|
public function getName() |
57
|
|
|
{ |
58
|
95 |
|
return 'buzz'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
1428 |
|
protected function sendInternalRequest(InternalRequestInterface $internalRequest) |
65
|
|
|
{ |
66
|
1428 |
|
$this->browser->getClient()->setTimeout($this->getConfiguration()->getTimeout()); |
67
|
1428 |
|
$this->browser->getClient()->setMaxRedirects(0); |
68
|
|
|
|
69
|
1428 |
|
$request = $this->browser->getMessageFactory()->createRequest( |
70
|
1428 |
|
$internalRequest->getMethod(), |
71
|
1428 |
|
$uri = (string) $internalRequest->getUri() |
72
|
1092 |
|
); |
73
|
|
|
|
74
|
1428 |
|
$request->setProtocolVersion($internalRequest->getProtocolVersion()); |
75
|
1428 |
|
$request->setHeaders($this->prepareHeaders($internalRequest, false)); |
76
|
|
|
|
77
|
1428 |
|
$data = $this->browser->getClient() instanceof AbstractCurl |
78
|
1260 |
|
? $this->prepareContent($internalRequest) |
79
|
1428 |
|
: $this->prepareBody($internalRequest); |
80
|
|
|
|
81
|
1428 |
|
$request->setContent($data); |
82
|
|
|
|
83
|
|
|
try { |
84
|
1428 |
|
$response = $this->browser->send($request); |
85
|
1108 |
|
} catch (\Exception $e) { |
86
|
68 |
|
throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage()); |
87
|
|
|
} |
88
|
|
|
|
89
|
1377 |
|
return $this->getConfiguration()->getMessageFactory()->createResponse( |
90
|
1377 |
|
$response->getStatusCode(), |
91
|
1377 |
|
sprintf('%.1f', $response->getProtocolVersion()), |
92
|
1377 |
|
HeadersNormalizer::normalize($response->getHeaders()), |
93
|
1377 |
|
BodyNormalizer::normalize($response->getContent(), $internalRequest->getMethod()) |
94
|
1053 |
|
); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|