media-cloud-sync
/
includes
/
sdk
/
s3
/
Aws
/
Endpoint
/
UseDualstackEndpoint
/
ConfigurationProvider.php
ConfigurationProvider.php in Media Cloud Sync 1.1.1, at includes/sdk/s3/Aws/Endpoint/UseDualstackEndpoint/ConfigurationProvider.php
| 1 | <?php |
| 2 | |
| 3 | namespace Dudlewebs\WPMCS\s3\Aws\Endpoint\UseDualstackEndpoint; |
| 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\Endpoint\UseDualstackEndpoint\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\Endpoint\UseDualstackEndpoint\onfigurationInterface} |
| 13 | * or rejected with an {@see \Aws\Endpoint\UseDualstackEndpoint\ConfigurationException}. |
| 14 | * |
| 15 | * <code> |
| 16 | * use Aws\Endpoint\UseDualstackEndpoint\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\Endpoint\UseDualstackEndpoint\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_USE_DUAL_STACK_ENDPOINT = 'AWS_USE_DUALSTACK_ENDPOINT'; |
| 47 | const INI_USE_DUAL_STACK_ENDPOINT = 'use_dualstack_endpoint'; |
| 48 | public static $cacheKey = 'aws_cached_use_dualstack_endpoint_config'; |
| 49 | protected static $interfaceClass = ConfigurationInterface::class; |
| 50 | protected static $exceptionClass = ConfigurationException::class; |
| 51 | /** |
| 52 | * Create a default config provider that first checks for environment |
| 53 | * variables, then checks for a specified profile in the environment-defined |
| 54 | * config file location (env variable is 'AWS_CONFIG_FILE', file location |
| 55 | * defaults to ~/.aws/config), then checks for the "default" profile in the |
| 56 | * environment-defined config file location, and failing those uses a default |
| 57 | * fallback set of configuration options. |
| 58 | * |
| 59 | * This provider is automatically wrapped in a memoize function that caches |
| 60 | * previously provided config options. |
| 61 | * |
| 62 | * @param array $config |
| 63 | * |
| 64 | * @return callable |
| 65 | */ |
| 66 | public static function defaultProvider(array $config = []) |
| 67 | { |
| 68 | $region = $config['region']; |
| 69 | $configProviders = [self::env($region)]; |
| 70 | if (!isset($config['use_aws_shared_config_files']) || $config['use_aws_shared_config_files'] != \false) { |
| 71 | $configProviders[] = self::ini($region); |
| 72 | } |
| 73 | $configProviders[] = self::fallback($region); |
| 74 | $memo = self::memoize(\call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders)); |
| 75 | if (isset($config['use_dual_stack_endpoint']) && $config['use_dual_stack_endpoint'] instanceof CacheInterface) { |
| 76 | return self::cache($memo, $config['use_dual_stack_endpoint'], self::$cacheKey); |
| 77 | } |
| 78 | return $memo; |
| 79 | } |
| 80 | /** |
| 81 | * Provider that creates config from environment variables. |
| 82 | * |
| 83 | * @return callable |
| 84 | */ |
| 85 | public static function env($region) |
| 86 | { |
| 87 | return function () use($region) { |
| 88 | // Use config from environment variables, if available |
| 89 | $useDualstackEndpoint = \getenv(self::ENV_USE_DUAL_STACK_ENDPOINT); |
| 90 | if (!empty($useDualstackEndpoint)) { |
| 91 | return Promise\Create::promiseFor(new Configuration($useDualstackEndpoint, $region)); |
| 92 | } |
| 93 | return self::reject('Could not find environment variable config' . ' in ' . self::ENV_USE_DUAL_STACK_ENDPOINT); |
| 94 | }; |
| 95 | } |
| 96 | /** |
| 97 | * Config provider that creates config using a config file whose location |
| 98 | * is specified by an environment variable 'AWS_CONFIG_FILE', defaulting to |
| 99 | * ~/.aws/config if not specified |
| 100 | * |
| 101 | * @param string|null $profile Profile to use. If not specified will use |
| 102 | * the "default" profile. |
| 103 | * @param string|null $filename If provided, uses a custom filename rather |
| 104 | * than looking in the default directory. |
| 105 | * |
| 106 | * @return callable |
| 107 | */ |
| 108 | public static function ini($region, $profile = null, $filename = null) |
| 109 | { |
| 110 | $filename = $filename ?: self::getDefaultConfigFilename(); |
| 111 | $profile = $profile ?: (\getenv(self::ENV_PROFILE) ?: 'default'); |
| 112 | return function () use($region, $profile, $filename) { |
| 113 | if (!@\is_readable($filename)) { |
| 114 | return self::reject("Cannot read configuration from {$filename}"); |
| 115 | } |
| 116 | // Use INI_SCANNER_NORMAL instead of INI_SCANNER_TYPED for PHP 5.5 compatibility |
| 117 | $data = \Dudlewebs\WPMCS\s3\Aws\parse_ini_file($filename, \true, \INI_SCANNER_NORMAL); |
| 118 | if ($data === \false) { |
| 119 | return self::reject("Invalid config file: {$filename}"); |
| 120 | } |
| 121 | if (!isset($data[$profile])) { |
| 122 | return self::reject("'{$profile}' not found in config file"); |
| 123 | } |
| 124 | if (!isset($data[$profile][self::INI_USE_DUAL_STACK_ENDPOINT])) { |
| 125 | return self::reject("Required use dualstack endpoint config values\n not present in INI profile '{$profile}' ({$filename})"); |
| 126 | } |
| 127 | // INI_SCANNER_NORMAL parses false-y values as an empty string |
| 128 | if ($data[$profile][self::INI_USE_DUAL_STACK_ENDPOINT] === "") { |
| 129 | $data[$profile][self::INI_USE_DUAL_STACK_ENDPOINT] = \false; |
| 130 | } |
| 131 | return Promise\Create::promiseFor(new Configuration($data[$profile][self::INI_USE_DUAL_STACK_ENDPOINT], $region)); |
| 132 | }; |
| 133 | } |
| 134 | /** |
| 135 | * Fallback config options when other sources are not set. |
| 136 | * |
| 137 | * @return callable |
| 138 | */ |
| 139 | public static function fallback($region) |
| 140 | { |
| 141 | return function () use($region) { |
| 142 | return Promise\Create::promiseFor(new Configuration(\false, $region)); |
| 143 | }; |
| 144 | } |
| 145 | } |
| 146 |