| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\DefaultsMode; |
| 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\DefaultsMode\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\DefaultsMode\ConfigurationInterface} |
| 14 |
* or rejected with an {@see \Aws\DefaultsMode\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\DefaultsMode\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_MODE = 'legacy'; |
| 48 |
const ENV_MODE = 'AWS_DEFAULTS_MODE'; |
| 49 |
const ENV_PROFILE = 'AWS_PROFILE'; |
| 50 |
const INI_MODE = 'defaults_mode'; |
| 51 |
public static $cacheKey = 'aws_defaults_mode'; |
| 52 |
protected static $interfaceClass = ConfigurationInterface::class; |
| 53 |
protected static $exceptionClass = ConfigurationException::class; |
| 54 |
/** |
| 55 |
* Create a default config provider that first checks for environment |
| 56 |
* variables, then checks for a specified profile in the environment-defined |
| 57 |
* config file location (env variable is 'AWS_CONFIG_FILE', file location |
| 58 |
* defaults to ~/.aws/config), then checks for the "default" profile in the |
| 59 |
* environment-defined config file location, and failing those uses a default |
| 60 |
* fallback set of configuration options. |
| 61 |
* |
| 62 |
* This provider is automatically wrapped in a memoize function that caches |
| 63 |
* previously provided config options. |
| 64 |
* |
| 65 |
* @param array $config |
| 66 |
* |
| 67 |
* @return callable |
| 68 |
*/ |
| 69 |
public static function defaultProvider(array $config = []) |
| 70 |
{ |
| 71 |
$configProviders = [self::env()]; |
| 72 |
if (!isset($config['use_aws_shared_config_files']) || $config['use_aws_shared_config_files'] != \false) { |
| 73 |
$configProviders[] = self::ini(); |
| 74 |
} |
| 75 |
$configProviders[] = self::fallback(); |
| 76 |
$memo = self::memoize(\call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders)); |
| 77 |
if (isset($config['defaultsMode']) && $config['defaultsMode'] instanceof CacheInterface) { |
| 78 |
return self::cache($memo, $config['defaultsMode'], self::$cacheKey); |
| 79 |
} |
| 80 |
return $memo; |
| 81 |
} |
| 82 |
/** |
| 83 |
* Provider that creates config from environment variables. |
| 84 |
* |
| 85 |
* @return callable |
| 86 |
*/ |
| 87 |
public static function env() |
| 88 |
{ |
| 89 |
return function () { |
| 90 |
// Use config from environment variables, if available |
| 91 |
$mode = \getenv(self::ENV_MODE); |
| 92 |
if (!empty($mode)) { |
| 93 |
return Promise\Create::promiseFor(new Configuration($mode)); |
| 94 |
} |
| 95 |
return self::reject('Could not find environment variable config' . ' in ' . self::ENV_MODE); |
| 96 |
}; |
| 97 |
} |
| 98 |
/** |
| 99 |
* Fallback config options when other sources are not set. |
| 100 |
* |
| 101 |
* @return callable |
| 102 |
*/ |
| 103 |
public static function fallback() |
| 104 |
{ |
| 105 |
return function () { |
| 106 |
return Promise\Create::promiseFor(new Configuration(self::DEFAULT_MODE)); |
| 107 |
}; |
| 108 |
} |
| 109 |
/** |
| 110 |
* Config provider that creates config using a config file whose location |
| 111 |
* is specified by an environment variable 'AWS_CONFIG_FILE', defaulting to |
| 112 |
* ~/.aws/config if not specified |
| 113 |
* |
| 114 |
* @param string|null $profile Profile to use. If not specified will use |
| 115 |
* the "default" profile. |
| 116 |
* @param string|null $filename If provided, uses a custom filename rather |
| 117 |
* than looking in the default directory. |
| 118 |
* |
| 119 |
* @return callable |
| 120 |
*/ |
| 121 |
public static function ini($profile = null, $filename = null) |
| 122 |
{ |
| 123 |
$filename = $filename ?: self::getDefaultConfigFilename(); |
| 124 |
$profile = $profile ?: (\getenv(self::ENV_PROFILE) ?: 'default'); |
| 125 |
return function () use($profile, $filename) { |
| 126 |
if (!\is_readable($filename)) { |
| 127 |
return self::reject("Cannot read configuration from {$filename}"); |
| 128 |
} |
| 129 |
$data = \Dudlewebs\WPMCS\s3\Aws\parse_ini_file($filename, \true); |
| 130 |
if ($data === \false) { |
| 131 |
return self::reject("Invalid config file: {$filename}"); |
| 132 |
} |
| 133 |
if (!isset($data[$profile])) { |
| 134 |
return self::reject("'{$profile}' not found in config file"); |
| 135 |
} |
| 136 |
if (!isset($data[$profile][self::INI_MODE])) { |
| 137 |
return self::reject("Required defaults mode config values\n not present in INI profile '{$profile}' ({$filename})"); |
| 138 |
} |
| 139 |
return Promise\Create::promiseFor(new Configuration($data[$profile][self::INI_MODE])); |
| 140 |
}; |
| 141 |
} |
| 142 |
/** |
| 143 |
* Unwraps a configuration object in whatever valid form it is in, |
| 144 |
* always returning a ConfigurationInterface object. |
| 145 |
* |
| 146 |
* @param mixed $config |
| 147 |
* @return ConfigurationInterface |
| 148 |
* @throws \InvalidArgumentException |
| 149 |
*/ |
| 150 |
public static function unwrap($config) |
| 151 |
{ |
| 152 |
if (\is_callable($config)) { |
| 153 |
$config = $config(); |
| 154 |
} |
| 155 |
if ($config instanceof PromiseInterface) { |
| 156 |
$config = $config->wait(); |
| 157 |
} |
| 158 |
if ($config instanceof ConfigurationInterface) { |
| 159 |
return $config; |
| 160 |
} |
| 161 |
if (\is_string($config)) { |
| 162 |
return new Configuration($config); |
| 163 |
} |
| 164 |
throw new \InvalidArgumentException('Not a valid defaults mode configuration' . ' argument.'); |
| 165 |
} |
| 166 |
} |
| 167 |
|