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

[Service] Cleaning up code

This commit is contained in:
Michael Dowling 2011-03-28 15:47:49 -05:00
parent 675c979094
commit 9a63bb5112
2 changed files with 64 additions and 108 deletions

View File

@ -19,42 +19,12 @@ class ApiCommand
/**
* @var array Arguments
*/
protected $args;
protected $args = array();
/**
* @var string API action name
* @var array Configuration data
*/
protected $name;
/**
* @var string HTTP method of the command
*/
protected $method;
/**
* @var int Minimum number of arguments required by the command
*/
protected $minArgs = 0;
/**
* @var string Path routing information of the command to include in the path
*/
protected $path = '';
/**
* @var string Command documentation
*/
protected $doc;
/**
* @var bool Whether or not the command can be sent in a batch request
*/
protected $canBatch = true;
/**
* @var string Concrete class that this ApiCommand is associated with
*/
protected $concreteCommandClass = 'Guzzle\\Service\\Command\\ClosureCommand';
protected $config = array();
/**
* Constructor
@ -82,19 +52,16 @@ class ApiCommand
* prepend - Text to prepend when adding this value to a location
* append - Text to append when adding to a location
*/
public function __construct($config)
public function __construct(array $config)
{
$this->name = isset($config['name']) ? trim($config['name']) : '';
$this->doc = isset($config['doc']) ? trim($config['doc']) : '';
$this->method = isset($config['method']) ? trim($config['method']) : '';
$this->minArgs = isset($config['min_args']) ? min(100, max(0, $config['min_args'])) : '';
$this->canBatch = isset($config['can_batch']) ? $config['can_batch'] : '';
$this->path = isset($config['path']) ? trim($config['path']) : '';
$this->args = array();
if (isset($config['class'])) {
$this->concreteCommandClass = $config['class'];
}
$this->config = $config;
$this->config['name'] = isset($config['name']) ? trim($config['name']) : '';
$this->config['doc'] = isset($config['doc']) ? trim($config['doc']) : '';
$this->config['method'] = isset($config['method']) ? trim($config['method']) : '';
$this->config['min_args'] = isset($config['min_args']) ? min(100, max(0, $config['min_args'])) : 0;
$this->config['can_batch'] = isset($config['can_batch']) ? $config['can_batch'] : '';
$this->config['path'] = isset($config['path']) ? trim($config['path']) : '';
$this->config['class'] = isset($config['class']) ? trim($config['class']) : 'Guzzle\\Service\\Command\\ClosureCommand';
// Build the argument array
if (isset($config['args']) && is_array($config['args'])) {
@ -144,7 +111,7 @@ class ApiCommand
*/
public function getMethod()
{
return $this->method;
return $this->config['method'];
}
/**
@ -154,7 +121,7 @@ class ApiCommand
*/
public function getConcreteClass()
{
return $this->concreteCommandClass;
return $this->config['class'];
}
/**
@ -164,7 +131,7 @@ class ApiCommand
*/
public function getName()
{
return $this->name;
return $this->config['name'];
}
/**
@ -174,7 +141,7 @@ class ApiCommand
*/
public function getDoc()
{
return $this->doc;
return $this->config['doc'];
}
/**
@ -184,7 +151,7 @@ class ApiCommand
*/
public function getMinArgs()
{
return $this->minArgs;
return $this->config['min_args'];
}
/**
@ -195,7 +162,7 @@ class ApiCommand
*/
public function getPath()
{
return $this->path;
return $this->config['path'];
}
/**
@ -205,7 +172,7 @@ class ApiCommand
*/
public function canBatch()
{
return $this->canBatch;
return $this->config['can_batch'];
}
/**
@ -220,14 +187,13 @@ class ApiCommand
public function validate(Collection $config)
{
$errors = array();
// Validate that the right number of args has been supplied
if ($this->minArgs && count($config) < $this->minArgs) {
$errors[] = $this->name . ' requires at least ' . $this->minArgs . ' arguments';
if ($this->config['min_args'] && count($config) < $this->config['min_args']) {
$errors[] = $this->config['name'] . ' requires at least '
. $this->config['min_args'] . ' arguments';
}
$e = Inspector::getInstance()->validateConfig($this->args, $config, false);
if (is_array($e)) {
$errors = array_merge($errors, $e);
}

View File

@ -42,62 +42,52 @@ class ServiceBuilder
public static function factory($filename, CacheAdapterInterface $cacheAdapter = null, $ttl = 86400)
{
// Compute the cache key for this service and check if it exists in cache
$key = 'guz_service_' . md5($filename);
$cached = $cacheAdapter ? $cacheAdapter->fetch($key) : false;
if ($cached) {
// Load the config from cache
$config = unserialize($cached);
} else {
// Build the service config from the XML file if the file exists
if (!is_file($filename)) {
throw new \RuntimeException('Unable to open service configuration file ' . $filename);
}
$config = array();
$xml = new \SimpleXMLElement($filename, null, true);
// Create a client entry for each client in the XML file
foreach ($xml->clients->client as $client) {
$row = array();
$name = (string) $client->attributes()->name;
$class = (string) $client->attributes()->class;
// Check if this client builder extends another client
if ($extends = (string) $client->attributes()->extends) {
// Make sure that the service it's extending has been defined
if (!isset($config[$extends])) {
throw new \LogicException($name . ' is trying to extend a non-existent or not yet defined service: ' . $extends);
}
$class = $class ?: $config[$extends]['class'];
$row = $config[$extends]['params'];
}
// Add attributes to the row's parameters
foreach ($client->param as $param) {
$row[(string) $param->attributes()->name] = (string) $param->attributes()->value;
}
// Add this client builder
$config[$name] = array(
'class' => str_replace('.', '\\', $class),
'params' => $row
);
}
if ($cacheAdapter) {
$cacheAdapter->save($key, serialize($config), $ttl);
if ($cacheAdapter) {
$key = 'guz_service_' . md5($filename);
$cached = $cacheAdapter ? $cacheAdapter->fetch($key) : false;
if ($cached) {
return new self(unserialize($cached));
}
}
// Build the service config from the XML file if the file exists
if (!is_file($filename)) {
throw new \RuntimeException('Unable to open service configuration file ' . $filename);
}
$builder = new self($config);
$config = array();
$xml = new \SimpleXMLElement($filename, null, true);
return $builder;
// Create a client entry for each client in the XML file
foreach ($xml->clients->client as $client) {
$row = array();
$name = (string) $client->attributes()->name;
$class = (string) $client->attributes()->class;
// Check if this client builder extends another client
if ($extends = (string) $client->attributes()->extends) {
// Make sure that the service it's extending has been defined
if (!isset($config[$extends])) {
throw new \LogicException($name . ' is trying to extend a non-existent or not yet defined service: ' . $extends);
}
$class = $class ?: $config[$extends]['class'];
$row = $config[$extends]['params'];
}
// Add attributes to the row's parameters
foreach ($client->param as $param) {
$row[(string) $param->attributes()->name] = (string) $param->attributes()->value;
}
// Add this client builder
$config[$name] = array(
'class' => str_replace('.', '\\', $class),
'params' => $row
);
}
if ($cacheAdapter) {
$cacheAdapter->save($key, serialize($config), $ttl);
}
return new self($config);
}
/**