Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Retry/ConfigurationProvider.php

raw symbols SCIP
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 * A configuration provider is a function that returns a promise that is
14 * fulfilled with a {@see \Aws\Retry\ConfigurationInterface}
15 * or rejected with an {@see \Aws\Retry\Exception\ConfigurationException}.
16 *
17 * <code>
18 * use Aws\Sts\RegionalEndpoints\ConfigurationProvider;
19 * $provider = ConfigurationProvider::defaultProvider();
20 * // Returns a ConfigurationInterface or throws.
21 * $config = $provider()->wait();
22 * </code>
23 *
24 * Configuration providers can be composed to create configuration using
25 * conditional logic that can create different configurations in different
26 * environments. You can compose multiple providers into a single provider using
27 * {@see \Aws\Retry\ConfigurationProvider::chain}. This function
28 * accepts providers as variadic arguments and returns a new function that will
29 * invoke each provider until a successful configuration is returned.
30 *
31 * <code>
32 * // First try an INI file at this location.
33 * $a = ConfigurationProvider::ini(null, '/path/to/file.ini');
34 * // Then try an INI file at this location.
35 * $b = ConfigurationProvider::ini(null, '/path/to/other-file.ini');
36 * // Then try loading from environment variables.
37 * $c = ConfigurationProvider::env();
38 * // Combine the three providers together.
39 * $composed = ConfigurationProvider::chain($a, $b, $c);
40 * // Returns a promise that is fulfilled with a configuration or throws.
41 * $promise = $composed();
42 * // Wait on the configuration to resolve.
43 * $config = $promise->wait();
44 * </code>
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 * Create a default config provider that first checks for environment
64 * variables, then checks for a specified profile in the environment-defined
65 * config file location (env variable is 'AWS_CONFIG_FILE', file location
66 * defaults to ~/.aws/config), then checks for the "default" profile in the
67 * environment-defined config file location, and failing those uses a default
68 * fallback set of configuration options.
69 *
70 * This provider is automatically wrapped in a memoize function that caches
71 * previously provided config options.
72 *
73 * @param array $config
74 *
75 * @return callable
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 * Provider that creates config from environment variables.
103 *
104 * @return callable
105 */
106 public static function env()
107 {
108 return function () {
109 // Use config from environment variables, if available
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 * Fallback config options when other sources are not set.
127 *
128 * @return callable
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 * Returns the default retry mode. Reflects the AWS_NEW_RETRIES_2026
141 * opt-in: 'standard' when the env flag is set, 'legacy' otherwise.
142 */
143 public static function getDefaultMode(): string
144 {
145 return OptIn::isEnabled() ? 'standard' : self::DEFAULT_MODE;
146 }
147
148 /**
149 * Config provider that creates config using a config file whose location
150 * is specified by an environment variable 'AWS_CONFIG_FILE', defaulting to
151 * ~/.aws/config if not specified
152 *
153 * @param string|null $profile Profile to use. If not specified will use
154 * the "default" profile.
155 * @param string|null $filename If provided, uses a custom filename rather
156 * than looking in the default directory.
157 *
158 * @return callable
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 * Unwraps a configuration object in whatever valid form it is in,
198 * always returning a ConfigurationInterface object.
199 *
200 * @param mixed $config
201 * @return ConfigurationInterface
202 * @throws \InvalidArgumentException
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 // An integer value for this config indicates the legacy 'retries'
217 // config option, which is incremented to translate to max attempts
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 }