PluginProbe
Media Cloud Sync / 1.2.2
Media Cloud Sync v1.2.2
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 / EndpointDiscovery / ConfigurationProvider.php

ConfigurationProvider.php in Media Cloud Sync 1.2.2, at includes/sdk/s3/Aws/EndpointDiscovery/ConfigurationProvider.php

191 lines 8.3 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\EndpointDiscovery;
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\EndpointDiscovery\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\EndpointDiscovery\ConfigurationInterface}
14 * or rejected with an {@see \Aws\EndpointDiscovery\Exception\ConfigurationException}.
15 *
16 * <code>
17 * use Aws\EndpointDiscovery\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\EndpointDiscovery\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_ENABLED = \false;
48 const DEFAULT_CACHE_LIMIT = 1000;
49 const ENV_ENABLED = 'AWS_ENDPOINT_DISCOVERY_ENABLED';
50 const ENV_ENABLED_ALT = 'AWS_ENABLE_ENDPOINT_DISCOVERY';
51 const ENV_PROFILE = 'AWS_PROFILE';
52 public static $cacheKey = 'aws_cached_endpoint_discovery_config';
53 protected static $interfaceClass = ConfigurationInterface::class;
54 protected static $exceptionClass = ConfigurationException::class;
55 /**
56 * Create a default config provider that first checks for environment
57 * variables, then checks for a specified profile in the environment-defined
58 * config file location (env variable is 'AWS_CONFIG_FILE', file location
59 * defaults to ~/.aws/config), then checks for the "default" profile in the
60 * environment-defined config file location, and failing those uses a default
61 * fallback set of configuration options.
62 *
63 * This provider is automatically wrapped in a memoize function that caches
64 * previously provided config options.
65 *
66 * @param array $config
67 *
68 * @return callable
69 */
70 public static function defaultProvider(array $config = [])
71 {
72 $configProviders = [self::env()];
73 if (!isset($config['use_aws_shared_config_files']) || $config['use_aws_shared_config_files'] != \false) {
74 $configProviders[] = self::ini();
75 }
76 $configProviders[] = self::fallback($config);
77 $memo = self::memoize(\call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders));
78 if (isset($config['endpoint_discovery']) && $config['endpoint_discovery'] instanceof CacheInterface) {
79 return self::cache($memo, $config['endpoint_discovery'], self::$cacheKey);
80 }
81 return $memo;
82 }
83 /**
84 * Provider that creates config from environment variables.
85 *
86 * @param $cacheLimit
87 * @return callable
88 */
89 public static function env($cacheLimit = self::DEFAULT_CACHE_LIMIT)
90 {
91 return function () use($cacheLimit) {
92 // Use config from environment variables, if available
93 $enabled = \getenv(self::ENV_ENABLED);
94 if ($enabled === \false || $enabled === '') {
95 $enabled = \getenv(self::ENV_ENABLED_ALT);
96 }
97 if ($enabled !== \false && $enabled !== '') {
98 return Promise\Create::promiseFor(new Configuration($enabled, $cacheLimit));
99 }
100 return self::reject('Could not find environment variable config' . ' in ' . self::ENV_ENABLED);
101 };
102 }
103 /**
104 * Fallback config options when other sources are not set. Will check the
105 * service model for any endpoint discovery required operations, and enable
106 * endpoint discovery in that case. If no required operations found, will use
107 * the class default values.
108 *
109 * @param array $config
110 * @return callable
111 */
112 public static function fallback($config = [])
113 {
114 $enabled = self::DEFAULT_ENABLED;
115 if (!empty($config['api_provider']) && !empty($config['service']) && !empty($config['version'])) {
116 $provider = $config['api_provider'];
117 $apiData = $provider('api', $config['service'], $config['version']);
118 if (!empty($apiData['operations'])) {
119 foreach ($apiData['operations'] as $operation) {
120 if (!empty($operation['endpointdiscovery']['required'])) {
121 $enabled = \true;
122 }
123 }
124 }
125 }
126 return function () use($enabled) {
127 return Promise\Create::promiseFor(new Configuration($enabled, self::DEFAULT_CACHE_LIMIT));
128 };
129 }
130 /**
131 * Config provider that creates config using a config file whose location
132 * is specified by an environment variable 'AWS_CONFIG_FILE', defaulting to
133 * ~/.aws/config if not specified
134 *
135 * @param string|null $profile Profile to use. If not specified will use
136 * the "default" profile.
137 * @param string|null $filename If provided, uses a custom filename rather
138 * than looking in the default directory.
139 * @param int $cacheLimit
140 *
141 * @return callable
142 */
143 public static function ini($profile = null, $filename = null, $cacheLimit = self::DEFAULT_CACHE_LIMIT)
144 {
145 $filename = $filename ?: self::getDefaultConfigFilename();
146 $profile = $profile ?: (\getenv(self::ENV_PROFILE) ?: 'default');
147 return function () use($profile, $filename, $cacheLimit) {
148 if (!@\is_readable($filename)) {
149 return self::reject("Cannot read configuration from {$filename}");
150 }
151 $data = \Dudlewebs\WPMCS\s3\Aws\parse_ini_file($filename, \true);
152 if ($data === \false) {
153 return self::reject("Invalid config file: {$filename}");
154 }
155 if (!isset($data[$profile])) {
156 return self::reject("'{$profile}' not found in config file");
157 }
158 if (!isset($data[$profile]['endpoint_discovery_enabled'])) {
159 return self::reject("Required endpoint discovery config values\n not present in INI profile '{$profile}' ({$filename})");
160 }
161 return Promise\Create::promiseFor(new Configuration($data[$profile]['endpoint_discovery_enabled'], $cacheLimit));
162 };
163 }
164 /**
165 * Unwraps a configuration object in whatever valid form it is in,
166 * always returning a ConfigurationInterface object.
167 *
168 * @param mixed $config
169 * @return ConfigurationInterface
170 * @throws \InvalidArgumentException
171 */
172 public static function unwrap($config)
173 {
174 if (\is_callable($config)) {
175 $config = $config();
176 }
177 if ($config instanceof PromiseInterface) {
178 $config = $config->wait();
179 }
180 if ($config instanceof ConfigurationInterface) {
181 return $config;
182 } elseif (\is_array($config) && isset($config['enabled'])) {
183 if (isset($config['cache_limit'])) {
184 return new Configuration($config['enabled'], $config['cache_limit']);
185 }
186 return new Configuration($config['enabled'], self::DEFAULT_CACHE_LIMIT);
187 }
188 throw new \InvalidArgumentException('Not a valid endpoint_discovery ' . 'configuration argument.');
189 }
190 }
191