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