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

Syncing up the command interface and abstract command class. Removing extraneous methods. Coding standards updates.

This commit is contained in:
Michael Dowling 2011-03-20 18:46:47 -05:00
parent 8cd23cefb1
commit 6ef91c5e13
7 changed files with 37 additions and 66 deletions

View File

@ -52,6 +52,7 @@ abstract class AbstractCommand extends Collection implements CommandInterface
*
* @param array|Collection $parameters (optional) Collection of parameters
* to set on the command
* @param ApiCommand $apiCommand (optional) Command definition from description
*/
public function __construct($parameters = null, ApiCommand $apiCommand = null)
{
@ -83,20 +84,6 @@ abstract class AbstractCommand extends Collection implements CommandInterface
return $this->apiCommand ?: new NullObject();
}
/**
* Set the API command associated with the command
*
* @param ApiCommand $apiCommand API command information
*
* @return AbstractCommand
*/
public function setApiCommand(ApiCommand $apiCommand)
{
$this->apiCommand = $apiCommand;
return $this;
}
/**
* Get whether or not the command can be batched
*
@ -263,28 +250,14 @@ abstract class AbstractCommand extends Collection implements CommandInterface
}
/**
* Set an HTTP header on the outbound request
*
* @param string $header The name of the header to set
* @param string $value The value to set on the header
*
* @return AbstractCommand
*/
public function setRequestHeader($header, $value)
{
$this->get('headers')->set($header, $value);
return $this;
}
/**
* Get the object that manages the request headers
* Get the object that manages the request headers that will be set on any
* outbound requests from the command
*
* @return Collection
*/
public function getRequestHeaders()
{
return ($this->request) ? $this->request->getHeaders() : $this->get('headers');
return $this->get('headers', new Collection());
}
/**

View File

@ -10,6 +10,7 @@ use Guzzle\Common\Collection;
use Guzzle\Http\Message\Response;
use Guzzle\Http\Message\RequestInterface;
use Guzzle\Service\Client;
use Guzzle\Service\Description\ApiCommand;
/**
* Command object to handle preparing and processing client requests and
@ -24,8 +25,16 @@ interface CommandInterface
*
* @param array|Collection $parameters (optional) Collection of parameters
* to set on the command
* @param ApiCommand $apiCommand (optional) Command definition from description
*/
public function __construct($parameters = null);
public function __construct($parameters = null, ApiCommand $apiCommand = null);
/**
* Get the API command information about the command
*
* @return ApiCommand|NullObject
*/
public function getApiCommand();
/**
* Get whether or not the command can be batched
@ -49,6 +58,15 @@ interface CommandInterface
*/
public function getClient();
/**
* Set the client objec that will execute the command
*
* @param Client $client The client objec that will execute the command
*
* @return Command
*/
public function setClient(Client $client);
/**
* Get the request object associated with the command
*
@ -95,33 +113,15 @@ interface CommandInterface
*
* @param Client $client (optional) The client object used to execute the command
*
* @return Command Provides a fluent interface.
* @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);
/**
* Set the client objec that will execute the command
*
* @param Client $client The client objec that will execute the command
*
* @return Command
*/
public function setClient(Client $client);
/**
* Set an HTTP header on the outbound request
*
* @param string $header The name of the header to set
* @param string $value The value to set on the header
*
* @return AbstractCommand
*/
public function setRequestHeader($header, $value);
/**
* Get the object that manages the request headers
* Get the object that manages the request headers that will be set on any
* outbound requests from the command
*
* @return Collection
*/

View File

@ -44,7 +44,7 @@ class CommandSet implements \IteratorAggregate, \Countable
}
}
$this->pool = (!$pool) ? new Pool() : $pool;
$this->pool = $pool ?: new Pool();
}
/**
@ -168,8 +168,8 @@ class CommandSet implements \IteratorAggregate, \Countable
*/
public function hasCommand($command)
{
return (bool)(count(array_filter($this->commands, function($value) use ($command) {
return (is_string($command)) ? ($value instanceof $command) : ($value === $command);
return (bool) (count(array_filter($this->commands, function($value) use ($command) {
return is_string($command) ? ($value instanceof $command) : ($value === $command);
})) > 0);
}
@ -184,7 +184,7 @@ class CommandSet implements \IteratorAggregate, \Countable
public function removeCommand($command)
{
$this->commands = array_values(array_filter($this->commands, function($value) use ($command) {
return (is_string($command)) ? (!($value instanceof $command)) : ($value !== $command);
return is_string($command) ? !($value instanceof $command) : ($value !== $command);
}));
return $this;

View File

@ -29,6 +29,7 @@ class DynamicCommandFactory implements CommandFactoryInterface
{
if ($command->getConcreteClass() != 'Guzzle\\Service\\Command\\ClosureCommand') {
$class = $command->getConcreteClass();
return new $class($args);
}

View File

@ -91,7 +91,7 @@ class XmlDescriptionBuilder implements DescriptionBuilderInterface
'doc' => (string) $command->doc,
'method' => (string) $attr->method,
'path' => (string) $attr->path,
'min_args' => (int)(string) $attr->min_args,
'min_args' => (int) (string) $attr->min_args,
'can_batch' => (string) $attr->can_batch == 'false' ? false : true,
'class' => (string) $attr->class ?: ServiceDescription::DEFAULT_COMMAND_CLASS,
'args' => $args

View File

@ -95,10 +95,10 @@ abstract class ResourceIterator extends AbstractSubject implements \Iterator, \C
{
$this->client = $client;
$this->data = $data;
$this->limit = (array_key_exists('limit', $data)) ? $data['limit'] : -1;
$this->pageSize = (array_key_exists('page_size', $data)) ? $data['page_size'] : false;
$this->resourceList = (array_key_exists('resources', $data)) ? $data['resources'] : array();
$this->nextToken = (array_key_exists('next_token', $data)) ? $data['next_token'] : false;
$this->limit = array_key_exists('limit', $data) ? $data['limit'] : -1;
$this->pageSize = array_key_exists('page_size', $data) ? $data['page_size'] : false;
$this->resourceList = array_key_exists('resources', $data) ? $data['resources'] : array();
$this->nextToken = array_key_exists('next_token', $data) ? $data['next_token'] : false;
$this->retrievedCount = count($this->resourceList);
$this->onLoad();
}

View File

@ -174,13 +174,12 @@ class CommandTest extends AbstractCommandTest
/**
* @covers Guzzle\Service\Command\AbstractCommand::prepare
* @covers Guzzle\Service\Command\AbstractCommand::setRequestHeader
* @covers Guzzle\Service\Command\AbstractCommand::getRequestHeaders
*/
public function testCommandsAllowsCustomRequestHeaders()
{
$command = new MockCommand();
$command->setRequestHeader('test', '123');
$command->getRequestHeaders()->set('test', '123');
$this->assertInstanceOf('Guzzle\Common\Collection', $command->getRequestHeaders());
$this->assertEquals('123', $command->getRequestHeaders()->get('test'));
@ -213,8 +212,6 @@ class CommandTest extends AbstractCommandTest
$client = $this->getClient();
$command->prepare($client);
$this->assertEquals('123', $command->get('test'));
$this->assertSame($command, $command->setApiCommand($api));
$this->assertSame($api, $command->getApiCommand($api));
}
}