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

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
sagikazarmark committed May 3, 2015
1 parent ae43658 commit 2b7632a
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 2 deletions.
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"
"ext-curl": "*",
"php-http/adapter-core": "dev-initial_import",
"guzzlehttp/guzzle": "~5.0",
"guzzlehttp/ringphp": "^1.0.8@dev"
},
"require-dev": {
"phpunit/phpunit": "~4.4"
"php-http/adapter-integration-tests": "dev-master"
},
"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 <[email protected]>
*
* 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\Message\RequestInterface;
use GuzzleHttp\Pool;
use Http\Adapter\Message\InternalRequestInterface;
use Http\Adapter\Normalizer\BodyNormalizer;

/**
* @author GeLo <[email protected]>
*/
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 (\Exception $e) {

This comment has been minimized.

Copy link
@sagikazarmark

sagikazarmark May 4, 2015

Author Member

Shouldn't we typehint against a specific exception which we can make sure that has a getRequest method?

This comment has been minimized.

Copy link
@egeloen

egeloen May 4, 2015

Contributor

Absolutely...

This comment has been minimized.

Copy link
@egeloen

egeloen May 4, 2015

Contributor

According to http://docs.guzzlephp.org/en/latest/quickstart.html#exceptions, the exception should be a GuzzleHttp\Exception\RequestException

var_dump($e); exit;
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;
}
}
43 changes: 43 additions & 0 deletions tests/Guzzle5HttpAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Http Adapter package.
*
* (c) Eric GELOEN <[email protected]>
*
* 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 GuzzleHttp\Ring\Client\CurlHandler;
use Http\Adapter\Guzzle5HttpAdapter;

/**
* @author GeLo <[email protected]>
*/
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
*/
protected function createHandler()
{
return new CurlHandler();
}
}
28 changes: 28 additions & 0 deletions tests/Guzzle5MultiHttpAdapterTest.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 <[email protected]>
*
* 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 <[email protected]>
*/
class Guzzle5MultiHttpAdapterTest extends Guzzle5HttpAdapterTest
{
/**
* Returns a handler for the client
*/
protected function createHandler()
{
return new CurlMultiHandler();
}
}

0 comments on commit 2b7632a

Please sign in to comment.