ConfigurationResolver.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace Aws\Configuration;
  3. class ConfigurationResolver
  4. {
  5. const ENV_PROFILE = 'AWS_PROFILE';
  6. const ENV_CONFIG_FILE = 'AWS_CONFIG_FILE';
  7. public static $envPrefix = 'AWS_';
  8. /**
  9. * Generic configuration resolver that first checks for environment
  10. * variables, then checks for a specified profile in the environment-defined
  11. * config file location (env variable is 'AWS_CONFIG_FILE', file location
  12. * defaults to ~/.aws/config), then checks for the "default" profile in the
  13. * environment-defined config file location, and failing those uses a default
  14. * fallback value.
  15. *
  16. * @param string $key Configuration key to be used when attempting
  17. * to retrieve value from the environment or ini file.
  18. * @param mixed $defaultValue
  19. * @param string $expectedType The expected type of the retrieved value.
  20. * @param array $config additional configuration options.
  21. *
  22. * @return mixed
  23. */
  24. public static function resolve(
  25. $key,
  26. $defaultValue,
  27. $expectedType,
  28. $config = []
  29. )
  30. {
  31. $iniOptions = isset($config['ini_resolver_options'])
  32. ? $config['ini_resolver_options']
  33. : [];
  34. $envValue = self::env($key, $expectedType);
  35. if (!is_null($envValue)) {
  36. return $envValue;
  37. }
  38. if (!isset($config['use_aws_shared_config_files'])
  39. || $config['use_aws_shared_config_files'] != false
  40. ) {
  41. $iniValue = self::ini(
  42. $key,
  43. $expectedType,
  44. null,
  45. null,
  46. $iniOptions
  47. );
  48. if(!is_null($iniValue)) {
  49. return $iniValue;
  50. }
  51. }
  52. return $defaultValue;
  53. }
  54. /**
  55. * Resolves config values from environment variables.
  56. *
  57. * @param string $key Configuration key to be used when attempting
  58. * to retrieve value from the environment.
  59. * @param string $expectedType The expected type of the retrieved value.
  60. *
  61. * @return null | mixed
  62. */
  63. public static function env($key, $expectedType)
  64. {
  65. // Use config from environment variables, if available
  66. $envValue = getenv(self::$envPrefix . strtoupper($key));
  67. if (!empty($envValue)) {
  68. if ($expectedType) {
  69. $envValue = self::convertType($envValue, $expectedType);
  70. }
  71. return $envValue;
  72. }
  73. return null;
  74. }
  75. /**
  76. * Gets config values from a config file whose location
  77. * is specified by an environment variable 'AWS_CONFIG_FILE', defaulting to
  78. * ~/.aws/config if not specified
  79. *
  80. *
  81. * @param string $key Configuration key to be used when attempting
  82. * to retrieve value from ini file.
  83. * @param string $expectedType The expected type of the retrieved value.
  84. * @param string|null $profile Profile to use. If not specified will use
  85. * the "default" profile.
  86. * @param string|null $filename If provided, uses a custom filename rather
  87. * than looking in the default directory.
  88. *
  89. * @return null | mixed
  90. */
  91. public static function ini(
  92. $key,
  93. $expectedType,
  94. $profile = null,
  95. $filename = null,
  96. $options = []
  97. ){
  98. $filename = $filename ?: (self::getDefaultConfigFilename());
  99. $profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default');
  100. if (!@is_readable($filename)) {
  101. return null;
  102. }
  103. // Use INI_SCANNER_NORMAL instead of INI_SCANNER_TYPED for PHP 5.5 compatibility
  104. //TODO change after deprecation
  105. $data = @\Aws\parse_ini_file($filename, true, INI_SCANNER_NORMAL);
  106. if (isset($options['section'])
  107. && isset($options['subsection'])
  108. && isset($options['key']))
  109. {
  110. return self::retrieveValueFromIniSubsection(
  111. $data,
  112. $profile,
  113. $filename,
  114. $expectedType,
  115. $options
  116. );
  117. }
  118. if ($data === false
  119. || !isset($data[$profile])
  120. || !isset($data[$profile][$key])
  121. ) {
  122. return null;
  123. }
  124. // INI_SCANNER_NORMAL parses false-y values as an empty string
  125. if ($data[$profile][$key] === "") {
  126. if ($expectedType === 'bool') {
  127. $data[$profile][$key] = false;
  128. } elseif ($expectedType === 'int') {
  129. $data[$profile][$key] = 0;
  130. }
  131. }
  132. return self::convertType($data[$profile][$key], $expectedType);
  133. }
  134. /**
  135. * Gets the environment's HOME directory if available.
  136. *
  137. * @return null | string
  138. */
  139. private static function getHomeDir()
  140. {
  141. // On Linux/Unix-like systems, use the HOME environment variable
  142. if ($homeDir = getenv('HOME')) {
  143. return $homeDir;
  144. }
  145. // Get the HOMEDRIVE and HOMEPATH values for Windows hosts
  146. $homeDrive = getenv('HOMEDRIVE');
  147. $homePath = getenv('HOMEPATH');
  148. return ($homeDrive && $homePath) ? $homeDrive . $homePath : null;
  149. }
  150. /**
  151. * Gets default config file location from environment, falling back to aws
  152. * default location
  153. *
  154. * @return string
  155. */
  156. private static function getDefaultConfigFilename()
  157. {
  158. if ($filename = getenv(self::ENV_CONFIG_FILE)) {
  159. return $filename;
  160. }
  161. return self::getHomeDir() . '/.aws/config';
  162. }
  163. /**
  164. * Normalizes string values pulled out of ini files and
  165. * environment variables.
  166. *
  167. * @param string $value The value retrieved from the environment or
  168. * ini file.
  169. * @param $type $string The type that the value needs to be converted to.
  170. *
  171. * @return mixed
  172. */
  173. private static function convertType($value, $type)
  174. {
  175. if ($type === 'bool'
  176. && !is_null($convertedValue = \Aws\boolean_value($value))
  177. ) {
  178. return $convertedValue;
  179. }
  180. if ($type === 'int'
  181. && filter_var($value, FILTER_VALIDATE_INT)
  182. ) {
  183. $value = intVal($value);
  184. }
  185. return $value;
  186. }
  187. /**
  188. * Normalizes string values pulled out of ini files and
  189. * environment variables.
  190. *
  191. * @param array $data The data retrieved the ini file
  192. * @param string $profile The specified ini profile
  193. * @param string $filename The full path to the ini file
  194. * @param array $options Additional arguments passed to the configuration resolver
  195. *
  196. * @return mixed
  197. */
  198. private static function retrieveValueFromIniSubsection(
  199. $data,
  200. $profile,
  201. $filename,
  202. $expectedType,
  203. $options
  204. ){
  205. $section = $options['section'];
  206. if ($data === false
  207. || !isset($data[$profile][$section])
  208. || !isset($data["{$section} {$data[$profile][$section]}"])
  209. ) {
  210. return null;
  211. }
  212. $services_section = \Aws\parse_ini_section_with_subsections(
  213. $filename,
  214. "services {$data[$profile]['services']}"
  215. );
  216. if (!isset($services_section[$options['subsection']][$options['key']])
  217. ) {
  218. return null;
  219. }
  220. return self::convertType(
  221. $services_section[$options['subsection']][$options['key']],
  222. $expectedType
  223. );
  224. }
  225. }