Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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