Created
August 9, 2010 15:04
-
-
Save pzb/515539 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'cloudfusion/cloudfusion.class.php'; | |
class EasyAmazonSDB | |
{ | |
/* The CloudFusion SimpleDB object */ | |
private $sdb; | |
/* Result of the last operation */ | |
public $last_result = null; | |
/* Do we have the extended API? */ | |
private $xapi = false; | |
function __construct($keyid = null, $secretkey = null, $region = 'default') | |
{ | |
$this->sdb = new AmazonSDB($keyid, $secretkey); | |
$this->set_region($region); | |
$this->xapi = property_exists($this->sdb, 'extended_api'); | |
} | |
public function set_region($region) | |
{ | |
static $regions = array( | |
'us-east-1' => 'sdb.amazonaws.com', | |
'us-west-1' => 'sdb.us-west-1.amazonaws.com', | |
'eu-west-1' => 'sdb.eu-west-1.amazonaws.com', | |
'ap-southeast-1' => 'sdb.ap-southeast-1.amazonaws.com' | |
); | |
static $aliases = array( | |
'eu' => 'eu-west-1', | |
'apac' => 'ap-southeast-1', | |
'default' => 'us-east-1' | |
); | |
$region = strtolower($region); | |
// If we have a '.' assume hostname | |
if (strpos($region, '.') === FALSE) { | |
if (isset($aliases[$region])) { | |
$region = $aliases[$region]; | |
} | |
if (isset($regions[$region])) { | |
$region = $regions[$region]; | |
} | |
// No hostname now? fail | |
if (strpos($region, '.') === FALSE) { | |
return FALSE; | |
} | |
} | |
$this->sdb->set_hostname($region); | |
return TRUE; | |
} | |
public function create_domain($domain_name) | |
{ | |
$this->last_result = $this->sdb->create_domain($domain_name); | |
if (!$this->last_result->isOK()) { | |
return FALSE; | |
} | |
return TRUE; | |
} | |
public function list_domains($max_domains = null, $next_token = null) | |
{ | |
$opt = array(); | |
if (!is_null($max_domains)) { | |
$opt['MaxNumberOfDomains'] = $max_domains; | |
} | |
if (!is_null($next_token)) { | |
$opt['NextToken'] = $next_token; | |
} | |
$r = $this->last_result = $this->sdb->list_domains($opt); | |
if (!$this->last_result->isOK()) { | |
return FALSE; | |
} | |
$ret = array('DomainName' => array()); | |
if ($r->body->ListDomainsResult->NextToken) { | |
$ret['NextToken'] = $r->body->ListDomainsResult->NextToken; | |
} | |
foreach ($r->body->ListDomainsResult->DomainName as $d) { | |
$ret['DomainName'][] = (string) $d; | |
} | |
return $ret; | |
} | |
public function delete_domain($domain_name) | |
{ | |
$this->last_result = $this->sdb->delete_domain($domain_name); | |
if (!$this->last_result->isOK()) { | |
return FALSE; | |
} | |
return TRUE; | |
} | |
public function domain_metadata($domain_name) | |
{ | |
$r = $this->last_result = $this->sdb->domain_metadata($domain_name); | |
if (!$this->last_result->isOK()) { | |
return FALSE; | |
} | |
$ret = array(); | |
foreach ($r->body->DomainMetadataResult->children() as $a) { | |
$n = $a->getName(); | |
$v = (string) $a; | |
$ret[$n] = $v; | |
} | |
return $ret; | |
} | |
public function put_attributes($domain_name, $item_name, $keypairs, $replace = null, $expect= null) | |
{ | |
if (!is_null($expect)) { | |
if (!$this->xapi) { | |
$this->last_result = 'Extended API not available'; | |
return FALSE; | |
} | |
$this->last_result = $this->sdb->put_attributes($domain_name, $item_name, $keypairs, $replace, $expect); | |
} else { | |
$this->last_result = $this->sdb->put_attributes($domain_name, $item_name, $keypairs, $replace); | |
} | |
if (!$this->last_result->isOK()) { | |
return FALSE; | |
} | |
return TRUE; | |
} | |
public function batch_put_attributes($domain_name, $item_keypairs, $replace = null) | |
{ | |
$this->last_result = $this->sdb->batch_put_attributes($domain_name, $item_keypairs, $replace); | |
if (!$this->last_result->isOK()) { | |
return FALSE; | |
} | |
return TRUE; | |
} | |
public function get_attributes($domain_name, $item_name, $keys = null, $consistent = false) | |
{ | |
if ($consistent) { | |
if (!$this->xapi) { | |
$this->last_result = 'Extended API not available'; | |
return FALSE; | |
} | |
$r = $this->last_result = $this->sdb->get_attributes($domain_name, $item_name, $keys, $consistent); | |
} else { | |
$r = $this->last_result = $this->sdb->get_attributes($domain_name, $item_name, $keys); | |
} | |
if (!$this->last_result->isOK()) { | |
return FALSE; | |
} | |
$attrs = array(); | |
foreach ($r->body->GetAttributesResult->Attribute as $a) { | |
$n = (string)$a->Name; | |
$v = (string) $a->Value; | |
if (isset($attrs[$n])) { | |
if (!is_array($attrs[$n])) { | |
$attrs[$n] = array($attrs[$n]); | |
} | |
$attrs[$n][] = $v; | |
} else { | |
$attrs[$n] = $v; | |
} | |
} | |
return $attrs; | |
} | |
public function delete_attributes($domain_name, $item_name, $keys = null, $expect = null) | |
{ | |
if (!is_null($expect)) { | |
if (!$this->xapi) { | |
$this->last_result = 'Extended API not available'; | |
return FALSE; | |
} | |
$this->last_result = $this->sdb->delete_attributes($domain_name, $item_name, $keys, $expect); | |
} else { | |
$this->last_result = $this->sdb->delete_attributes($domain_name, $item_name, $keys); | |
} | |
if (!$this->last_result->isOK()) { | |
return FALSE; | |
} | |
return TRUE; | |
} | |
public function select($expression = null, $next_token = null, $consistent_read = false) | |
{ | |
$opts = array(); | |
if (!is_null($next_token)) { | |
$opts['NextToken'] = $next_token; | |
} | |
if ($consistent_read) { | |
$opts['ConsistentRead'] = 'true'; | |
} | |
$r = $this->last_result = $this->sdb->select($expression, $opts); | |
if (!$this->last_result->isOK()) { | |
return FALSE; | |
} | |
$ret = array(); | |
foreach ($r->body->SelectResult->Item as $item) { | |
$iname = (string) $item->Name; | |
$ret[$iname] = array(); | |
foreach ($item->Attribute as $attr) { | |
$n = (string) $attr->Name; | |
$v = (string) $attr->Value; | |
if (isset($ret[$iname][$n])) { | |
if (!is_array($ret[$iname][$n])) { | |
$ret[$iname][$n] = array($ret[$iname][$n]); | |
} | |
$ret[$iname][$n][] = $v; | |
} else { | |
$ret[$iname][$n] = $v; | |
} | |
} | |
} | |
return $ret; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment