mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-24 18:13:00 +01:00
FutureResponse does not need a full transaction. Just a response please
This commit is contained in:
parent
cee099a079
commit
303d753c9f
@ -240,7 +240,7 @@ class Client implements ClientInterface
|
||||
return new FutureResponse(
|
||||
function () use ($response, $trans) {
|
||||
$response->deref();
|
||||
return $trans;
|
||||
return $trans->response;
|
||||
},
|
||||
function () use ($response) {
|
||||
return $response->cancel();
|
||||
|
@ -2,9 +2,9 @@
|
||||
namespace GuzzleHttp\Message;
|
||||
|
||||
use GuzzleHttp\Ring\BaseFutureTrait;
|
||||
use GuzzleHttp\Ring\Core;
|
||||
use GuzzleHttp\Ring\Exception\CancelledFutureAccessException;
|
||||
use GuzzleHttp\Ring\FutureInterface;
|
||||
use GuzzleHttp\Transaction;
|
||||
use GuzzleHttp\Stream\StreamInterface;
|
||||
|
||||
/**
|
||||
@ -12,14 +12,14 @@ use GuzzleHttp\Stream\StreamInterface;
|
||||
*
|
||||
* When created, you must provide a function that is used to dereference the
|
||||
* future result and return it's value. The function has no arguments and MUST
|
||||
* return an instance of a GuzzleHttp\Transaction object.
|
||||
* return an instance of a {@see GuzzleHttp\Message\ResponseInterface} object.
|
||||
*
|
||||
* You can optionally provide a function in the constructor that can be used to
|
||||
* cancel the future from completing if possible. This function has no
|
||||
* arguments and returns a boolean value representing whether or not the
|
||||
* response could be cancelled.
|
||||
*
|
||||
* @property Transaction transaction
|
||||
* @property ResponseInterface response
|
||||
*/
|
||||
class FutureResponse implements ResponseInterface, FutureInterface
|
||||
{
|
||||
@ -27,108 +27,108 @@ class FutureResponse implements ResponseInterface, FutureInterface
|
||||
|
||||
public function deref()
|
||||
{
|
||||
return $this->transaction->response;
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
public function getStatusCode()
|
||||
{
|
||||
return $this->transaction->response->getStatusCode();
|
||||
return $this->response->getStatusCode();
|
||||
}
|
||||
|
||||
public function getReasonPhrase()
|
||||
{
|
||||
return $this->transaction->response->getReasonPhrase();
|
||||
return $this->response->getReasonPhrase();
|
||||
}
|
||||
|
||||
public function getEffectiveUrl()
|
||||
{
|
||||
return $this->transaction->response->getEffectiveUrl();
|
||||
return $this->response->getEffectiveUrl();
|
||||
}
|
||||
|
||||
public function setEffectiveUrl($url)
|
||||
{
|
||||
$this->transaction->response->setEffectiveUrl($url);
|
||||
$this->response->setEffectiveUrl($url);
|
||||
}
|
||||
|
||||
public function json(array $config = [])
|
||||
{
|
||||
return $this->transaction->response->json($config);
|
||||
return $this->response->json($config);
|
||||
}
|
||||
|
||||
public function xml(array $config = [])
|
||||
{
|
||||
return $this->transaction->response->xml($config);
|
||||
return $this->response->xml($config);
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->transaction->response->__toString();
|
||||
return $this->response->__toString();
|
||||
}
|
||||
|
||||
public function getProtocolVersion()
|
||||
{
|
||||
return $this->transaction->response->getProtocolVersion();
|
||||
return $this->response->getProtocolVersion();
|
||||
}
|
||||
|
||||
public function setBody(StreamInterface $body = null)
|
||||
{
|
||||
$this->transaction->response->setBody($body);
|
||||
$this->response->setBody($body);
|
||||
}
|
||||
|
||||
public function getBody()
|
||||
{
|
||||
return $this->transaction->response->getBody();
|
||||
return $this->response->getBody();
|
||||
}
|
||||
|
||||
public function getHeaders()
|
||||
{
|
||||
return $this->transaction->response->getHeaders();
|
||||
return $this->response->getHeaders();
|
||||
}
|
||||
|
||||
public function getHeader($header)
|
||||
{
|
||||
return $this->transaction->response->getHeader($header);
|
||||
return $this->response->getHeader($header);
|
||||
}
|
||||
|
||||
public function getHeaderLines($header)
|
||||
{
|
||||
return $this->transaction->response->getHeaderLines($header);
|
||||
return $this->response->getHeaderLines($header);
|
||||
}
|
||||
|
||||
public function hasHeader($header)
|
||||
{
|
||||
return $this->transaction->response->hasHeader($header);
|
||||
return $this->response->hasHeader($header);
|
||||
}
|
||||
|
||||
public function removeHeader($header)
|
||||
{
|
||||
$this->transaction->response->removeHeader($header);
|
||||
$this->response->removeHeader($header);
|
||||
}
|
||||
|
||||
public function addHeader($header, $value)
|
||||
{
|
||||
$this->transaction->response->addHeader($header, $value);
|
||||
$this->response->addHeader($header, $value);
|
||||
}
|
||||
|
||||
public function addHeaders(array $headers)
|
||||
{
|
||||
$this->transaction->response->addHeaders($headers);
|
||||
$this->response->addHeaders($headers);
|
||||
}
|
||||
|
||||
public function setHeader($header, $value)
|
||||
{
|
||||
$this->transaction->response->setHeader($header, $value);
|
||||
$this->response->setHeader($header, $value);
|
||||
}
|
||||
|
||||
public function setHeaders(array $headers)
|
||||
{
|
||||
$this->transaction->response->setHeaders($headers);
|
||||
$this->response->setHeaders($headers);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
public function __get($name)
|
||||
{
|
||||
if ($name !== 'transaction') {
|
||||
if ($name !== 'response') {
|
||||
throw new \RuntimeException('Unknown property: ' . $name);
|
||||
} elseif ($this->isCancelled) {
|
||||
throw new CancelledFutureAccessException('You are attempting '
|
||||
@ -137,13 +137,13 @@ class FutureResponse implements ResponseInterface, FutureInterface
|
||||
|
||||
$dereffn = $this->dereffn;
|
||||
$this->dereffn = $this->cancelfn = null;
|
||||
$this->transaction = $dereffn();
|
||||
$this->response = $dereffn();
|
||||
|
||||
if (!$this->transaction instanceof Transaction) {
|
||||
if (!$this->response instanceof ResponseInterface) {
|
||||
throw new \RuntimeException('Future did not return a valid '
|
||||
. 'transaction. Got ' . gettype($this->transaction));
|
||||
. 'response. Found ' . Core::describeType($this->response));
|
||||
}
|
||||
|
||||
return $this->transaction;
|
||||
return $this->response;
|
||||
}
|
||||
}
|
||||
|
12
src/Pool.php
12
src/Pool.php
@ -5,6 +5,7 @@ use GuzzleHttp\Event\RequestEvents;
|
||||
use GuzzleHttp\Event\CompleteEvent;
|
||||
use GuzzleHttp\Event\ErrorEvent;
|
||||
use GuzzleHttp\Message\RequestInterface;
|
||||
use GuzzleHttp\Ring\Core;
|
||||
use GuzzleHttp\Ring\FutureInterface;
|
||||
use GuzzleHttp\Event\ListenerAttacherTrait;
|
||||
|
||||
@ -248,12 +249,11 @@ class Pool implements FutureInterface
|
||||
$this->iter->next();
|
||||
|
||||
if (!($request instanceof RequestInterface)) {
|
||||
$found = is_object($request)
|
||||
? get_class($request)
|
||||
: gettype($request);
|
||||
$err = sprintf('All requests in the provided iterator must '
|
||||
. 'implement RequestInterface. Found %s', $found);
|
||||
throw new \RuntimeException($err);
|
||||
throw new \RuntimeException(sprintf(
|
||||
'All requests in the provided iterator must implement '
|
||||
. 'RequestInterface. Found %s',
|
||||
Core::describeType($request)
|
||||
));
|
||||
}
|
||||
|
||||
$request->getConfig()->set('future', 'lazy');
|
||||
|
@ -1,12 +1,9 @@
|
||||
<?php
|
||||
namespace GuzzleHttp\Tests\Message;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Message\FutureResponse;
|
||||
use GuzzleHttp\Message\Request;
|
||||
use GuzzleHttp\Message\Response;
|
||||
use GuzzleHttp\Stream\Stream;
|
||||
use GuzzleHttp\Transaction;
|
||||
|
||||
class FutureResponseTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
@ -22,7 +19,7 @@ class FutureResponseTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
* @expectedExceptionMessage Future did not return a valid transaction. Got NULL
|
||||
* @expectedExceptionMessage Future did not return a valid response. Found NULL
|
||||
*/
|
||||
public function testEnsuresDerefReturnsTransaction()
|
||||
{
|
||||
@ -35,13 +32,7 @@ class FutureResponseTest extends \PHPUnit_Framework_TestCase
|
||||
$str = Stream::factory('foo');
|
||||
$response = new Response(200, ['Foo' => 'bar'], $str);
|
||||
$future = new FutureResponse(function () use ($response) {
|
||||
$t = new Transaction(
|
||||
new Client(),
|
||||
new Request('GET', 'http://www.foo.com')
|
||||
);
|
||||
$t->response = $response;
|
||||
$response->setEffectiveUrl('http://www.foo.com');
|
||||
return $t;
|
||||
return $response;
|
||||
});
|
||||
$this->assertFalse($future->realized());
|
||||
$this->assertEquals(200, $future->getStatusCode());
|
||||
@ -102,12 +93,7 @@ class FutureResponseTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$response = new Response(200, ['Foo' => 'bar']);
|
||||
$future = new FutureResponse(function () use ($response) {
|
||||
$t = new Transaction(
|
||||
new Client(),
|
||||
new Request('GET', 'http://www.foo.com')
|
||||
);
|
||||
$t->response = $response;
|
||||
return $t;
|
||||
return $response;
|
||||
});
|
||||
$this->assertSame($response, $future->deref());
|
||||
$this->assertTrue($future->realized());
|
||||
|
Loading…
x
Reference in New Issue
Block a user