| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\S3\UseArnRegion; |
| 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\UseArnRegion\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\UseArnRegion\ConfigurationInterface} |
| 13 |
* or rejected with an {@see \Aws\S3\UseArnRegion\Exception\ConfigurationException}. |
| 14 |
* |
| 15 |
* <code> |
| 16 |
* use Aws\S3\UseArnRegion\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\UseArnRegion\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_ARN_REGION = 'AWS_S3_USE_ARN_REGION'; |
| 47 |
const INI_USE_ARN_REGION = 's3_use_arn_region'; |
| 48 |
const DEFAULT_USE_ARN_REGION = \false; |
| 49 |
public static $cacheKey = 'aws_s3_use_arn_region_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['use_arn_region']) && $config['use_arn_region'] instanceof CacheInterface) { |
| 76 |
return self::cache($memo, $config['use_arn_region'], 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() |
| 86 |
{ |
| 87 |
return function () { |
| 88 |
// Use config from environment variables, if available |
| 89 |
$useArnRegion = \getenv(self::ENV_USE_ARN_REGION); |
| 90 |
if (!empty($useArnRegion)) { |
| 91 |
return Promise\Create::promiseFor(new Configuration($useArnRegion)); |
| 92 |
} |
| 93 |
return self::reject('Could not find environment variable config' . ' in ' . self::ENV_USE_ARN_REGION); |
| 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($profile = null, $filename = null) |
| 109 |
{ |
| 110 |
$filename = $filename ?: self::getDefaultConfigFilename(); |
| 111 |
$profile = $profile ?: (\getenv(self::ENV_PROFILE) ?: 'default'); |
| 112 |
return function () use($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_ARN_REGION])) { |
| 125 |
return self::reject("Required S3 Use Arn Region 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_ARN_REGION] === "") { |
| 129 |
$data[$profile][self::INI_USE_ARN_REGION] = \false; |
| 130 |
} |
| 131 |
return Promise\Create::promiseFor(new Configuration($data[$profile][self::INI_USE_ARN_REGION])); |
| 132 |
}; |
| 133 |
} |
| 134 |
/** |
| 135 |
* Fallback config options when other sources are not set. |
| 136 |
* |
| 137 |
* @return callable |
| 138 |
*/ |
| 139 |
public static function fallback() |
| 140 |
{ |
| 141 |
return function () { |
| 142 |
return Promise\Create::promiseFor(new Configuration(self::DEFAULT_USE_ARN_REGION)); |
| 143 |
}; |
| 144 |
} |
| 145 |
} |
| 146 |
|