| 1 |
<?php |
| 2 |
|
| 3 |
namespace Aws\Configuration; |
| 4 |
|
| 5 |
class ConfigurationResolver |
| 6 |
{ |
| 7 |
const ENV_PROFILE = 'AWS_PROFILE'; |
| 8 |
const ENV_CONFIG_FILE = 'AWS_CONFIG_FILE'; |
| 9 |
|
| 10 |
public static $envPrefix = 'AWS_'; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public static function resolve( |
| 29 |
$key, |
| 30 |
$defaultValue, |
| 31 |
$expectedType, |
| 32 |
$config = [] |
| 33 |
) |
| 34 |
{ |
| 35 |
$iniOptions = isset($config['ini_resolver_options']) |
| 36 |
? $config['ini_resolver_options'] |
| 37 |
: []; |
| 38 |
|
| 39 |
$envValue = self::env($key, $expectedType); |
| 40 |
if (!is_null($envValue)) { |
| 41 |
return $envValue; |
| 42 |
} |
| 43 |
|
| 44 |
if (!isset($config['use_aws_shared_config_files']) |
| 45 |
|| $config['use_aws_shared_config_files'] != false |
| 46 |
) { |
| 47 |
$iniValue = self::ini( |
| 48 |
$key, |
| 49 |
$expectedType, |
| 50 |
null, |
| 51 |
null, |
| 52 |
$iniOptions |
| 53 |
); |
| 54 |
if(!is_null($iniValue)) { |
| 55 |
return $iniValue; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return $defaultValue; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public static function env($key, $expectedType = 'string') |
| 72 |
{ |
| 73 |
|
| 74 |
$envValue = getenv(self::$envPrefix . strtoupper($key)); |
| 75 |
if (!empty($envValue)) { |
| 76 |
if ($expectedType) { |
| 77 |
$envValue = self::convertType($envValue, $expectedType); |
| 78 |
} |
| 79 |
return $envValue; |
| 80 |
} |
| 81 |
|
| 82 |
return null; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public static function ini( |
| 102 |
$key, |
| 103 |
$expectedType, |
| 104 |
$profile = null, |
| 105 |
$filename = null, |
| 106 |
$options = [] |
| 107 |
){ |
| 108 |
$filename = $filename ?: (self::getDefaultConfigFilename()); |
| 109 |
$profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default'); |
| 110 |
|
| 111 |
if (!@is_readable($filename)) { |
| 112 |
return null; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
$data = @\Aws\parse_ini_file($filename, true, INI_SCANNER_NORMAL); |
| 117 |
|
| 118 |
if (isset($options['section']) |
| 119 |
&& isset($options['subsection']) |
| 120 |
&& isset($options['key'])) |
| 121 |
{ |
| 122 |
return self::retrieveValueFromIniSubsection( |
| 123 |
$data, |
| 124 |
$profile, |
| 125 |
$filename, |
| 126 |
$expectedType, |
| 127 |
$options |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
if ($data === false |
| 132 |
|| !isset($data[$profile]) |
| 133 |
|| !isset($data[$profile][$key]) |
| 134 |
) { |
| 135 |
return null; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
if ($data[$profile][$key] === "") { |
| 140 |
if ($expectedType === 'bool') { |
| 141 |
$data[$profile][$key] = false; |
| 142 |
} elseif ($expectedType === 'int') { |
| 143 |
$data[$profile][$key] = 0; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
return self::convertType($data[$profile][$key], $expectedType); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
private static function getHomeDir() |
| 156 |
{ |
| 157 |
|
| 158 |
if ($homeDir = getenv('HOME')) { |
| 159 |
return $homeDir; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
$homeDrive = getenv('HOMEDRIVE'); |
| 164 |
$homePath = getenv('HOMEPATH'); |
| 165 |
|
| 166 |
return ($homeDrive && $homePath) ? $homeDrive . $homePath : null; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
private static function getDefaultConfigFilename() |
| 176 |
{ |
| 177 |
if ($filename = getenv(self::ENV_CONFIG_FILE)) { |
| 178 |
return $filename; |
| 179 |
} |
| 180 |
return self::getHomeDir() . '/.aws/config'; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
private static function convertType($value, $type) |
| 194 |
{ |
| 195 |
if ($type === 'bool' |
| 196 |
&& !is_null($convertedValue = \Aws\boolean_value($value)) |
| 197 |
) { |
| 198 |
return $convertedValue; |
| 199 |
} |
| 200 |
|
| 201 |
if ($type === 'int' |
| 202 |
&& filter_var($value, FILTER_VALIDATE_INT) |
| 203 |
) { |
| 204 |
$value = intVal($value); |
| 205 |
} |
| 206 |
|
| 207 |
return $value; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
private static function retrieveValueFromIniSubsection( |
| 222 |
$data, |
| 223 |
$profile, |
| 224 |
$filename, |
| 225 |
$expectedType, |
| 226 |
$options |
| 227 |
){ |
| 228 |
$section = $options['section']; |
| 229 |
if ($data === false |
| 230 |
|| !isset($data[$profile][$section]) |
| 231 |
|| !isset($data["{$section} {$data[$profile][$section]}"]) |
| 232 |
) { |
| 233 |
return null; |
| 234 |
} |
| 235 |
|
| 236 |
$services_section = \Aws\parse_ini_section_with_subsections( |
| 237 |
$filename, |
| 238 |
"services {$data[$profile]['services']}" |
| 239 |
); |
| 240 |
|
| 241 |
if (empty($options['subsection']) || empty($options['key'])) { |
| 242 |
return null; |
| 243 |
} |
| 244 |
|
| 245 |
if (!isset($services_section[$options['subsection']][$options['key']])) { |
| 246 |
return null; |
| 247 |
} |
| 248 |
|
| 249 |
return self::convertType( |
| 250 |
$services_section[$options['subsection']][$options['key']], |
| 251 |
$expectedType |
| 252 |
); |
| 253 |
} |
| 254 |
} |