123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- namespace Aws\Retry;
- use Aws\AbstractConfigurationProvider;
- use Aws\CacheInterface;
- use Aws\ConfigurationProviderInterface;
- use Aws\Retry\Exception\ConfigurationException;
- use GuzzleHttp\Promise;
- use GuzzleHttp\Promise\PromiseInterface;
- class ConfigurationProvider extends AbstractConfigurationProvider
- implements ConfigurationProviderInterface
- {
- const DEFAULT_MAX_ATTEMPTS = 3;
- const DEFAULT_MODE = 'legacy';
- const ENV_MAX_ATTEMPTS = 'AWS_MAX_ATTEMPTS';
- const ENV_MODE = 'AWS_RETRY_MODE';
- const ENV_PROFILE = 'AWS_PROFILE';
- const INI_MAX_ATTEMPTS = 'max_attempts';
- const INI_MODE = 'retry_mode';
- public static $cacheKey = 'aws_retries_config';
- protected static $interfaceClass = ConfigurationInterface::class;
- protected static $exceptionClass = ConfigurationException::class;
-
- public static function defaultProvider(array $config = [])
- {
- $configProviders = [self::env()];
- if (
- !isset($config['use_aws_shared_config_files'])
- || $config['use_aws_shared_config_files'] != false
- ) {
- $configProviders[] = self::ini();
- }
- $configProviders[] = self::fallback();
- $memo = self::memoize(
- call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders)
- );
- if (isset($config['retries'])
- && $config['retries'] instanceof CacheInterface
- ) {
- return self::cache($memo, $config['retries'], self::$cacheKey);
- }
- return $memo;
- }
-
- public static function env()
- {
- return function () {
-
- $mode = getenv(self::ENV_MODE);
- $maxAttempts = getenv(self::ENV_MAX_ATTEMPTS)
- ? getenv(self::ENV_MAX_ATTEMPTS)
- : self::DEFAULT_MAX_ATTEMPTS;
- if (!empty($mode)) {
- return Promise\Create::promiseFor(
- new Configuration($mode, $maxAttempts)
- );
- }
- return self::reject('Could not find environment variable config'
- . ' in ' . self::ENV_MODE);
- };
- }
-
- public static function fallback()
- {
- return function () {
- return Promise\Create::promiseFor(
- new Configuration(self::DEFAULT_MODE, self::DEFAULT_MAX_ATTEMPTS)
- );
- };
- }
-
- public static function ini(
- $profile = null,
- $filename = null
- ) {
- $filename = $filename ?: (self::getDefaultConfigFilename());
- $profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default');
- return function () use ($profile, $filename) {
- if (!@is_readable($filename)) {
- return self::reject("Cannot read configuration from $filename");
- }
- $data = \Aws\parse_ini_file($filename, true);
- 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_MODE])) {
- return self::reject("Required retry config values
- not present in INI profile '{$profile}' ({$filename})");
- }
- $maxAttempts = isset($data[$profile][self::INI_MAX_ATTEMPTS])
- ? $data[$profile][self::INI_MAX_ATTEMPTS]
- : self::DEFAULT_MAX_ATTEMPTS;
- return Promise\Create::promiseFor(
- new Configuration(
- $data[$profile][self::INI_MODE],
- $maxAttempts
- )
- );
- };
- }
-
- public static function unwrap($config)
- {
- if (is_callable($config)) {
- $config = $config();
- }
- if ($config instanceof PromiseInterface) {
- $config = $config->wait();
- }
- if ($config instanceof ConfigurationInterface) {
- return $config;
- }
-
-
- if (is_int($config)) {
- return new Configuration('legacy', $config + 1);
- }
- if (is_array($config) && isset($config['mode'])) {
- $maxAttempts = isset($config['max_attempts'])
- ? $config['max_attempts']
- : self::DEFAULT_MAX_ATTEMPTS;
- return new Configuration($config['mode'], $maxAttempts);
- }
- throw new \InvalidArgumentException('Not a valid retry configuration'
- . ' argument.');
- }
- }
|