Skip to content

Commit

Permalink
Now uses the manifest file to resolve service names when calling Sdk:…
Browse files Browse the repository at this point in the history
…:createClient
  • Loading branch information
jeremeamia committed Jun 18, 2015
1 parent d62abf0 commit a5260c6
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 66 deletions.
4 changes: 1 addition & 3 deletions src/Api/ApiProvider.php
Expand Up @@ -91,9 +91,7 @@ public static function resolve(callable $provider, $type, $service, $version)
*/
public static function defaultProvider()
{
$dir = __DIR__ . '/../data';

return new self($dir, \Aws\load_compiled_json("$dir/manifest.json"));
return new self(__DIR__ . '/../data', \Aws\manifest());
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/AwsClient.php
Expand Up @@ -119,13 +119,14 @@ public static function getArguments()
*
* @param array $args Client configuration arguments.
*
* @throws \InvalidArgumentException if any required options are missing
* @throws \InvalidArgumentException if any required options are missing or
* the service is not supported.
*/
public function __construct(array $args)
{
list($service, $exceptionClass) = $this->parseClass();
if (!isset($args['service'])) {
$args['service'] = Sdk::getEndpointPrefix($service);
$args['service'] = manifest($service)['endpoint'];
}
if (!isset($args['exception_class'])) {
$args['exception_class'] = $exceptionClass;
Expand Down
73 changes: 30 additions & 43 deletions src/Sdk.php
Expand Up @@ -55,19 +55,6 @@ class Sdk
{
const VERSION = '3.0.4';

/** @var array Map of custom lowercase names to service endpoint names. */
private static $aliases = [
'configservice' => 'config',
'cloudwatch' => 'monitoring',
'cloudwatchlogs' => 'logs',
'cognitoidentity' => 'cognito-identity',
'cognitosync' => 'cognito-sync',
'directoryservice' => 'ds',
'efs' => 'elasticfilesystem',
'emr' => 'elasticmapreduce',
'ses' => 'email',
];

/** @var array Arguments for creating clients */
private $args;

Expand Down Expand Up @@ -101,49 +88,49 @@ public function __call($name, array $args = [])
throw new \BadMethodCallException("Unknown method: {$name}.");
}

/**
* Create an endpoint prefix name from a namespace.
*
* @param string $name Namespace name
*
* @return string
*/
public static function getEndpointPrefix($name)
{
$name = strtolower($name);

return isset(self::$aliases[$name]) ? self::$aliases[$name] : $name;
}

/**
* Get a client by name using an array of constructor options.
*
* @param string $name Client namespace name (e.g., DynamoDb).
* @param array $args Custom arguments to provide to the client.
* @param string $name Service name or namespace (e.g., DynamoDb, s3).
* @param array $args Arguments to configure the client.
*
* @return AwsClientInterface
* @throws \InvalidArgumentException
* @see Aws\AwsClient::__construct for a list of available options.
* @throws \InvalidArgumentException if any required options are missing or
* the service is not supported.
* @see Aws\AwsClient::__construct for a list of available options for args.
*/
public function createClient($name, array $args = [])
{
// Merge provided args with stored args
if (isset($this->args[$name])) {
$args += $this->args[$name];
}
// Get information about the service from the manifest file.
$service = manifest($name);
$namespace = $service['namespace'];

$args += $this->args;
// Merge provided args with stored, service-specific args.
if (isset($this->args[$namespace])) {
$args += $this->args[$namespace];
}

// Provide the endpoint prefix in the args.
if (!isset($args['service'])) {
$args['service'] = self::getEndpointPrefix($name);
$args['service'] = $service['endpoint'];
}

$client = "Aws\\{$name}\\{$name}Client";

if (!class_exists($client)) {
$client = 'Aws\\AwsClient';
}
// Instantiate the client class.
$client = "Aws\\{$namespace}\\{$namespace}Client";
return new $client($args + $this->args);
}

return new $client($args);
/**
* Determine the endpoint prefix from a client namespace.
*
* @param string $name Namespace name
*
* @return string
* @internal
* @deprecated Use the `\Aws\manifest()` function instead.
*/
public static function getEndpointPrefix($name)
{
return manifest($name)['endpoint'];
}
}
45 changes: 45 additions & 0 deletions src/functions.php
Expand Up @@ -309,3 +309,48 @@ function (CommandInterface $_, RequestInterface $r) use (&$request) {

return $request;
}

/**
* Retrieves data for a service from the SDK's service manifest file.
*
* Manifest data is stored statically, so it does not need to be loaded more
* than once per process. The JSON data is also cached in opcache.
*
* @param string $service Case-insensitive namespace or endpoint prefix of the
* service for which you are retrieving manifest data.
*
* @return RequestInterface
* @throws \InvalidArgumentException if the service is not supported.
*/
function manifest($service = null)
{
// Load the manifest and create aliases for lowercased namespaces
static $manifest = [];
static $aliases = [];
if (empty($manifest)) {
$manifest = load_compiled_json(__DIR__ . '/data/manifest.json');
foreach ($manifest as $endpoint => $info) {
$alias = strtolower($info['namespace']);
if ($alias !== $endpoint) {
$aliases[$alias] = $endpoint;
}
}
}

// If no service specified, then return the whole manifest.
if ($service === null) {
return $manifest;
}

// Look up the service's info in the manifest data.
$service = strtolower($service);
if (isset($manifest[$service])) {
return $manifest[$service] + ['endpoint' => $service];
} elseif (isset($aliases[$service])) {
return manifest($aliases[$service]);
} else {
throw new \InvalidArgumentException(
"The service \"{$service}\" is not provided by the AWS SDK for PHP."
);
}
}
21 changes: 3 additions & 18 deletions tests/SdkTest.php
Expand Up @@ -13,7 +13,7 @@ class SdkTest extends \PHPUnit_Framework_TestCase
*/
public function testEnsuresMissingMethodThrowsException()
{
(new Sdk())->foo();
(new Sdk)->foo();
}

public function testHasMagicMethods()
Expand All @@ -31,7 +31,7 @@ public function testCreatesClients()
{
$this->assertInstanceOf(
'Aws\AwsClientInterface',
(new Sdk())->createDynamoDb([
(new Sdk)->createDynamoDb([
'region' => 'us-east-1',
'version' => 'latest'
])
Expand All @@ -42,25 +42,10 @@ public function testCreatesClientsWithAlias()
{
$this->assertInstanceOf(
'Aws\AwsClientInterface',
(new Sdk())->createCloudWatch([
(new Sdk)->createCloudWatch([
'region' => 'us-east-1',
'version' => 'latest'
])
);
}

/**
* @expectedException \Aws\Exception\UnresolvedApiException
*/
public function testCreatesGenericClient()
{
// Use a config that contains a service-specific config.
$sdk = new Sdk([
'version' => 'latest',
'foo' => ['region' => 'us-east-1']
]);

// Create a client with an unknown name.
$client = $sdk->createClient('foo');
}
}

0 comments on commit a5260c6

Please sign in to comment.