PluginProbe
Media Cloud Sync / 1.2.12
Media Cloud Sync v1.2.12
1.4.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 All 36 releases
media-cloud-sync / includes / sdk / s3 / Aws / S3 / RegionalEndpoint / ConfigurationProvider.php

ConfigurationProvider.php in Media Cloud Sync 1.2.12, at includes/sdk/s3/Aws/S3/RegionalEndpoint/ConfigurationProvider.php

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