Skip to content
This repository was archived by the owner on Jan 6, 2024. It is now read-only.

Commit 2b7632a

Browse files
committed
Initial import
1 parent ae43658 commit 2b7632a

File tree

5 files changed

+255
-2
lines changed

5 files changed

+255
-2
lines changed

composer.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
],
1313
"require": {
1414
"php": ">=5.4",
15-
"php-http/adapter-core": "dev-master"
15+
"ext-curl": "*",
16+
"php-http/adapter-core": "dev-initial_import",
17+
"guzzlehttp/guzzle": "~5.0",
18+
"guzzlehttp/ringphp": "^1.0.8@dev"
1619
},
1720
"require-dev": {
18-
"phpunit/phpunit": "~4.4"
21+
"php-http/adapter-integration-tests": "dev-master"
1922
},
2023
"provide": {
2124
"php-http/adapter-implementation": "0.1"
@@ -25,6 +28,11 @@
2528
"Http\\Adapter\\": "src/"
2629
}
2730
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"Http\\Adapter\\Tests\\": "tests/"
34+
}
35+
},
2836
"extra": {
2937
"branch-alias": {
3038
"dev-master": "0.1-dev"

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<directory>tests/</directory>
66
</testsuite>
77
</testsuites>
8+
<php>
9+
<server name="TEST_SERVER" value="http://127.0.0.1:10000/server.php" />
10+
</php>
811
<filter>
912
<whitelist>
1013
<directory suffix=".php">src/</directory>

src/Guzzle5HttpAdapter.php

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the 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 Http\Adapter;
13+
14+
use GuzzleHttp\Client;
15+
use GuzzleHttp\ClientInterface;
16+
use GuzzleHttp\Event\CompleteEvent;
17+
use GuzzleHttp\Event\ErrorEvent;
18+
use GuzzleHttp\Message\RequestInterface;
19+
use GuzzleHttp\Pool;
20+
use Http\Adapter\Message\InternalRequestInterface;
21+
use Http\Adapter\Normalizer\BodyNormalizer;
22+
23+
/**
24+
* @author GeLo <[email protected]>
25+
*/
26+
class Guzzle5HttpAdapter extends CurlHttpAdapter
27+
{
28+
/**
29+
* @var ClientInterface
30+
*/
31+
private $client;
32+
33+
/**
34+
*
35+
* @param ClientInterface|null $client
36+
* @param ConfigurationInterface|null $configuration
37+
*/
38+
public function __construct(ClientInterface $client = null, ConfigurationInterface $configuration = null)
39+
{
40+
parent::__construct($configuration);
41+
42+
$this->client = $client ?: new Client();
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function getName()
49+
{
50+
return 'guzzle5';
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
protected function sendInternalRequest(InternalRequestInterface $internalRequest)
57+
{
58+
try {
59+
$response = $this->client->send($this->createRequest($internalRequest));
60+
} catch (\Exception $e) {
61+
var_dump($e); exit;
62+
throw HttpAdapterException::cannotFetchUri(
63+
$e->getRequest()->getUrl(),
64+
$this->getName(),
65+
$e->getMessage()
66+
);
67+
}
68+
69+
return $this->getConfiguration()->getMessageFactory()->createResponse(
70+
(integer) $response->getStatusCode(),
71+
$response->getProtocolVersion(),
72+
$response->getHeaders(),
73+
BodyNormalizer::normalize(
74+
function () use ($response) {
75+
return $response->getBody()->detach();
76+
},
77+
$internalRequest->getMethod()
78+
)
79+
);
80+
}
81+
82+
/**
83+
* {@inheritdoc}
84+
*/
85+
protected function sendInternalRequests(array $internalRequests, $success, $error)
86+
{
87+
$requests = [];
88+
foreach ($internalRequests as $internalRequest) {
89+
$requests[] = $this->createRequest($internalRequest, $success, $error);
90+
}
91+
92+
Pool::batch($this->client, $requests);
93+
}
94+
95+
/**
96+
* {@inheritdoc}
97+
*/
98+
protected function createFile($file)
99+
{
100+
return fopen($file, 'r');
101+
}
102+
103+
/**
104+
* Creates a request.
105+
*
106+
* @param InternalRequestInterface $internalRequest
107+
* @param callable|null $success
108+
* @param callable|null $error
109+
*
110+
* @return RequestInterface
111+
*/
112+
private function createRequest(InternalRequestInterface $internalRequest, callable $success = null, callable $error = null)
113+
{
114+
$request = $this->client->createRequest(
115+
$internalRequest->getMethod(),
116+
(string) $internalRequest->getUri(),
117+
[
118+
'exceptions' => false,
119+
'allow_redirects' => false,
120+
'timeout' => $this->getConfiguration()->getTimeout(),
121+
'connect_timeout' => $this->getConfiguration()->getTimeout(),
122+
'version' => $internalRequest->getProtocolVersion(),
123+
'headers' => $this->prepareHeaders($internalRequest),
124+
'body' => $this->prepareContent($internalRequest),
125+
]
126+
);
127+
128+
if (isset($success)) {
129+
$messageFactory = $this->getConfiguration()->getMessageFactory();
130+
131+
$request->getEmitter()->on(
132+
'complete',
133+
function (CompleteEvent $event) use ($success, $internalRequest, $messageFactory) {
134+
$response = $messageFactory->createResponse(
135+
(integer) $event->getResponse()->getStatusCode(),
136+
$event->getResponse()->getProtocolVersion(),
137+
$event->getResponse()->getHeaders(),
138+
BodyNormalizer::normalize(
139+
function () use ($event) {
140+
return $event->getResponse()->getBody()->detach();
141+
},
142+
$internalRequest->getMethod()
143+
)
144+
);
145+
146+
$response = $response->withParameter('request', $internalRequest);
147+
call_user_func($success, $response);
148+
}
149+
);
150+
}
151+
152+
if (isset($error)) {
153+
$httpAdapterName = $this->getName();
154+
155+
$request->getEmitter()->on(
156+
'error',
157+
function (ErrorEvent $event) use ($error, $internalRequest, $httpAdapterName) {
158+
$exception = HttpAdapterException::cannotFetchUri(
159+
$event->getException()->getRequest()->getUrl(),
160+
$httpAdapterName,
161+
$event->getException()->getMessage()
162+
);
163+
$exception->setRequest($internalRequest);
164+
call_user_func($error, $exception);
165+
}
166+
);
167+
}
168+
169+
return $request;
170+
}
171+
}

tests/Guzzle5HttpAdapterTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the 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 Http\Adapter\Tests;
13+
14+
use GuzzleHttp\Client;
15+
use GuzzleHttp\Ring\Client\CurlHandler;
16+
use Http\Adapter\Guzzle5HttpAdapter;
17+
18+
/**
19+
* @author GeLo <[email protected]>
20+
*/
21+
class Guzzle5HttpAdapterTest extends HttpAdapterTest
22+
{
23+
public function testGetName()
24+
{
25+
$this->assertSame('guzzle5', $this->httpAdapter->getName());
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
protected function createHttpAdapter()
32+
{
33+
return new Guzzle5HttpAdapter(new Client(['handler' => $this->createHandler()]));
34+
}
35+
36+
/**
37+
* Returns a handler for the client
38+
*/
39+
protected function createHandler()
40+
{
41+
return new CurlHandler();
42+
}
43+
}

tests/Guzzle5MultiHttpAdapterTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the 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 Http\Adapter\Tests;
13+
14+
use GuzzleHttp\Ring\Client\CurlMultiHandler;
15+
16+
/**
17+
* @author GeLo <[email protected]>
18+
*/
19+
class Guzzle5MultiHttpAdapterTest extends Guzzle5HttpAdapterTest
20+
{
21+
/**
22+
* Returns a handler for the client
23+
*/
24+
protected function createHandler()
25+
{
26+
return new CurlMultiHandler();
27+
}
28+
}

0 commit comments

Comments
 (0)