| 1 |
<?php |
| 2 |
namespace Aws\S3\RegionalEndpoint; |
| 3 |
|
| 4 |
use Aws\AbstractConfigurationProvider; |
| 5 |
use Aws\CacheInterface; |
| 6 |
use Aws\ConfigurationProviderInterface; |
| 7 |
use Aws\S3\RegionalEndpoint\Exception\ConfigurationException; |
| 8 |
use GuzzleHttp\Promise; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class ConfigurationProvider extends AbstractConfigurationProvider |
| 45 |
implements ConfigurationProviderInterface |
| 46 |
{ |
| 47 |
const ENV_ENDPOINTS_TYPE = 'AWS_S3_US_EAST_1_REGIONAL_ENDPOINT'; |
| 48 |
const INI_ENDPOINTS_TYPE = 's3_us_east_1_regional_endpoint'; |
| 49 |
const DEFAULT_ENDPOINTS_TYPE = 'legacy'; |
| 50 |
|
| 51 |
public static $cacheKey = 'aws_s3_us_east_1_regional_endpoint_config'; |
| 52 |
|
| 53 |
protected static $interfaceClass = ConfigurationInterface::class; |
| 54 |
protected static $exceptionClass = ConfigurationException::class; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public static function defaultProvider(array $config = []) |
| 72 |
{ |
| 73 |
$configProviders = [self::env()]; |
| 74 |
if ( |
| 75 |
!isset($config['use_aws_shared_config_files']) |
| 76 |
|| $config['use_aws_shared_config_files'] != false |
| 77 |
) { |
| 78 |
$configProviders[] = self::ini(); |
| 79 |
} |
| 80 |
$configProviders[] = self::fallback(); |
| 81 |
|
| 82 |
$memo = self::memoize( |
| 83 |
call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders) |
| 84 |
); |
| 85 |
|
| 86 |
if (isset($config['s3_us_east_1_regional_endpoint']) |
| 87 |
&& $config['s3_us_east_1_regional_endpoint'] instanceof CacheInterface |
| 88 |
) { |
| 89 |
return self::cache($memo, $config['s3_us_east_1_regional_endpoint'], self::$cacheKey); |
| 90 |
} |
| 91 |
|
| 92 |
return $memo; |
| 93 |
} |
| 94 |
|
| 95 |
public static function env() |
| 96 |
{ |
| 97 |
return function () { |
| 98 |
|
| 99 |
$endpointsType = getenv(self::ENV_ENDPOINTS_TYPE); |
| 100 |
if (!empty($endpointsType)) { |
| 101 |
return Promise\Create::promiseFor( |
| 102 |
new Configuration($endpointsType) |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
return self::reject('Could not find environment variable config' |
| 107 |
. ' in ' . self::ENV_ENDPOINTS_TYPE); |
| 108 |
}; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public static function ini( |
| 124 |
$profile = null, |
| 125 |
$filename = null |
| 126 |
) { |
| 127 |
$filename = $filename ?: (self::getDefaultConfigFilename()); |
| 128 |
$profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default'); |
| 129 |
|
| 130 |
return function () use ($profile, $filename) { |
| 131 |
if (!@is_readable($filename)) { |
| 132 |
return self::reject("Cannot read configuration from $filename"); |
| 133 |
} |
| 134 |
$data = \Aws\parse_ini_file($filename, true); |
| 135 |
if ($data === false) { |
| 136 |
return self::reject("Invalid config file: $filename"); |
| 137 |
} |
| 138 |
if (!isset($data[$profile])) { |
| 139 |
return self::reject("'$profile' not found in config file"); |
| 140 |
} |
| 141 |
if (!isset($data[$profile][self::INI_ENDPOINTS_TYPE])) { |
| 142 |
return self::reject("Required S3 regional endpoint config values |
| 143 |
not present in INI profile '{$profile}' ({$filename})"); |
| 144 |
} |
| 145 |
|
| 146 |
return Promise\Create::promiseFor( |
| 147 |
new Configuration($data[$profile][self::INI_ENDPOINTS_TYPE]) |
| 148 |
); |
| 149 |
}; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public static function fallback() |
| 158 |
{ |
| 159 |
return function () { |
| 160 |
return Promise\Create::promiseFor( |
| 161 |
new Configuration(self::DEFAULT_ENDPOINTS_TYPE, true) |
| 162 |
); |
| 163 |
}; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public static function unwrap($config) |
| 175 |
{ |
| 176 |
if (is_callable($config)) { |
| 177 |
$config = $config(); |
| 178 |
} |
| 179 |
if ($config instanceof Promise\PromiseInterface) { |
| 180 |
$config = $config->wait(); |
| 181 |
} |
| 182 |
if ($config instanceof ConfigurationInterface) { |
| 183 |
return $config; |
| 184 |
} |
| 185 |
if (is_string($config)) { |
| 186 |
return new Configuration($config); |
| 187 |
} |
| 188 |
if (is_array($config) && isset($config['endpoints_type'])) { |
| 189 |
return new Configuration($config['endpoints_type']); |
| 190 |
} |
| 191 |
|
| 192 |
throw new \InvalidArgumentException('Not a valid S3 regional endpoint ' |
| 193 |
. 'configuration argument.'); |
| 194 |
} |
| 195 |
} |