vendor/aws/aws-sdk-php/src/S3/RegionalEndpoint/ConfigurationProvider.php line 83

Open in your IDE?
  1. <?php
  2. namespace Aws\S3\RegionalEndpoint;
  3. use Aws\AbstractConfigurationProvider;
  4. use Aws\CacheInterface;
  5. use Aws\ConfigurationProviderInterface;
  6. use Aws\S3\RegionalEndpoint\Exception\ConfigurationException;
  7. use GuzzleHttp\Promise;
  8. /**
  9.  * A configuration provider is a function that returns a promise that is
  10.  * fulfilled with a {@see \Aws\S3\RegionalEndpoint\ConfigurationInterface}
  11.  * or rejected with an {@see \Aws\S3\RegionalEndpoint\Exception\ConfigurationException}.
  12.  *
  13.  * <code>
  14.  * use Aws\S3\RegionalEndpoint\ConfigurationProvider;
  15.  * $provider = ConfigurationProvider::defaultProvider();
  16.  * // Returns a ConfigurationInterface or throws.
  17.  * $config = $provider()->wait();
  18.  * </code>
  19.  *
  20.  * Configuration providers can be composed to create configuration using
  21.  * conditional logic that can create different configurations in different
  22.  * environments. You can compose multiple providers into a single provider using
  23.  * {@see \Aws\S3\RegionalEndpoint\ConfigurationProvider::chain}. This function
  24.  * accepts providers as variadic arguments and returns a new function that will
  25.  * invoke each provider until a successful configuration is returned.
  26.  *
  27.  * <code>
  28.  * // First try an INI file at this location.
  29.  * $a = ConfigurationProvider::ini(null, '/path/to/file.ini');
  30.  * // Then try an INI file at this location.
  31.  * $b = ConfigurationProvider::ini(null, '/path/to/other-file.ini');
  32.  * // Then try loading from environment variables.
  33.  * $c = ConfigurationProvider::env();
  34.  * // Combine the three providers together.
  35.  * $composed = ConfigurationProvider::chain($a, $b, $c);
  36.  * // Returns a promise that is fulfilled with a configuration or throws.
  37.  * $promise = $composed();
  38.  * // Wait on the configuration to resolve.
  39.  * $config = $promise->wait();
  40.  * </code>
  41.  */
  42. class ConfigurationProvider extends AbstractConfigurationProvider
  43.     implements ConfigurationProviderInterface
  44. {
  45.     const ENV_ENDPOINTS_TYPE 'AWS_S3_US_EAST_1_REGIONAL_ENDPOINT';
  46.     const INI_ENDPOINTS_TYPE 's3_us_east_1_regional_endpoint';
  47.     const DEFAULT_ENDPOINTS_TYPE 'legacy';
  48.     public static $cacheKey 'aws_s3_us_east_1_regional_endpoint_config';
  49.     protected static $interfaceClass ConfigurationInterface::class;
  50.     protected static $exceptionClass ConfigurationException::class;
  51.     /**
  52.      * Create a default config provider that first checks for environment
  53.      * variables, then checks for a specified profile in the environment-defined
  54.      * config file location (env variable is 'AWS_CONFIG_FILE', file location
  55.      * defaults to ~/.aws/config), then checks for the "default" profile in the
  56.      * environment-defined config file location, and failing those uses a default
  57.      * fallback set of configuration options.
  58.      *
  59.      * This provider is automatically wrapped in a memoize function that caches
  60.      * previously provided config options.
  61.      *
  62.      * @param array $config
  63.      *
  64.      * @return callable
  65.      */
  66.     public static function defaultProvider(array $config = [])
  67.     {
  68.         $configProviders = [self::env()];
  69.         if (
  70.             !isset($config['use_aws_shared_config_files'])
  71.             || $config['use_aws_shared_config_files'] != false
  72.         ) {
  73.             $configProviders[] = self::ini();
  74.         }
  75.         $configProviders[] = self::fallback();
  76.         $memo self::memoize(
  77.             call_user_func_array('self::chain'$configProviders)
  78.         );
  79.         if (isset($config['s3_us_east_1_regional_endpoint'])
  80.             && $config['s3_us_east_1_regional_endpoint'] instanceof CacheInterface
  81.         ) {
  82.             return self::cache($memo$config['s3_us_east_1_regional_endpoint'], self::$cacheKey);
  83.         }
  84.         return $memo;
  85.     }
  86.     public static function env()
  87.     {
  88.         return function () {
  89.             // Use config from environment variables, if available
  90.             $endpointsType getenv(self::ENV_ENDPOINTS_TYPE);
  91.             if (!empty($endpointsType)) {
  92.                 return Promise\Create::promiseFor(
  93.                     new Configuration($endpointsType)
  94.                 );
  95.             }
  96.             return self::reject('Could not find environment variable config'
  97.                 ' in ' self::ENV_ENDPOINTS_TYPE);
  98.         };
  99.     }
  100.     /**
  101.      * Config provider that creates config using a config file whose location
  102.      * is specified by an environment variable 'AWS_CONFIG_FILE', defaulting to
  103.      * ~/.aws/config if not specified
  104.      *
  105.      * @param string|null $profile  Profile to use. If not specified will use
  106.      *                              the "default" profile.
  107.      * @param string|null $filename If provided, uses a custom filename rather
  108.      *                              than looking in the default directory.
  109.      *
  110.      * @return callable
  111.      */
  112.     public static function ini(
  113.         $profile null,
  114.         $filename null
  115.     ) {
  116.         $filename $filename ?: (self::getDefaultConfigFilename());
  117.         $profile $profile ?: (getenv(self::ENV_PROFILE) ?: 'default');
  118.         return function () use ($profile$filename) {
  119.             if (!@is_readable($filename)) {
  120.                 return self::reject("Cannot read configuration from $filename");
  121.             }
  122.             $data = \Aws\parse_ini_file($filenametrue);
  123.             if ($data === false) {
  124.                 return self::reject("Invalid config file: $filename");
  125.             }
  126.             if (!isset($data[$profile])) {
  127.                 return self::reject("'$profile' not found in config file");
  128.             }
  129.             if (!isset($data[$profile][self::INI_ENDPOINTS_TYPE])) {
  130.                 return self::reject("Required S3 regional endpoint config values 
  131.                     not present in INI profile '{$profile}' ({$filename})");
  132.             }
  133.             return Promise\Create::promiseFor(
  134.                 new Configuration($data[$profile][self::INI_ENDPOINTS_TYPE])
  135.             );
  136.         };
  137.     }
  138.     /**
  139.      * Fallback config options when other sources are not set.
  140.      *
  141.      * @return callable
  142.      */
  143.     public static function fallback()
  144.     {
  145.         return function () {
  146.             return Promise\Create::promiseFor(
  147.                 new Configuration(self::DEFAULT_ENDPOINTS_TYPE)
  148.             );
  149.         };
  150.     }
  151.     /**
  152.      * Unwraps a configuration object in whatever valid form it is in,
  153.      * always returning a ConfigurationInterface object.
  154.      *
  155.      * @param  mixed $config
  156.      * @return ConfigurationInterface
  157.      * @throws \InvalidArgumentException
  158.      */
  159.     public static function unwrap($config)
  160.     {
  161.         if (is_callable($config)) {
  162.             $config $config();
  163.         }
  164.         if ($config instanceof Promise\PromiseInterface) {
  165.             $config $config->wait();
  166.         }
  167.         if ($config instanceof ConfigurationInterface) {
  168.             return $config;
  169.         }
  170.         if (is_string($config)) {
  171.             return new Configuration($config);
  172.         }
  173.         if (is_array($config) && isset($config['endpoints_type'])) {
  174.             return new Configuration($config['endpoints_type']);
  175.         }
  176.         throw new \InvalidArgumentException('Not a valid S3 regional endpoint '
  177.             'configuration argument.');
  178.     }
  179. }