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

[Service] Cleaning up Command API

This commit is contained in:
Michael Dowling 2011-03-28 00:36:17 -05:00
parent 1dce958700
commit 675c979094
7 changed files with 18 additions and 38 deletions

View File

@ -307,7 +307,7 @@ class Client extends AbstractSubject
{
if ($command instanceof CommandInterface) {
$command->prepare($this);
$command->setClient($this)->prepare();
$this->getEventManager()->notify('command.before_send', $command);
$command->getRequest()->send();
$this->getEventManager()->notify('command.after_send', $command);

View File

@ -58,10 +58,9 @@ abstract class AbstractCommand extends Collection implements CommandInterface
{
parent::__construct($parameters);
$this->apiCommand = $apiCommand;
// Add arguments and validate the command
if ($this->apiCommand) {
if ($apiCommand) {
$this->apiCommand = $apiCommand;
Inspector::getInstance()->validateConfig($apiCommand->getArgs(), $this, false);
} else if (!($this instanceof ClosureCommand)) {
Inspector::getInstance()->validateClass(get_class($this), $this, false);
@ -225,33 +224,25 @@ abstract class AbstractCommand extends Collection implements CommandInterface
}
/**
* Prepare the command for executing.
*
* Create a request object for the command.
*
* @param Client $client (optional) The client object used to execute the command
* Prepare the command for executing and create a request object.
*
* @return RequestInterface Returns the generated request
* @throws RuntimeException if a client object has not been set previously
* or in the prepare()
*/
public function prepare(Client $client = null)
public function prepare()
{
if (!$this->isPrepared()) {
if ($client) {
$this->client = $client;
}
if (!$this->client) {
throw new \RuntimeException('A Client object must be associated with the command before it can be prepared.');
}
// Fail on missing required arguments when it is not a ClosureCommand
if (!($this instanceof ClosureCommand)) {
if ($this->getApiCommand() instanceof NullObject) {
Inspector::getInstance()->validateClass(get_class($this), $this, true);
if ($this->apiCommand) {
Inspector::getInstance()->validateConfig($this->apiCommand->getArgs(), $this);
} else {
Inspector::getInstance()->validateConfig($this->getApiCommand()->getArgs(), $this);
Inspector::getInstance()->validateClass(get_class($this), $this, true);
}
}

View File

@ -114,17 +114,13 @@ interface CommandInterface
public function isExecuted();
/**
* Prepare the command for executing.
*
* Create a request object for the command.
*
* @param Client $client (optional) The client object used to execute the command
* Prepare the command for executing and create a request object.
*
* @return RequestInterface Returns the generated request
* @throws RuntimeException if a client object has not been set previously
* or in the prepare()
*/
public function prepare(Client $client = null);
public function prepare();
/**
* Get the object that manages the request headers that will be set on any

View File

@ -111,9 +111,7 @@ class CommandSet implements \IteratorAggregate, \Countable, Observer
// Execute all batched commands in parallel
$parallel = $this->getParallelCommands();
if (count($parallel)) {
$this->pool->reset();
// Prepare each request and send out Client notifications
foreach ($parallel as $command) {
$request = $command->prepare();
@ -122,7 +120,6 @@ class CommandSet implements \IteratorAggregate, \Countable, Observer
$command->getClient()->getEventManager()->notify('command.before_send', $command);
$this->pool->add($request);
}
$this->pool->send();
}

View File

@ -68,7 +68,7 @@ class ClosureCommandTest extends \Guzzle\Tests\GuzzleTestCase
));
$client = $this->getServiceBuilder()->get('mock');
$c->prepare($client);
$c->setClient($client)->prepare();
$this->assertEquals('123', $c->get('testing'));
$this->assertEquals('http://www.test.com/', $c->getRequest()->getUrl());
}
@ -88,6 +88,6 @@ class ClosureCommandTest extends \Guzzle\Tests\GuzzleTestCase
));
$client = $this->getServiceBuilder()->get('mock');
$c->prepare($client);
$c->setClient($client)->prepare();
}
}

View File

@ -102,7 +102,7 @@ class CommandTest extends AbstractCommandTest
$command = new MockCommand();
$client = $this->getClient();
$command->prepare($client);
$command->setClient($client)->prepare();
$this->assertEquals($client, $command->getClient());
$this->assertTrue($command->isPrepared());
}
@ -202,9 +202,7 @@ class CommandTest extends AbstractCommandTest
$this->assertInstanceOf('Guzzle\Common\Collection', $command->getRequestHeaders());
$this->assertEquals('123', $command->getRequestHeaders()->get('test'));
$client = $this->getClient();
$command->prepare($client);
$command->setClient($this->getClient())->prepare();
$this->assertEquals('123', $command->getRequest()->getHeaders()->get('test'));
}
@ -228,8 +226,7 @@ class CommandTest extends AbstractCommandTest
$command = new MockCommand(array(), $api);
$this->assertSame($api, $command->getApiCommand());
$client = $this->getClient();
$command->prepare($client);
$command->setClient($this->getClient())->prepare();
$this->assertEquals('123', $command->get('test'));
$this->assertSame($api, $command->getApiCommand($api));
}

View File

@ -104,8 +104,7 @@ class DynamicCommandFactoryTest extends \Guzzle\Tests\GuzzleTestCase
'bucket' => 'test',
'key' => 'key'
));
$request = $command->prepare($client);
$request = $command->setClient($client)->prepare();
// Ensure that the path values were injected into the path and base_url
$this->assertEquals('/key', $request->getPath());
@ -149,7 +148,7 @@ class DynamicCommandFactoryTest extends \Guzzle\Tests\GuzzleTestCase
'h' => 'haha'
));
$request = $command->prepare($client);
$request = $command->setClient($client)->prepare();
$this->assertEquals(
"PUT /?test=abc&i=test HTTP/1.1\r\n" .
@ -171,7 +170,7 @@ class DynamicCommandFactoryTest extends \Guzzle\Tests\GuzzleTestCase
'i' => 'does not change the value because it\'s static'
));
$request = $command->prepare($client);
$request = $command->setClient($client)->prepare();
$this->assertEquals(
"PUT /?test=abc&i=test HTTP/1.1\r\n" .