vendor/aws/aws-sdk-php/src/Endpoint/UseDualstackEndpoint/ConfigurationProvider.php line 134

Open in your IDE?
  1. <?php
  2. namespace Aws\Endpoint\UseDualstackEndpoint;
  3. use Aws\AbstractConfigurationProvider;
  4. use Aws\CacheInterface;
  5. use Aws\ConfigurationProviderInterface;
  6. use Aws\Endpoint\UseDualstackEndpoint\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\Endpoint\UseDualstackEndpoint\onfigurationInterface}
  11.  * or rejected with an {@see \Aws\Endpoint\UseDualstackEndpoint\ConfigurationException}.
  12.  *
  13.  * <code>
  14.  * use Aws\Endpoint\UseDualstackEndpoint\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\Endpoint\UseDualstackEndpoint\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_USE_DUAL_STACK_ENDPOINT 'AWS_USE_DUALSTACK_ENDPOINT';
  46.     const INI_USE_DUAL_STACK_ENDPOINT 'use_dualstack_endpoint';
  47.     public static $cacheKey 'aws_cached_use_dualstack_endpoint_config';
  48.     protected static $interfaceClass ConfigurationInterface::class;
  49.     protected static $exceptionClass ConfigurationException::class;
  50.     /**
  51.      * Create a default config provider that first checks for environment
  52.      * variables, then checks for a specified profile in the environment-defined
  53.      * config file location (env variable is 'AWS_CONFIG_FILE', file location
  54.      * defaults to ~/.aws/config), then checks for the "default" profile in the
  55.      * environment-defined config file location, and failing those uses a default
  56.      * fallback set of configuration options.
  57.      *
  58.      * This provider is automatically wrapped in a memoize function that caches
  59.      * previously provided config options.
  60.      *
  61.      * @param array $config
  62.      *
  63.      * @return callable
  64.      */
  65.     public static function defaultProvider(array $config = [])
  66.     {
  67.         $region $config['region'];
  68.         $configProviders = [self::env($region)];
  69.         if (
  70.             !isset($config['use_aws_shared_config_files'])
  71.             || $config['use_aws_shared_config_files'] != false
  72.         ) {
  73.             $configProviders[] = self::ini($region);
  74.         }
  75.         $configProviders[] = self::fallback($region);
  76.         $memo self::memoize(
  77.             call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders)
  78.         );
  79.         if (isset($config['use_dual_stack_endpoint'])
  80.             && $config['use_dual_stack_endpoint'] instanceof CacheInterface
  81.         ) {
  82.             return self::cache($memo$config['use_dual_stack_endpoint'], self::$cacheKey);
  83.         }
  84.         return $memo;
  85.     }
  86.     /**
  87.      * Provider that creates config from environment variables.
  88.      *
  89.      * @return callable
  90.      */
  91.     public static function env($region)
  92.     {
  93.         return function () use ($region) {
  94.             // Use config from environment variables, if available
  95.             $useDualstackEndpoint getenv(self::ENV_USE_DUAL_STACK_ENDPOINT);
  96.             if (!empty($useDualstackEndpoint)) {
  97.                 return Promise\Create::promiseFor(
  98.                     new Configuration($useDualstackEndpoint$region)
  99.                 );
  100.             }
  101.             return self::reject('Could not find environment variable config'
  102.                 ' in ' self::ENV_USE_DUAL_STACK_ENDPOINT);
  103.         };
  104.     }
  105.     /**
  106.      * Config provider that creates config using a config file whose location
  107.      * is specified by an environment variable 'AWS_CONFIG_FILE', defaulting to
  108.      * ~/.aws/config if not specified
  109.      *
  110.      * @param string|null $profile  Profile to use. If not specified will use
  111.      *                              the "default" profile.
  112.      * @param string|null $filename If provided, uses a custom filename rather
  113.      *                              than looking in the default directory.
  114.      *
  115.      * @return callable
  116.      */
  117.     public static function ini($region$profile null$filename null)
  118.     {
  119.         $filename $filename ?: (self::getDefaultConfigFilename());
  120.         $profile $profile ?: (getenv(self::ENV_PROFILE) ?: 'default');
  121.         return function () use ($region$profile$filename) {
  122.             if (!@is_readable($filename)) {
  123.                 return self::reject("Cannot read configuration from $filename");
  124.             }
  125.             // Use INI_SCANNER_NORMAL instead of INI_SCANNER_TYPED for PHP 5.5 compatibility
  126.             $data = \Aws\parse_ini_file($filenametrueINI_SCANNER_NORMAL);
  127.             if ($data === false) {
  128.                 return self::reject("Invalid config file: $filename");
  129.             }
  130.             if (!isset($data[$profile])) {
  131.                 return self::reject("'$profile' not found in config file");
  132.             }
  133.             if (!isset($data[$profile][self::INI_USE_DUAL_STACK_ENDPOINT])) {
  134.                 return self::reject("Required use dualstack endpoint config values
  135.                     not present in INI profile '{$profile}' ({$filename})");
  136.             }
  137.             // INI_SCANNER_NORMAL parses false-y values as an empty string
  138.             if ($data[$profile][self::INI_USE_DUAL_STACK_ENDPOINT] === "") {
  139.                 $data[$profile][self::INI_USE_DUAL_STACK_ENDPOINT] = false;
  140.             }
  141.             return Promise\Create::promiseFor(
  142.                 new Configuration($data[$profile][self::INI_USE_DUAL_STACK_ENDPOINT], $region)
  143.             );
  144.         };
  145.     }
  146.     /**
  147.      * Fallback config options when other sources are not set.
  148.      *
  149.      * @return callable
  150.      */
  151.     public static function fallback($region)
  152.     {
  153.         return function () use ($region) {
  154.             return Promise\Create::promiseFor(new Configuration(false$region));
  155.         };
  156.     }
  157. }