1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-24 18:13:00 +01:00

[Common] Adding BatchClosureDivisor

[Http] Throwing exception when a php://temp stream cannot be created
when using CURLOPT_STDERR
Adding client keyword to composer
This commit is contained in:
Michael Dowling 2012-06-18 21:22:33 -07:00
parent 688631a8c3
commit 9586d67b66
4 changed files with 88 additions and 1 deletions

View File

@ -2,7 +2,7 @@
"name": "guzzle/guzzle",
"type": "library",
"description": "Guzzle is a PHP HTTP client library and framework for building RESTful web service clients",
"keywords": ["framework", "http", "rest", "web service", "curl"],
"keywords": ["framework", "http", "rest", "web service", "curl", "client"],
"homepage": "http://www.guzzlephp.org/",
"license": "MIT",

View File

@ -0,0 +1,45 @@
<?php
namespace Guzzle\Common\Batch;
use Guzzle\Common\Exception\InvalidArgumentException;
/**
* Divides batches using a callable
*/
class BatchClosureDivisor implements BatchDivisorInterface
{
/**
* @var callable Method used to divide the batches
*/
protected $callable;
/**
* @var mixed $context Context passed to the callable
*/
protected $context;
/**
* @param callable $callable Method used to divide the batches. The method
* must accept an \SplQueue and return an array of
* arrays containing the divided items
* @param mixed $context Optional context to pass to the batch divisor
*/
public function __construct($callable, $context = null)
{
if (!is_callable($callable)) {
throw new InvalidArgumentException('Must pass a callable');
}
$this->callable = $callable;
$this->context = $context;
}
/**
* {@inheritdoc}
*/
public function createBatches(\SplQueue $queue)
{
return call_user_func($this->callable, $queue, $this->context);
}
}

View File

@ -3,6 +3,7 @@
namespace Guzzle\Http\Curl;
use Guzzle\Common\Exception\InvalidArgumentException;
use Guzzle\Common\Exception\RuntimeException;
use Guzzle\Common\Collection;
use Guzzle\Http\Message\RequestInterface;
use Guzzle\Http\Parser\ParserRegistry;
@ -78,6 +79,11 @@ class CurlHandle
// Enable curl debug information if the 'debug' param was set
if (!$requestCurlOptions->get('disable_wire')) {
$curlOptions[CURLOPT_STDERR] = fopen('php://temp', 'r+');
// @codeCoverageIgnoreStart
if (false === $curlOptions[CURLOPT_STDERR]) {
throw new RuntimeException('Unable to create a stream for CURLOPT_STDERR');
}
// @codeCoverageIgnoreEnd
$curlOptions[CURLOPT_VERBOSE] = true;
}

View File

@ -0,0 +1,36 @@
<?php
namespace Guzzle\Tests\Common;
use Guzzle\Common\Batch\BatchClosureDivisor;
/**
* @covers Guzzle\Common\Batch\BatchClosureDivisor
*/
class BatchClosureDivisorTest extends \Guzzle\Tests\GuzzleTestCase
{
/**
* @expectedException Guzzle\Common\Exception\InvalidArgumentException
*/
public function testEnsuresCallableIsCallable()
{
$d = new BatchClosureDivisor(new \stdClass());
}
public function testDividesBatch()
{
$queue = new \SplQueue();
$queue[] = 'foo';
$queue[] = 'baz';
$d = new BatchClosureDivisor(function (\SplQueue $queue, $context) {
return array(
array('foo'),
array('baz')
);
}, 'Bar!');
$batches = $d->createBatches($queue);
$this->assertEquals(array(array('foo'), array('baz')), $batches);
}
}