123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- namespace Aws\Configuration;
- class ConfigurationResolver
- {
- const ENV_PROFILE = 'AWS_PROFILE';
- const ENV_CONFIG_FILE = 'AWS_CONFIG_FILE';
- public static $envPrefix = 'AWS_';
-
- public static function resolve(
- $key,
- $defaultValue,
- $expectedType,
- $config = []
- )
- {
- $iniOptions = isset($config['ini_resolver_options'])
- ? $config['ini_resolver_options']
- : [];
- $envValue = self::env($key, $expectedType);
- if (!is_null($envValue)) {
- return $envValue;
- }
- if (!isset($config['use_aws_shared_config_files'])
- || $config['use_aws_shared_config_files'] != false
- ) {
- $iniValue = self::ini(
- $key,
- $expectedType,
- null,
- null,
- $iniOptions
- );
- if(!is_null($iniValue)) {
- return $iniValue;
- }
- }
- return $defaultValue;
- }
-
- public static function env($key, $expectedType)
- {
-
- $envValue = getenv(self::$envPrefix . strtoupper($key));
- if (!empty($envValue)) {
- if ($expectedType) {
- $envValue = self::convertType($envValue, $expectedType);
- }
- return $envValue;
- }
- return null;
- }
-
- public static function ini(
- $key,
- $expectedType,
- $profile = null,
- $filename = null,
- $options = []
- ){
- $filename = $filename ?: (self::getDefaultConfigFilename());
- $profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default');
- if (!@is_readable($filename)) {
- return null;
- }
-
-
- $data = @\Aws\parse_ini_file($filename, true, INI_SCANNER_NORMAL);
- if (isset($options['section'])
- && isset($options['subsection'])
- && isset($options['key']))
- {
- return self::retrieveValueFromIniSubsection(
- $data,
- $profile,
- $filename,
- $expectedType,
- $options
- );
- }
- if ($data === false
- || !isset($data[$profile])
- || !isset($data[$profile][$key])
- ) {
- return null;
- }
-
- if ($data[$profile][$key] === "") {
- if ($expectedType === 'bool') {
- $data[$profile][$key] = false;
- } elseif ($expectedType === 'int') {
- $data[$profile][$key] = 0;
- }
- }
- return self::convertType($data[$profile][$key], $expectedType);
- }
-
- private static function getHomeDir()
- {
-
- if ($homeDir = getenv('HOME')) {
- return $homeDir;
- }
-
- $homeDrive = getenv('HOMEDRIVE');
- $homePath = getenv('HOMEPATH');
- return ($homeDrive && $homePath) ? $homeDrive . $homePath : null;
- }
-
- private static function getDefaultConfigFilename()
- {
- if ($filename = getenv(self::ENV_CONFIG_FILE)) {
- return $filename;
- }
- return self::getHomeDir() . '/.aws/config';
- }
-
- private static function convertType($value, $type)
- {
- if ($type === 'bool'
- && !is_null($convertedValue = \Aws\boolean_value($value))
- ) {
- return $convertedValue;
- }
- if ($type === 'int'
- && filter_var($value, FILTER_VALIDATE_INT)
- ) {
- $value = intVal($value);
- }
- return $value;
- }
-
- private static function retrieveValueFromIniSubsection(
- $data,
- $profile,
- $filename,
- $expectedType,
- $options
- ){
- $section = $options['section'];
- if ($data === false
- || !isset($data[$profile][$section])
- || !isset($data["{$section} {$data[$profile][$section]}"])
- ) {
- return null;
- }
- $services_section = \Aws\parse_ini_section_with_subsections(
- $filename,
- "services {$data[$profile]['services']}"
- );
- if (!isset($services_section[$options['subsection']][$options['key']])
- ) {
- return null;
- }
- return self::convertType(
- $services_section[$options['subsection']][$options['key']],
- $expectedType
- );
- }
- }
|