123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace Aws\Endpoint\UseDualstackEndpoint;
- use Aws\AbstractConfigurationProvider;
- use Aws\CacheInterface;
- use Aws\ConfigurationProviderInterface;
- use Aws\Endpoint\UseDualstackEndpoint\Exception\ConfigurationException;
- use GuzzleHttp\Promise;
- class ConfigurationProvider extends AbstractConfigurationProvider
- implements ConfigurationProviderInterface
- {
- const ENV_USE_DUAL_STACK_ENDPOINT = 'AWS_USE_DUALSTACK_ENDPOINT';
- const INI_USE_DUAL_STACK_ENDPOINT = 'use_dualstack_endpoint';
- public static $cacheKey = 'aws_cached_use_dualstack_endpoint_config';
- protected static $interfaceClass = ConfigurationInterface::class;
- protected static $exceptionClass = ConfigurationException::class;
-
- public static function defaultProvider(array $config = [])
- {
- $region = $config['region'];
- $configProviders = [self::env($region)];
- if (
- !isset($config['use_aws_shared_config_files'])
- || $config['use_aws_shared_config_files'] != false
- ) {
- $configProviders[] = self::ini($region);
- }
- $configProviders[] = self::fallback($region);
- $memo = self::memoize(
- call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders)
- );
- if (isset($config['use_dual_stack_endpoint'])
- && $config['use_dual_stack_endpoint'] instanceof CacheInterface
- ) {
- return self::cache($memo, $config['use_dual_stack_endpoint'], self::$cacheKey);
- }
- return $memo;
- }
-
- public static function env($region)
- {
- return function () use ($region) {
-
- $useDualstackEndpoint = getenv(self::ENV_USE_DUAL_STACK_ENDPOINT);
- if (!empty($useDualstackEndpoint)) {
- return Promise\Create::promiseFor(
- new Configuration($useDualstackEndpoint, $region)
- );
- }
- return self::reject('Could not find environment variable config'
- . ' in ' . self::ENV_USE_DUAL_STACK_ENDPOINT);
- };
- }
-
- public static function ini($region, $profile = null, $filename = null)
- {
- $filename = $filename ?: (self::getDefaultConfigFilename());
- $profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default');
- return function () use ($region, $profile, $filename) {
- if (!@is_readable($filename)) {
- return self::reject("Cannot read configuration from $filename");
- }
-
- $data = \Aws\parse_ini_file($filename, true, INI_SCANNER_NORMAL);
- if ($data === false) {
- return self::reject("Invalid config file: $filename");
- }
- if (!isset($data[$profile])) {
- return self::reject("'$profile' not found in config file");
- }
- if (!isset($data[$profile][self::INI_USE_DUAL_STACK_ENDPOINT])) {
- return self::reject("Required use dualstack endpoint config values
- not present in INI profile '{$profile}' ({$filename})");
- }
-
- if ($data[$profile][self::INI_USE_DUAL_STACK_ENDPOINT] === "") {
- $data[$profile][self::INI_USE_DUAL_STACK_ENDPOINT] = false;
- }
- return Promise\Create::promiseFor(
- new Configuration($data[$profile][self::INI_USE_DUAL_STACK_ENDPOINT], $region)
- );
- };
- }
-
- public static function fallback($region)
- {
- return function () use ($region) {
- return Promise\Create::promiseFor(new Configuration(false, $region));
- };
- }
- }
|