| 1 |
<?php |
| 2 |
namespace Aws\EndpointDiscovery; |
| 3 |
|
| 4 |
use Aws\AbstractConfigurationProvider; |
| 5 |
use Aws\CacheInterface; |
| 6 |
use Aws\ConfigurationProviderInterface; |
| 7 |
use Aws\EndpointDiscovery\Exception\ConfigurationException; |
| 8 |
use GuzzleHttp\Promise; |
| 9 |
use GuzzleHttp\Promise\PromiseInterface; |
| 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 |
|
| 45 |
class ConfigurationProvider extends AbstractConfigurationProvider |
| 46 |
implements ConfigurationProviderInterface |
| 47 |
{ |
| 48 |
const DEFAULT_ENABLED = false; |
| 49 |
const DEFAULT_CACHE_LIMIT = 1000; |
| 50 |
const ENV_ENABLED = 'AWS_ENDPOINT_DISCOVERY_ENABLED'; |
| 51 |
const ENV_ENABLED_ALT = 'AWS_ENABLE_ENDPOINT_DISCOVERY'; |
| 52 |
const ENV_PROFILE = 'AWS_PROFILE'; |
| 53 |
|
| 54 |
public static $cacheKey = 'aws_cached_endpoint_discovery_config'; |
| 55 |
|
| 56 |
protected static $interfaceClass = ConfigurationInterface::class; |
| 57 |
protected static $exceptionClass = ConfigurationException::class; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public static function defaultProvider(array $config = []) |
| 75 |
{ |
| 76 |
$configProviders = [self::env()]; |
| 77 |
if ( |
| 78 |
!isset($config['use_aws_shared_config_files']) |
| 79 |
|| $config['use_aws_shared_config_files'] != false |
| 80 |
) { |
| 81 |
$configProviders[] = self::ini(); |
| 82 |
} |
| 83 |
$configProviders[] = self::fallback($config); |
| 84 |
|
| 85 |
$memo = self::memoize( |
| 86 |
call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders) |
| 87 |
); |
| 88 |
|
| 89 |
if (isset($config['endpoint_discovery']) |
| 90 |
&& $config['endpoint_discovery'] instanceof CacheInterface |
| 91 |
) { |
| 92 |
return self::cache($memo, $config['endpoint_discovery'], self::$cacheKey); |
| 93 |
} |
| 94 |
|
| 95 |
return $memo; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public static function env($cacheLimit = self::DEFAULT_CACHE_LIMIT) |
| 105 |
{ |
| 106 |
return function () use ($cacheLimit) { |
| 107 |
|
| 108 |
$enabled = getenv(self::ENV_ENABLED); |
| 109 |
if ($enabled === false || $enabled === '') { |
| 110 |
$enabled = getenv(self::ENV_ENABLED_ALT); |
| 111 |
} |
| 112 |
if ($enabled !== false && $enabled !== '') { |
| 113 |
return Promise\Create::promiseFor( |
| 114 |
new Configuration($enabled, $cacheLimit) |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
return self::reject('Could not find environment variable config' |
| 119 |
. ' in ' . self::ENV_ENABLED); |
| 120 |
}; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public static function fallback($config = []) |
| 133 |
{ |
| 134 |
$enabled = self::DEFAULT_ENABLED; |
| 135 |
if (!empty($config['api_provider']) |
| 136 |
&& !empty($config['service']) |
| 137 |
&& !empty($config['version']) |
| 138 |
) { |
| 139 |
$provider = $config['api_provider']; |
| 140 |
$apiData = $provider('api', $config['service'], $config['version']); |
| 141 |
if (!empty($apiData['operations'])) { |
| 142 |
foreach ($apiData['operations'] as $operation) { |
| 143 |
if (!empty($operation['endpointdiscovery']['required'])) { |
| 144 |
$enabled = true; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return function () use ($enabled) { |
| 151 |
return Promise\Create::promiseFor( |
| 152 |
new Configuration( |
| 153 |
$enabled, |
| 154 |
self::DEFAULT_CACHE_LIMIT |
| 155 |
) |
| 156 |
); |
| 157 |
}; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public static function ini( |
| 174 |
$profile = null, |
| 175 |
$filename = null, |
| 176 |
$cacheLimit = self::DEFAULT_CACHE_LIMIT |
| 177 |
) { |
| 178 |
$filename = $filename ?: (self::getDefaultConfigFilename()); |
| 179 |
$profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default'); |
| 180 |
|
| 181 |
return function () use ($profile, $filename, $cacheLimit) { |
| 182 |
if (!@is_readable($filename)) { |
| 183 |
return self::reject("Cannot read configuration from $filename"); |
| 184 |
} |
| 185 |
$data = \Aws\parse_ini_file($filename, true); |
| 186 |
if ($data === false) { |
| 187 |
return self::reject("Invalid config file: $filename"); |
| 188 |
} |
| 189 |
if (!isset($data[$profile])) { |
| 190 |
return self::reject("'$profile' not found in config file"); |
| 191 |
} |
| 192 |
if (!isset($data[$profile]['endpoint_discovery_enabled'])) { |
| 193 |
return self::reject("Required endpoint discovery config values |
| 194 |
not present in INI profile '{$profile}' ({$filename})"); |
| 195 |
} |
| 196 |
|
| 197 |
return Promise\Create::promiseFor( |
| 198 |
new Configuration( |
| 199 |
$data[$profile]['endpoint_discovery_enabled'], |
| 200 |
$cacheLimit |
| 201 |
) |
| 202 |
); |
| 203 |
}; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public static function unwrap($config) |
| 215 |
{ |
| 216 |
if (is_callable($config)) { |
| 217 |
$config = $config(); |
| 218 |
} |
| 219 |
if ($config instanceof PromiseInterface) { |
| 220 |
$config = $config->wait(); |
| 221 |
} |
| 222 |
if ($config instanceof ConfigurationInterface) { |
| 223 |
return $config; |
| 224 |
} elseif (is_array($config) && isset($config['enabled'])) { |
| 225 |
if (isset($config['cache_limit'])) { |
| 226 |
return new Configuration( |
| 227 |
$config['enabled'], |
| 228 |
$config['cache_limit'] |
| 229 |
); |
| 230 |
} |
| 231 |
return new Configuration( |
| 232 |
$config['enabled'], |
| 233 |
self::DEFAULT_CACHE_LIMIT |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
throw new \InvalidArgumentException('Not a valid endpoint_discovery ' |
| 238 |
. 'configuration argument.'); |
| 239 |
} |
| 240 |
} |