123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
- namespace Aws\DefaultsMode;
- use Aws\AbstractConfigurationProvider;
- use Aws\CacheInterface;
- use Aws\ConfigurationProviderInterface;
- use Aws\DefaultsMode\Exception\ConfigurationException;
- use GuzzleHttp\Promise;
- use GuzzleHttp\Promise\PromiseInterface;
- class ConfigurationProvider extends AbstractConfigurationProvider
- implements ConfigurationProviderInterface
- {
- const DEFAULT_MODE = 'legacy';
- const ENV_MODE = 'AWS_DEFAULTS_MODE';
- const ENV_PROFILE = 'AWS_PROFILE';
- const INI_MODE = 'defaults_mode';
- public static $cacheKey = 'aws_defaults_mode';
- protected static $interfaceClass = ConfigurationInterface::class;
- protected static $exceptionClass = ConfigurationException::class;
-
- public static function defaultProvider(array $config = [])
- {
- $configProviders = [self::env()];
- if (
- !isset($config['use_aws_shared_config_files'])
- || $config['use_aws_shared_config_files'] != false
- ) {
- $configProviders[] = self::ini();
- }
- $configProviders[] = self::fallback();
- $memo = self::memoize(
- call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders)
- );
- if (isset($config['defaultsMode'])
- && $config['defaultsMode'] instanceof CacheInterface
- ) {
- return self::cache($memo, $config['defaultsMode'], self::$cacheKey);
- }
- return $memo;
- }
-
- public static function env()
- {
- return function () {
-
- $mode = getenv(self::ENV_MODE);
- if (!empty($mode)) {
- return Promise\Create::promiseFor(
- new Configuration($mode)
- );
- }
- return self::reject('Could not find environment variable config'
- . ' in ' . self::ENV_MODE);
- };
- }
-
- public static function fallback()
- {
- return function () {
- return Promise\Create::promiseFor(
- new Configuration( self::DEFAULT_MODE)
- );
- };
- }
-
- public static function ini(
- $profile = null,
- $filename = null
- ) {
- $filename = $filename ?: (self::getDefaultConfigFilename());
- $profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default');
- return function () use ($profile, $filename) {
- if (!is_readable($filename)) {
- return self::reject("Cannot read configuration from $filename");
- }
- $data = \Aws\parse_ini_file($filename, true);
- if ($data === false) {
- return self::reject("Invalid config file: $filename");
- }
- if (!isset($data[$profile])) {
- return self::reject("'$profile' not found in config file");
- }
- if (!isset($data[$profile][self::INI_MODE])) {
- return self::reject("Required defaults mode config values
- not present in INI profile '{$profile}' ({$filename})");
- }
- return Promise\Create::promiseFor(
- new Configuration(
- $data[$profile][self::INI_MODE]
- )
- );
- };
- }
-
- public static function unwrap($config)
- {
- if (is_callable($config)) {
- $config = $config();
- }
- if ($config instanceof PromiseInterface) {
- $config = $config->wait();
- }
- if ($config instanceof ConfigurationInterface) {
- return $config;
- }
- if (is_string($config)) {
- return new Configuration($config);
- }
- throw new \InvalidArgumentException('Not a valid defaults mode configuration'
- . ' argument.');
- }
- }
|