ConfigurationProvider.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Aws\S3\UseArnRegion;
  3. use Aws\AbstractConfigurationProvider;
  4. use Aws\CacheInterface;
  5. use Aws\ConfigurationProviderInterface;
  6. use Aws\S3\UseArnRegion\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\UseArnRegion\ConfigurationInterface}
  11. * or rejected with an {@see \Aws\S3\UseArnRegion\Exception\ConfigurationException}.
  12. *
  13. * <code>
  14. * use Aws\S3\UseArnRegion\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\UseArnRegion\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_ARN_REGION = 'AWS_S3_USE_ARN_REGION';
  46. const INI_USE_ARN_REGION = 's3_use_arn_region';
  47. const DEFAULT_USE_ARN_REGION = true;
  48. public static $cacheKey = 'aws_s3_use_arn_region_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([ConfigurationProvider::class, 'chain'], $configProviders)
  78. );
  79. if (isset($config['use_arn_region'])
  80. && $config['use_arn_region'] instanceof CacheInterface
  81. ) {
  82. return self::cache($memo, $config['use_arn_region'], 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()
  92. {
  93. return function () {
  94. // Use config from environment variables, if available
  95. $useArnRegion = getenv(self::ENV_USE_ARN_REGION);
  96. if (!empty($useArnRegion)) {
  97. return Promise\Create::promiseFor(
  98. new Configuration($useArnRegion)
  99. );
  100. }
  101. return self::reject('Could not find environment variable config'
  102. . ' in ' . self::ENV_USE_ARN_REGION);
  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($profile = null, $filename = null)
  118. {
  119. $filename = $filename ?: (self::getDefaultConfigFilename());
  120. $profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default');
  121. return function () use ($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($filename, true, INI_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_ARN_REGION])) {
  134. return self::reject("Required S3 Use Arn Region 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_ARN_REGION] === "") {
  139. $data[$profile][self::INI_USE_ARN_REGION] = false;
  140. }
  141. return Promise\Create::promiseFor(
  142. new Configuration($data[$profile][self::INI_USE_ARN_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()
  152. {
  153. return function () {
  154. return Promise\Create::promiseFor(
  155. new Configuration(self::DEFAULT_USE_ARN_REGION)
  156. );
  157. };
  158. }
  159. }