Skip to content
This repository was archived by the owner on Jan 6, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/tests export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/CONTRIBUTING.md export-ignore
/phpunit.xml.dist export-ignore
tests/ export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.scrutinizer.yml export-ignore
.travis.yml export-ignore
CONTRIBUTING.md export-ignore
phpunit.xml.dist export-ignore
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/build
/vendor
/composer.lock
/phpunit.xml
build/
vendor/
composer.lock
phpunit.xml
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ install:
- travis_retry composer self-update
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-source --no-interaction

before_script:
- vendor/bin/http_test_server > /dev/null 2>&1 &

script: vendor/bin/phpunit ${PHPUNIT_FLAGS}

after_script:
Expand Down
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
],
"require": {
"php": ">=5.4",
"php-http/adapter-core": "dev-master"
"php-http/adapter-core": "0.1.*@dev",
"guzzlehttp/guzzle": "~5.1",
"guzzlehttp/ringphp": "^1.0.8@dev"
},
"require-dev": {
"phpunit/phpunit": "~4.4"
"ext-curl": "*",
"php-http/adapter-integration-tests": "0.1.*@dev"
},
"provide": {
"php-http/adapter-implementation": "0.1"
Expand All @@ -25,6 +28,11 @@
"Http\\Adapter\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Http\\Adapter\\Tests\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "0.1-dev"
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<directory>tests/</directory>
</testsuite>
</testsuites>
<php>
<server name="TEST_SERVER" value="http://127.0.0.1:10000/server.php" />
</php>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
Expand Down
171 changes: 171 additions & 0 deletions src/Guzzle5HttpAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php

/*
* This file is part of the Http Adapter package.
*
* (c) Eric GELOEN <geloen.eric@gmail.com>
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code.
*/

namespace Http\Adapter;

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Event\CompleteEvent;
use GuzzleHttp\Event\ErrorEvent;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Message\RequestInterface;
use GuzzleHttp\Pool;
use Http\Adapter\Message\InternalRequestInterface;
use Http\Adapter\Normalizer\BodyNormalizer;

/**
* @author GeLo <geloen.eric@gmail.com>
*/
class Guzzle5HttpAdapter extends CurlHttpAdapter
{
/**
* @var ClientInterface
*/
private $client;

/**
*
* @param ClientInterface|null $client
* @param ConfigurationInterface|null $configuration
*/
public function __construct(ClientInterface $client = null, ConfigurationInterface $configuration = null)
{
parent::__construct($configuration);

$this->client = $client ?: new Client();
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'guzzle5';
}

/**
* {@inheritdoc}
*/
protected function sendInternalRequest(InternalRequestInterface $internalRequest)
{
try {
$response = $this->client->send($this->createRequest($internalRequest));
} catch (RequestException $e) {
throw HttpAdapterException::cannotFetchUri(
$e->getRequest()->getUrl(),
$this->getName(),
$e->getMessage()
);
}

return $this->getConfiguration()->getMessageFactory()->createResponse(
(integer) $response->getStatusCode(),
$response->getProtocolVersion(),
$response->getHeaders(),
BodyNormalizer::normalize(
function () use ($response) {
return $response->getBody()->detach();
},
$internalRequest->getMethod()
)
);
}

/**
* {@inheritdoc}
*/
protected function sendInternalRequests(array $internalRequests, $success, $error)
{
$requests = [];
foreach ($internalRequests as $internalRequest) {
$requests[] = $this->createRequest($internalRequest, $success, $error);
}

Pool::batch($this->client, $requests);
}

/**
* {@inheritdoc}
*/
protected function createFile($file)
{
return fopen($file, 'r');
}

/**
* Creates a request.
*
* @param InternalRequestInterface $internalRequest
* @param callable|null $success
* @param callable|null $error
*
* @return RequestInterface
*/
private function createRequest(InternalRequestInterface $internalRequest, callable $success = null, callable $error = null)
{
$request = $this->client->createRequest(
$internalRequest->getMethod(),
(string) $internalRequest->getUri(),
[
'exceptions' => false,
'allow_redirects' => false,
'timeout' => $this->getConfiguration()->getTimeout(),
'connect_timeout' => $this->getConfiguration()->getTimeout(),
'version' => $internalRequest->getProtocolVersion(),
'headers' => $this->prepareHeaders($internalRequest),
'body' => $this->prepareContent($internalRequest),
]
);

if (isset($success)) {
$messageFactory = $this->getConfiguration()->getMessageFactory();

$request->getEmitter()->on(
'complete',
function (CompleteEvent $event) use ($success, $internalRequest, $messageFactory) {
$response = $messageFactory->createResponse(
(integer) $event->getResponse()->getStatusCode(),
$event->getResponse()->getProtocolVersion(),
$event->getResponse()->getHeaders(),
BodyNormalizer::normalize(
function () use ($event) {
return $event->getResponse()->getBody()->detach();
},
$internalRequest->getMethod()
)
);

$response = $response->withParameter('request', $internalRequest);
call_user_func($success, $response);
}
);
}

if (isset($error)) {
$httpAdapterName = $this->getName();

$request->getEmitter()->on(
'error',
function (ErrorEvent $event) use ($error, $internalRequest, $httpAdapterName) {
$exception = HttpAdapterException::cannotFetchUri(
$event->getException()->getRequest()->getUrl(),
$httpAdapterName,
$event->getException()->getMessage()
);
$exception->setRequest($internalRequest);
call_user_func($error, $exception);
}
);
}

return $request;
}
}
30 changes: 30 additions & 0 deletions tests/Guzzle5CurlHttpAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Http Adapter package.
*
* (c) Eric GELOEN <geloen.eric@gmail.com>
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code.
*/

namespace Http\Adapter\Tests;

use GuzzleHttp\Ring\Client\CurlHandler;

/**
* @requires PHP 5.5
*
* @author GeLo <geloen.eric@gmail.com>
*/
class Guzzle5CurlHttpAdapterTest extends Guzzle5HttpAdapterTest
{
/**
* {@inheritdoc}
*/
protected function createHandler()
{
return new CurlHandler();
}
}
41 changes: 41 additions & 0 deletions tests/Guzzle5HttpAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Http Adapter package.
*
* (c) Eric GELOEN <geloen.eric@gmail.com>
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code.
*/

namespace Http\Adapter\Tests;

use GuzzleHttp\Client;
use Http\Adapter\Guzzle5HttpAdapter;

/**
* @author GeLo <geloen.eric@gmail.com>
*/
abstract class Guzzle5HttpAdapterTest extends HttpAdapterTest
{
public function testGetName()
{
$this->assertSame('guzzle5', $this->httpAdapter->getName());
}

/**
* {@inheritdoc}
*/
protected function createHttpAdapter()
{
return new Guzzle5HttpAdapter(new Client(['handler' => $this->createHandler()]));
}

/**
* Returns a handler for the client
*
* @return object
*/
abstract protected function createHandler();
}
28 changes: 28 additions & 0 deletions tests/Guzzle5MultiCurlHttpAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Http Adapter package.
*
* (c) Eric GELOEN <geloen.eric@gmail.com>
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code.
*/

namespace Http\Adapter\Tests;

use GuzzleHttp\Ring\Client\CurlMultiHandler;

/**
* @author GeLo <geloen.eric@gmail.com>
*/
class Guzzle5MultiCurlHttpAdapterTest extends Guzzle5HttpAdapterTest
{
/**
* {@inheritdoc}
*/
protected function createHandler()
{
return new CurlMultiHandler();
}
}
28 changes: 28 additions & 0 deletions tests/Guzzle5StreamHttpAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Http Adapter package.
*
* (c) Eric GELOEN <geloen.eric@gmail.com>
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code.
*/

namespace Http\Adapter\Tests;

use GuzzleHttp\Ring\Client\StreamHandler;

/**
* @author GeLo <geloen.eric@gmail.com>
*/
class Guzzle5StreamHttpAdapterTest extends Guzzle5HttpAdapterTest
{
/**
* {@inheritdoc}
*/
protected function createHandler()
{
return new StreamHandler();
}
}