mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-24 18:13:00 +01:00
Renaming BatchAdapter stuff to ParallelAdapter
This commit is contained in:
parent
0c03ac7acd
commit
da9e30f98e
@ -3,7 +3,7 @@
|
||||
namespace Guzzle\Http\Adapter\Curl;
|
||||
|
||||
use Guzzle\Http\Adapter\AdapterInterface;
|
||||
use Guzzle\Http\Adapter\BatchAdapterInterface;
|
||||
use Guzzle\Http\Adapter\ParallelAdapterInterface;
|
||||
use Guzzle\Http\Adapter\TransactionInterface;
|
||||
use Guzzle\Http\Event\RequestEvents;
|
||||
use Guzzle\Http\Exception\AdapterException;
|
||||
@ -17,7 +17,7 @@ use Guzzle\Http\Message\MessageFactoryInterface;
|
||||
* associative array of curl option constants mapping to values in the
|
||||
* **curl** key of a request's configuration options.
|
||||
*/
|
||||
class CurlAdapter implements AdapterInterface, BatchAdapterInterface
|
||||
class CurlAdapter implements AdapterInterface, ParallelAdapterInterface
|
||||
{
|
||||
const ERROR_STR = 'See http://curl.haxx.se/libcurl/c/libcurl-errors.html for an explanation of cURL errors';
|
||||
|
||||
@ -81,7 +81,7 @@ class CurlAdapter implements AdapterInterface, BatchAdapterInterface
|
||||
return $transaction->getResponse();
|
||||
}
|
||||
|
||||
public function batch(\Iterator $transactions, $parallel)
|
||||
public function sendAll(\Iterator $transactions, $parallel)
|
||||
{
|
||||
$context = new BatchContext(
|
||||
$this->checkoutMultiHandle(),
|
||||
|
@ -5,10 +5,10 @@ namespace Guzzle\Http\Adapter;
|
||||
use Guzzle\Http\Exception\RequestException;
|
||||
|
||||
/**
|
||||
* Decorates a regular adapter and creates a batch adapter that sends multiple
|
||||
* requests serially
|
||||
* Decorates a regular AdapterInterface object and creates a
|
||||
* ParallelAdapterInterface object that sends multiple HTTP requests serially.
|
||||
*/
|
||||
class FakeBatchAdapter implements BatchAdapterInterface
|
||||
class FakeParallelAdapter implements ParallelAdapterInterface
|
||||
{
|
||||
/** @var AdapterInterface */
|
||||
private $adapter;
|
||||
@ -21,7 +21,7 @@ class FakeBatchAdapter implements BatchAdapterInterface
|
||||
$this->adapter = $adapter;
|
||||
}
|
||||
|
||||
public function batch(\Iterator $transactions, $parallel)
|
||||
public function sendAll(\Iterator $transactions, $parallel)
|
||||
{
|
||||
foreach ($transactions as $transaction) {
|
||||
try {
|
@ -3,17 +3,17 @@
|
||||
namespace Guzzle\Http\Adapter;
|
||||
|
||||
/**
|
||||
* Adapter interface used to transfer multiple HTTP requests
|
||||
* Adapter interface used to transfer multiple HTTP requests in parallel.
|
||||
*/
|
||||
interface BatchAdapterInterface
|
||||
interface ParallelAdapterInterface
|
||||
{
|
||||
/**
|
||||
* Transfers multiple HTTP requests in parallel.
|
||||
*
|
||||
* RequestExceptions MUST not be thrown from a batch transfer.
|
||||
* RequestExceptions MUST not be thrown from a parallel transfer.
|
||||
*
|
||||
* @param \Iterator $transactions Iterable of TransactionInterface objects
|
||||
* @param int $parallel Maximum number of requests to send in parallel
|
||||
*/
|
||||
public function batch(\Iterator $transactions, $parallel);
|
||||
public function sendAll(\Iterator $transactions, $parallel);
|
||||
}
|
@ -4,8 +4,8 @@ namespace Guzzle\Http;
|
||||
|
||||
use Guzzle\Common\Collection;
|
||||
use Guzzle\Common\HasEmitterTrait;
|
||||
use Guzzle\Http\Adapter\FakeBatchAdapter;
|
||||
use Guzzle\Http\Adapter\BatchAdapterInterface;
|
||||
use Guzzle\Http\Adapter\FakeParallelAdapter;
|
||||
use Guzzle\Http\Adapter\ParallelAdapterInterface;
|
||||
use Guzzle\Http\Adapter\AdapterInterface;
|
||||
use Guzzle\Http\Adapter\StreamAdapter;
|
||||
use Guzzle\Http\Adapter\StreamingProxyAdapter;
|
||||
@ -30,8 +30,8 @@ class Client implements ClientInterface
|
||||
/** @var AdapterInterface */
|
||||
private $adapter;
|
||||
|
||||
/** @var BatchAdapterInterface */
|
||||
private $batchAdapter;
|
||||
/** @var ParallelAdapterInterface */
|
||||
private $parallelAdapter;
|
||||
|
||||
/** @var Url Base URL of the client */
|
||||
private $baseUrl;
|
||||
@ -63,7 +63,7 @@ class Client implements ClientInterface
|
||||
* an array that contains a URI template followed by an associative array of
|
||||
* expansion variables to inject into the URI template.
|
||||
* - adapter: Adapter used to transfer requests
|
||||
* - batch_adapter: Adapter used to transfer requests in parallel
|
||||
* - parallel_adapter: Adapter used to transfer requests in parallel
|
||||
* - message_factory: Factory used to create request and response object
|
||||
* - defaults: Default request options to apply to each request
|
||||
*/
|
||||
@ -179,7 +179,7 @@ class Client implements ClientInterface
|
||||
$requests = new TransactionIterator($requests, $this, $options);
|
||||
}
|
||||
|
||||
$this->batchAdapter->batch(
|
||||
$this->parallelAdapter->sendAll(
|
||||
$requests,
|
||||
isset($options['parallel']) ? $options['parallel'] : 50
|
||||
);
|
||||
@ -228,16 +228,16 @@ class Client implements ClientInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a default batch adapter to use based on the environment
|
||||
* Get a default parallel adapter to use based on the environment
|
||||
*
|
||||
* @return BatchAdapterInterface|null
|
||||
* @return ParallelAdapterInterface|null
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
private function getDefaultBatchAdapter()
|
||||
private function getDefaultParallelAdapter()
|
||||
{
|
||||
return extension_loaded('curl')
|
||||
? new CurlAdapter($this->messageFactory)
|
||||
: new FakeBatchAdapter($this->adapter);
|
||||
: new FakeParallelAdapter($this->adapter);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -250,12 +250,12 @@ class Client implements ClientInterface
|
||||
{
|
||||
if (extension_loaded('curl')) {
|
||||
if (!ini_get('allow_url_fopen')) {
|
||||
return $this->batchAdapter = $this->adapter = new CurlAdapter($this->messageFactory);
|
||||
return $this->parallelAdapter = $this->adapter = new CurlAdapter($this->messageFactory);
|
||||
} else {
|
||||
// Assume that the batch adapter will also be this CurlAdapter
|
||||
$this->batchAdapter = new CurlAdapter($this->messageFactory);
|
||||
// Assume that the parallel adapter will also be this CurlAdapter
|
||||
$this->parallelAdapter = new CurlAdapter($this->messageFactory);
|
||||
return new StreamingProxyAdapter(
|
||||
$this->batchAdapter,
|
||||
$this->parallelAdapter,
|
||||
new StreamAdapter($this->messageFactory)
|
||||
);
|
||||
}
|
||||
@ -309,13 +309,13 @@ class Client implements ClientInterface
|
||||
} else {
|
||||
$this->adapter = $this->getDefaultAdapter();
|
||||
}
|
||||
// If no batch adapter was explicitly provided and one was not defaulted
|
||||
// when creating the default adapter, then create one now
|
||||
if (isset($config['batch_adapter'])) {
|
||||
$this->batchAdapter = $config['batch_adapter'];
|
||||
unset($config['batch_adapter']);
|
||||
} elseif (!$this->batchAdapter) {
|
||||
$this->batchAdapter = $this->getDefaultBatchAdapter();
|
||||
// If no parallel adapter was explicitly provided and one was not
|
||||
// defaulted when creating the default adapter, then create one now.
|
||||
if (isset($config['parallel_adapter'])) {
|
||||
$this->parallelAdapter = $config['parallel_adapter'];
|
||||
unset($config['parallel_adapter']);
|
||||
} elseif (!$this->parallelAdapter) {
|
||||
$this->parallelAdapter = $this->getDefaultParallelAdapter();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ class CurlAdapterTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('bar', $response->getHeader('Foo'));
|
||||
}
|
||||
|
||||
public function testSendsBatchRequests()
|
||||
public function testSendsParallelRequests()
|
||||
{
|
||||
$c = new Client();
|
||||
self::$server->flush();
|
||||
@ -62,7 +62,7 @@ class CurlAdapterTest extends \PHPUnit_Framework_TestCase
|
||||
new Transaction($c, new Request('HEAD', self::$server->getUrl()))
|
||||
];
|
||||
$a = new CurlAdapter(new MessageFactory());
|
||||
$a->batch(new \ArrayIterator($transactions), 20);
|
||||
$a->sendAll(new \ArrayIterator($transactions), 20);
|
||||
foreach ($transactions as $t) {
|
||||
$this->assertContains($t->getResponse()->getStatusCode(), [200, 201, 202]);
|
||||
}
|
||||
|
@ -2,16 +2,16 @@
|
||||
|
||||
namespace Guzzle\Tests\Http\Adapter;
|
||||
|
||||
use Guzzle\Http\Adapter\FakeBatchAdapter;
|
||||
use Guzzle\Http\Adapter\FakeParallelAdapter;
|
||||
use Guzzle\Http\Adapter\MockAdapter;
|
||||
use Guzzle\Http\Client;
|
||||
use Guzzle\Http\Message\Response;
|
||||
use Guzzle\Http\Adapter\TransactionIterator;
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\Adapter\FakeBatchAdapter
|
||||
* @covers Guzzle\Http\Adapter\FakeParallelAdapter
|
||||
*/
|
||||
class FakeBatchAdapterTest extends \PHPUnit_Framework_TestCase
|
||||
class FakeParallelAdapterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testSendsAllTransactions()
|
||||
{
|
||||
@ -22,13 +22,13 @@ class FakeBatchAdapterTest extends \PHPUnit_Framework_TestCase
|
||||
];
|
||||
|
||||
$sent = [];
|
||||
$f = new FakeBatchAdapter(new MockAdapter(function ($trans) use (&$sent) {
|
||||
$f = new FakeParallelAdapter(new MockAdapter(function ($trans) use (&$sent) {
|
||||
$sent[] = $trans->getRequest()->getMethod();
|
||||
return new Response(200);
|
||||
}));
|
||||
|
||||
$tIter = new TransactionIterator($requests, $client, []);
|
||||
$f->batch($tIter, 2);
|
||||
$f->sendAll($tIter, 2);
|
||||
$this->assertContains('GET', $sent);
|
||||
$this->assertContains('HEAD', $sent);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user