| 1 |
<?php |
| 2 |
namespace Aws\Endpoint\UseDualstackEndpoint; |
| 3 |
|
| 4 |
use Aws\AbstractConfigurationProvider; |
| 5 |
use Aws\CacheInterface; |
| 6 |
use Aws\ConfigurationProviderInterface; |
| 7 |
use Aws\Endpoint\UseDualstackEndpoint\Exception\ConfigurationException; |
| 8 |
use GuzzleHttp\Promise; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class ConfigurationProvider extends AbstractConfigurationProvider |
| 45 |
implements ConfigurationProviderInterface |
| 46 |
{ |
| 47 |
const ENV_USE_DUAL_STACK_ENDPOINT = 'AWS_USE_DUALSTACK_ENDPOINT'; |
| 48 |
const INI_USE_DUAL_STACK_ENDPOINT = 'use_dualstack_endpoint'; |
| 49 |
|
| 50 |
public static $cacheKey = 'aws_cached_use_dualstack_endpoint_config'; |
| 51 |
|
| 52 |
protected static $interfaceClass = ConfigurationInterface::class; |
| 53 |
protected static $exceptionClass = ConfigurationException::class; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public static function defaultProvider(array $config = []) |
| 71 |
{ |
| 72 |
$region = $config['region']; |
| 73 |
$configProviders = [self::env($region)]; |
| 74 |
if ( |
| 75 |
!isset($config['use_aws_shared_config_files']) |
| 76 |
|| $config['use_aws_shared_config_files'] != false |
| 77 |
) { |
| 78 |
$configProviders[] = self::ini($region); |
| 79 |
} |
| 80 |
$configProviders[] = self::fallback($region); |
| 81 |
|
| 82 |
$memo = self::memoize( |
| 83 |
call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders) |
| 84 |
); |
| 85 |
|
| 86 |
if (isset($config['use_dual_stack_endpoint']) |
| 87 |
&& $config['use_dual_stack_endpoint'] instanceof CacheInterface |
| 88 |
) { |
| 89 |
return self::cache($memo, $config['use_dual_stack_endpoint'], self::$cacheKey); |
| 90 |
} |
| 91 |
|
| 92 |
return $memo; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public static function env($region) |
| 101 |
{ |
| 102 |
return function () use ($region) { |
| 103 |
|
| 104 |
$useDualstackEndpoint = getenv(self::ENV_USE_DUAL_STACK_ENDPOINT); |
| 105 |
if (!empty($useDualstackEndpoint)) { |
| 106 |
return Promise\Create::promiseFor( |
| 107 |
new Configuration($useDualstackEndpoint, $region) |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
return self::reject('Could not find environment variable config' |
| 112 |
. ' in ' . self::ENV_USE_DUAL_STACK_ENDPOINT); |
| 113 |
}; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public static function ini($region, $profile = null, $filename = null) |
| 129 |
{ |
| 130 |
$filename = $filename ?: (self::getDefaultConfigFilename()); |
| 131 |
$profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default'); |
| 132 |
|
| 133 |
return function () use ($region, $profile, $filename) { |
| 134 |
if (!@is_readable($filename)) { |
| 135 |
return self::reject("Cannot read configuration from $filename"); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
$data = \Aws\parse_ini_file($filename, true, INI_SCANNER_NORMAL); |
| 140 |
if ($data === false) { |
| 141 |
return self::reject("Invalid config file: $filename"); |
| 142 |
} |
| 143 |
if (!isset($data[$profile])) { |
| 144 |
return self::reject("'$profile' not found in config file"); |
| 145 |
} |
| 146 |
if (!isset($data[$profile][self::INI_USE_DUAL_STACK_ENDPOINT])) { |
| 147 |
return self::reject("Required use dualstack endpoint config values |
| 148 |
not present in INI profile '{$profile}' ({$filename})"); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
if ($data[$profile][self::INI_USE_DUAL_STACK_ENDPOINT] === "") { |
| 153 |
$data[$profile][self::INI_USE_DUAL_STACK_ENDPOINT] = false; |
| 154 |
} |
| 155 |
|
| 156 |
return Promise\Create::promiseFor( |
| 157 |
new Configuration($data[$profile][self::INI_USE_DUAL_STACK_ENDPOINT], $region) |
| 158 |
); |
| 159 |
}; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public static function fallback($region) |
| 168 |
{ |
| 169 |
return function () use ($region) { |
| 170 |
return Promise\Create::promiseFor(new Configuration(false, $region)); |
| 171 |
}; |
| 172 |
} |
| 173 |
} |