123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <?php
- namespace Aws\S3\RegionalEndpoint;
- use Aws\AbstractConfigurationProvider;
- use Aws\CacheInterface;
- use Aws\ConfigurationProviderInterface;
- use Aws\S3\RegionalEndpoint\Exception\ConfigurationException;
- use GuzzleHttp\Promise;
- class ConfigurationProvider extends AbstractConfigurationProvider
- implements ConfigurationProviderInterface
- {
- const ENV_ENDPOINTS_TYPE = 'AWS_S3_US_EAST_1_REGIONAL_ENDPOINT';
- const INI_ENDPOINTS_TYPE = 's3_us_east_1_regional_endpoint';
- const DEFAULT_ENDPOINTS_TYPE = 'legacy';
- public static $cacheKey = 'aws_s3_us_east_1_regional_endpoint_config';
- 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['s3_us_east_1_regional_endpoint'])
- && $config['s3_us_east_1_regional_endpoint'] instanceof CacheInterface
- ) {
- return self::cache($memo, $config['s3_us_east_1_regional_endpoint'], self::$cacheKey);
- }
- return $memo;
- }
- public static function env()
- {
- return function () {
-
- $endpointsType = getenv(self::ENV_ENDPOINTS_TYPE);
- if (!empty($endpointsType)) {
- return Promise\Create::promiseFor(
- new Configuration($endpointsType)
- );
- }
- return self::reject('Could not find environment variable config'
- . ' in ' . self::ENV_ENDPOINTS_TYPE);
- };
- }
-
- 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_ENDPOINTS_TYPE])) {
- return self::reject("Required S3 regional endpoint config values
- not present in INI profile '{$profile}' ({$filename})");
- }
- return Promise\Create::promiseFor(
- new Configuration($data[$profile][self::INI_ENDPOINTS_TYPE])
- );
- };
- }
-
- public static function fallback()
- {
- return function () {
- return Promise\Create::promiseFor(
- new Configuration(self::DEFAULT_ENDPOINTS_TYPE, true)
- );
- };
- }
-
- public static function unwrap($config)
- {
- if (is_callable($config)) {
- $config = $config();
- }
- if ($config instanceof Promise\PromiseInterface) {
- $config = $config->wait();
- }
- if ($config instanceof ConfigurationInterface) {
- return $config;
- }
- if (is_string($config)) {
- return new Configuration($config);
- }
- if (is_array($config) && isset($config['endpoints_type'])) {
- return new Configuration($config['endpoints_type']);
- }
- throw new \InvalidArgumentException('Not a valid S3 regional endpoint '
- . 'configuration argument.');
- }
- }
|