public function __construct(array $options = array())
{
// Instantiate the utilities class.
$this->util = new $this->utilities_class();
// Determine the current service.
$this->service = get_class($this);
// Create credentials based on the options
$runtime_credentials = new CFCredential($options);
$credentials_provided = false;
// Retrieve a credential set from config.inc.php if it exists
if (isset($options['credentials']))
{
// Use a specific credential set and merge with the runtime credentials
$this->credentials = CFCredentials::get($options['credentials'])
->merge($runtime_credentials);
}
else
{
try
{
// Use the default credential set and merge with the runtime credentials
$this->credentials = CFCredentials::get(CFCredentials::DEFAULT_KEY)
->merge($runtime_credentials);
}
catch (CFCredentials_Exception $e)
{
// Only the runtime credentials were provided
$this->credentials = $runtime_credentials;
}
}
// Check if keys were actually provided
if (isset($this->credentials['key']) && isset($this->credentials['secret']))
{
$credentials_provided = true;
}
// Check for an instance profile credentials override
if (isset($this->credentials['use_instance_profile_credentials']) && $this->credentials['use_instance_profile_credentials'])
{
$credentials_provided = false;
}
// Automatically enable whichever caching mechanism is set to default.
$this->set_cache_config($this->credentials->default_cache_config);
// If no credentials were provided, try to get them from the EC2 instance profile
if (!$credentials_provided)
{
// Default caching mechanism is required
if (!$this->credentials->default_cache_config)
{
// @codeCoverageIgnoreStart
throw new CFCredentials_Exception('No credentials were provided. The SDK attempts to retrieve Instance '
. 'Profile credentials from the EC2 Instance Metadata Service, but doing this requires the '
. '"default_cache_config" option to be set in the config.inc.php file or constructor. In order to '
. 'cache the retrieved credentials.');
// @codeCoverageIgnoreEnd
}
// Instantiate and invoke the cache for instance profile credentials
$cache = new $this->cache_class('instance_profile_credentials', $this->cache_location, 0, $this->cache_compress);
if ($data = $cache->read())
{
$cache->expire_in((strtotime($data['expires']) - time()) * 0.85);
}
$instance_profile_credentials = $cache->response_manager(array($this, 'cache_instance_profile_credentials'), array($cache, $options));
$this->credentials->key = $instance_profile_credentials['key'];
$this->credentials->secret = $instance_profile_credentials['secret'];
$this->credentials->token = $instance_profile_credentials['token'];
}
// Set internal credentials after they are resolved
$this->key = $this->credentials->key;
$this->secret_key = $this->credentials->secret;
$this->auth_token = $this->credentials->token;
}