PluginProbe
Media Cloud Sync / 1.2.9
Media Cloud Sync v1.2.9
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / Aws / Retry / ConfigurationProvider.php

ConfigurationProvider.php in Media Cloud Sync 1.2.9, at includes/sdk/s3/Aws/Retry/ConfigurationProvider.php

178 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\Aws\Retry;
4
5 use Dudlewebs\WPMCS\s3\Aws\AbstractConfigurationProvider;
6 use Dudlewebs\WPMCS\s3\Aws\CacheInterface;
7 use Dudlewebs\WPMCS\s3\Aws\ConfigurationProviderInterface;
8 use Dudlewebs\WPMCS\s3\Aws\Retry\Exception\ConfigurationException;
9 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise;
10 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromiseInterface;
11 /**
12 * A configuration provider is a function that returns a promise that is
13 * fulfilled with a {@see \Aws\Retry\ConfigurationInterface}
14 * or rejected with an {@see \Aws\Retry\Exception\ConfigurationException}.
15 *
16 * <code>
17 * use Aws\Sts\RegionalEndpoints\ConfigurationProvider;
18 * $provider = ConfigurationProvider::defaultProvider();
19 * // Returns a ConfigurationInterface or throws.
20 * $config = $provider()->wait();
21 * </code>
22 *
23 * Configuration providers can be composed to create configuration using
24 * conditional logic that can create different configurations in different
25 * environments. You can compose multiple providers into a single provider using
26 * {@see \Aws\Retry\ConfigurationProvider::chain}. This function
27 * accepts providers as variadic arguments and returns a new function that will
28 * invoke each provider until a successful configuration is returned.
29 *
30 * <code>
31 * // First try an INI file at this location.
32 * $a = ConfigurationProvider::ini(null, '/path/to/file.ini');
33 * // Then try an INI file at this location.
34 * $b = ConfigurationProvider::ini(null, '/path/to/other-file.ini');
35 * // Then try loading from environment variables.
36 * $c = ConfigurationProvider::env();
37 * // Combine the three providers together.
38 * $composed = ConfigurationProvider::chain($a, $b, $c);
39 * // Returns a promise that is fulfilled with a configuration or throws.
40 * $promise = $composed();
41 * // Wait on the configuration to resolve.
42 * $config = $promise->wait();
43 * </code>
44 */
45 class ConfigurationProvider extends AbstractConfigurationProvider implements ConfigurationProviderInterface
46 {
47 const DEFAULT_MAX_ATTEMPTS = 3;
48 const DEFAULT_MODE = 'legacy';
49 const ENV_MAX_ATTEMPTS = 'AWS_MAX_ATTEMPTS';
50 const ENV_MODE = 'AWS_RETRY_MODE';
51 const ENV_PROFILE = 'AWS_PROFILE';
52 const INI_MAX_ATTEMPTS = 'max_attempts';
53 const INI_MODE = 'retry_mode';
54 public static $cacheKey = 'aws_retries_config';
55 protected static $interfaceClass = ConfigurationInterface::class;
56 protected static $exceptionClass = ConfigurationException::class;
57 /**
58 * Create a default config provider that first checks for environment
59 * variables, then checks for a specified profile in the environment-defined
60 * config file location (env variable is 'AWS_CONFIG_FILE', file location
61 * defaults to ~/.aws/config), then checks for the "default" profile in the
62 * environment-defined config file location, and failing those uses a default
63 * fallback set of configuration options.
64 *
65 * This provider is automatically wrapped in a memoize function that caches
66 * previously provided config options.
67 *
68 * @param array $config
69 *
70 * @return callable
71 */
72 public static function defaultProvider(array $config = [])
73 {
74 $configProviders = [self::env()];
75 if (!isset($config['use_aws_shared_config_files']) || $config['use_aws_shared_config_files'] != \false) {
76 $configProviders[] = self::ini();
77 }
78 $configProviders[] = self::fallback();
79 $memo = self::memoize(\call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders));
80 if (isset($config['retries']) && $config['retries'] instanceof CacheInterface) {
81 return self::cache($memo, $config['retries'], self::$cacheKey);
82 }
83 return $memo;
84 }
85 /**
86 * Provider that creates config from environment variables.
87 *
88 * @return callable
89 */
90 public static function env()
91 {
92 return function () {
93 // Use config from environment variables, if available
94 $mode = \getenv(self::ENV_MODE);
95 $maxAttempts = \getenv(self::ENV_MAX_ATTEMPTS) ? \getenv(self::ENV_MAX_ATTEMPTS) : self::DEFAULT_MAX_ATTEMPTS;
96 if (!empty($mode)) {
97 return Promise\Create::promiseFor(new Configuration($mode, $maxAttempts));
98 }
99 return self::reject('Could not find environment variable config' . ' in ' . self::ENV_MODE);
100 };
101 }
102 /**
103 * Fallback config options when other sources are not set.
104 *
105 * @return callable
106 */
107 public static function fallback()
108 {
109 return function () {
110 return Promise\Create::promiseFor(new Configuration(self::DEFAULT_MODE, self::DEFAULT_MAX_ATTEMPTS));
111 };
112 }
113 /**
114 * Config provider that creates config using a config file whose location
115 * is specified by an environment variable 'AWS_CONFIG_FILE', defaulting to
116 * ~/.aws/config if not specified
117 *
118 * @param string|null $profile Profile to use. If not specified will use
119 * the "default" profile.
120 * @param string|null $filename If provided, uses a custom filename rather
121 * than looking in the default directory.
122 *
123 * @return callable
124 */
125 public static function ini($profile = null, $filename = null)
126 {
127 $filename = $filename ?: self::getDefaultConfigFilename();
128 $profile = $profile ?: (\getenv(self::ENV_PROFILE) ?: 'default');
129 return function () use($profile, $filename) {
130 if (!@\is_readable($filename)) {
131 return self::reject("Cannot read configuration from {$filename}");
132 }
133 $data = \Dudlewebs\WPMCS\s3\Aws\parse_ini_file($filename, \true);
134 if ($data === \false) {
135 return self::reject("Invalid config file: {$filename}");
136 }
137 if (!isset($data[$profile])) {
138 return self::reject("'{$profile}' not found in config file");
139 }
140 if (!isset($data[$profile][self::INI_MODE])) {
141 return self::reject("Required retry config values\n not present in INI profile '{$profile}' ({$filename})");
142 }
143 $maxAttempts = isset($data[$profile][self::INI_MAX_ATTEMPTS]) ? $data[$profile][self::INI_MAX_ATTEMPTS] : self::DEFAULT_MAX_ATTEMPTS;
144 return Promise\Create::promiseFor(new Configuration($data[$profile][self::INI_MODE], $maxAttempts));
145 };
146 }
147 /**
148 * Unwraps a configuration object in whatever valid form it is in,
149 * always returning a ConfigurationInterface object.
150 *
151 * @param mixed $config
152 * @return ConfigurationInterface
153 * @throws \InvalidArgumentException
154 */
155 public static function unwrap($config)
156 {
157 if (\is_callable($config)) {
158 $config = $config();
159 }
160 if ($config instanceof PromiseInterface) {
161 $config = $config->wait();
162 }
163 if ($config instanceof ConfigurationInterface) {
164 return $config;
165 }
166 // An integer value for this config indicates the legacy 'retries'
167 // config option, which is incremented to translate to max attempts
168 if (\is_int($config)) {
169 return new Configuration('legacy', $config + 1);
170 }
171 if (\is_array($config) && isset($config['mode'])) {
172 $maxAttempts = isset($config['max_attempts']) ? $config['max_attempts'] : self::DEFAULT_MAX_ATTEMPTS;
173 return new Configuration($config['mode'], $maxAttempts);
174 }
175 throw new \InvalidArgumentException('Not a valid retry configuration' . ' argument.');
176 }
177 }
178