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

Commit b94b6be

Browse files
committed
Merge pull request #1 from php-http/initial_import
Initial import
2 parents ae43658 + a7313a2 commit b94b6be

10 files changed

+326
-14
lines changed

.gitattributes

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/tests export-ignore
2-
/.editorconfig export-ignore
3-
/.gitattributes export-ignore
4-
/.gitignore export-ignore
5-
/.scrutinizer.yml export-ignore
6-
/.travis.yml export-ignore
7-
/CONTRIBUTING.md export-ignore
8-
/phpunit.xml.dist export-ignore
1+
tests/ export-ignore
2+
.editorconfig export-ignore
3+
.gitattributes export-ignore
4+
.gitignore export-ignore
5+
.scrutinizer.yml export-ignore
6+
.travis.yml export-ignore
7+
CONTRIBUTING.md export-ignore
8+
phpunit.xml.dist export-ignore

.gitignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/build
2-
/vendor
3-
/composer.lock
4-
/phpunit.xml
1+
build/
2+
vendor/
3+
composer.lock
4+
phpunit.xml

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ install:
2222
- travis_retry composer self-update
2323
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-source --no-interaction
2424

25+
before_script:
26+
- vendor/bin/http_test_server > /dev/null 2>&1 &
27+
2528
script: vendor/bin/phpunit ${PHPUNIT_FLAGS}
2629

2730
after_script:

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+
"php-http/adapter-core": "0.1.*@dev",
16+
"guzzlehttp/guzzle": "~5.1",
17+
"guzzlehttp/ringphp": "^1.0.8@dev"
1618
},
1719
"require-dev": {
18-
"phpunit/phpunit": "~4.4"
20+
"ext-curl": "*",
21+
"php-http/adapter-integration-tests": "0.1.*@dev"
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\Exception\RequestException;
19+
use GuzzleHttp\Message\RequestInterface;
20+
use GuzzleHttp\Pool;
21+
use Http\Adapter\Message\InternalRequestInterface;
22+
use Http\Adapter\Normalizer\BodyNormalizer;
23+
24+
/**
25+
* @author GeLo <[email protected]>
26+
*/
27+
class Guzzle5HttpAdapter extends CurlHttpAdapter
28+
{
29+
/**
30+
* @var ClientInterface
31+
*/
32+
private $client;
33+
34+
/**
35+
*
36+
* @param ClientInterface|null $client
37+
* @param ConfigurationInterface|null $configuration
38+
*/
39+
public function __construct(ClientInterface $client = null, ConfigurationInterface $configuration = null)
40+
{
41+
parent::__construct($configuration);
42+
43+
$this->client = $client ?: new Client();
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function getName()
50+
{
51+
return 'guzzle5';
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
protected function sendInternalRequest(InternalRequestInterface $internalRequest)
58+
{
59+
try {
60+
$response = $this->client->send($this->createRequest($internalRequest));
61+
} catch (RequestException $e) {
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/Guzzle5CurlHttpAdapterTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\CurlHandler;
15+
16+
/**
17+
* @requires PHP 5.5
18+
*
19+
* @author GeLo <[email protected]>
20+
*/
21+
class Guzzle5CurlHttpAdapterTest extends Guzzle5HttpAdapterTest
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
protected function createHandler()
27+
{
28+
return new CurlHandler();
29+
}
30+
}

tests/Guzzle5HttpAdapterTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 Http\Adapter\Guzzle5HttpAdapter;
16+
17+
/**
18+
* @author GeLo <[email protected]>
19+
*/
20+
abstract class Guzzle5HttpAdapterTest extends HttpAdapterTest
21+
{
22+
public function testGetName()
23+
{
24+
$this->assertSame('guzzle5', $this->httpAdapter->getName());
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
protected function createHttpAdapter()
31+
{
32+
return new Guzzle5HttpAdapter(new Client(['handler' => $this->createHandler()]));
33+
}
34+
35+
/**
36+
* Returns a handler for the client
37+
*
38+
* @return object
39+
*/
40+
abstract protected function createHandler();
41+
}
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 Guzzle5MultiCurlHttpAdapterTest extends Guzzle5HttpAdapterTest
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
protected function createHandler()
25+
{
26+
return new CurlMultiHandler();
27+
}
28+
}
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\StreamHandler;
15+
16+
/**
17+
* @author GeLo <[email protected]>
18+
*/
19+
class Guzzle5StreamHttpAdapterTest extends Guzzle5HttpAdapterTest
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
protected function createHandler()
25+
{
26+
return new StreamHandler();
27+
}
28+
}

0 commit comments

Comments
 (0)