PluginProbe ʕ •ᴥ•ʔ
Transferito: WP Migration / trunk
Transferito: WP Migration vtrunk
trunk 11.4.0 12.0.0 13.1.0 14.0.0 14.0.11 14.0.7 14.1.0 14.1.1 14.1.2 14.1.3 14.1.4
transferito / vendor / aws / aws-sdk-php / src / Configuration / ConfigurationResolver.php
transferito / vendor / aws / aws-sdk-php / src / Configuration Last commit date
ConfigurationResolver.php 10 months ago
ConfigurationResolver.php
251 lines
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 * Generic configuration resolver that first checks for environment
14 * variables, then checks for a specified profile in the environment-defined
15 * config file location (env variable is 'AWS_CONFIG_FILE', file location
16 * defaults to ~/.aws/config), then checks for the "default" profile in the
17 * environment-defined config file location, and failing those uses a default
18 * fallback value.
19 *
20 * @param string $key Configuration key to be used when attempting
21 * to retrieve value from the environment or ini file.
22 * @param mixed $defaultValue
23 * @param string $expectedType The expected type of the retrieved value.
24 * @param array $config additional configuration options.
25 *
26 * @return mixed
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 * Resolves config values from environment variables.
64 *
65 * @param string $key Configuration key to be used when attempting
66 * to retrieve value from the environment.
67 * @param string $expectedType The expected type of the retrieved value.
68 *
69 * @return null | mixed
70 */
71 public static function env($key, $expectedType)
72 {
73 // Use config from environment variables, if available
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 * Gets config values from a config file whose location
87 * is specified by an environment variable 'AWS_CONFIG_FILE', defaulting to
88 * ~/.aws/config if not specified
89 *
90 *
91 * @param string $key Configuration key to be used when attempting
92 * to retrieve value from ini file.
93 * @param string $expectedType The expected type of the retrieved value.
94 * @param string|null $profile Profile to use. If not specified will use
95 * the "default" profile.
96 * @param string|null $filename If provided, uses a custom filename rather
97 * than looking in the default directory.
98 *
99 * @return null | mixed
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 // Use INI_SCANNER_NORMAL instead of INI_SCANNER_TYPED for PHP 5.5 compatibility
115 //TODO change after deprecation
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 // INI_SCANNER_NORMAL parses false-y values as an empty string
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 * Gets the environment's HOME directory if available.
152 *
153 * @return null | string
154 */
155 private static function getHomeDir()
156 {
157 // On Linux/Unix-like systems, use the HOME environment variable
158 if ($homeDir = getenv('HOME')) {
159 return $homeDir;
160 }
161
162 // Get the HOMEDRIVE and HOMEPATH values for Windows hosts
163 $homeDrive = getenv('HOMEDRIVE');
164 $homePath = getenv('HOMEPATH');
165
166 return ($homeDrive && $homePath) ? $homeDrive . $homePath : null;
167 }
168
169 /**
170 * Gets default config file location from environment, falling back to aws
171 * default location
172 *
173 * @return string
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 * Normalizes string values pulled out of ini files and
185 * environment variables.
186 *
187 * @param string $value The value retrieved from the environment or
188 * ini file.
189 * @param $type $string The type that the value needs to be converted to.
190 *
191 * @return mixed
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 return $value;
207 }
208
209 /**
210 * Normalizes string values pulled out of ini files and
211 * environment variables.
212 *
213 * @param array $data The data retrieved the ini file
214 * @param string $profile The specified ini profile
215 * @param string $filename The full path to the ini file
216 * @param array $options Additional arguments passed to the configuration resolver
217 *
218 * @return mixed
219 */
220 private static function retrieveValueFromIniSubsection(
221 $data,
222 $profile,
223 $filename,
224 $expectedType,
225 $options
226 ){
227 $section = $options['section'];
228 if ($data === false
229 || !isset($data[$profile][$section])
230 || !isset($data["{$section} {$data[$profile][$section]}"])
231 ) {
232 return null;
233 }
234
235 $services_section = \Aws\parse_ini_section_with_subsections(
236 $filename,
237 "services {$data[$profile]['services']}"
238 );
239
240 if (!isset($services_section[$options['subsection']][$options['key']])
241 ) {
242 return null;
243 }
244
245 return self::convertType(
246 $services_section[$options['subsection']][$options['key']],
247 $expectedType
248 );
249 }
250 }
251