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