Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\DefaultsMode;
3
 
4
use Aws\AbstractConfigurationProvider;
5
use Aws\CacheInterface;
6
use Aws\ConfigurationProviderInterface;
7
use Aws\DefaultsMode\Exception\ConfigurationException;
8
use GuzzleHttp\Promise;
9
use GuzzleHttp\Promise\PromiseInterface;
10
 
11
/**
12
 * A configuration provider is a function that returns a promise that is
13
 * fulfilled with a {@see \Aws\DefaultsMode\ConfigurationInterface}
14
 * or rejected with an {@see \Aws\DefaultsMode\Exception\ConfigurationException}.
15
 *
16
 * <code>
17
 * use Aws\Sts\RegionalEndpoints\ConfigurationProvider;
18
 * $provider = ConfigurationProvider::defaultProvider();
19
 * // Returns a ConfigurationInterface or throws.
20
 * $config = $provider()->wait();
21
 * </code>
22
 *
23
 * Configuration providers can be composed to create configuration using
24
 * conditional logic that can create different configurations in different
25
 * environments. You can compose multiple providers into a single provider using
26
 * {@see \Aws\DefaultsMode\ConfigurationProvider::chain}. This function
27
 * accepts providers as variadic arguments and returns a new function that will
28
 * invoke each provider until a successful configuration is returned.
29
 *
30
 * <code>
31
 * // First try an INI file at this location.
32
 * $a = ConfigurationProvider::ini(null, '/path/to/file.ini');
33
 * // Then try an INI file at this location.
34
 * $b = ConfigurationProvider::ini(null, '/path/to/other-file.ini');
35
 * // Then try loading from environment variables.
36
 * $c = ConfigurationProvider::env();
37
 * // Combine the three providers together.
38
 * $composed = ConfigurationProvider::chain($a, $b, $c);
39
 * // Returns a promise that is fulfilled with a configuration or throws.
40
 * $promise = $composed();
41
 * // Wait on the configuration to resolve.
42
 * $config = $promise->wait();
43
 * </code>
44
 */
45
class ConfigurationProvider extends AbstractConfigurationProvider
46
    implements ConfigurationProviderInterface
47
{
48
    const DEFAULT_MODE = 'legacy';
49
    const ENV_MODE = 'AWS_DEFAULTS_MODE';
50
    const ENV_PROFILE = 'AWS_PROFILE';
51
    const INI_MODE = 'defaults_mode';
52
 
53
    public static $cacheKey = 'aws_defaults_mode';
54
 
55
    protected static $interfaceClass = ConfigurationInterface::class;
56
    protected static $exceptionClass = ConfigurationException::class;
57
 
58
    /**
59
     * Create a default config provider that first checks for environment
60
     * variables, then checks for a specified profile in the environment-defined
61
     * config file location (env variable is 'AWS_CONFIG_FILE', file location
62
     * defaults to ~/.aws/config), then checks for the "default" profile in the
63
     * environment-defined config file location, and failing those uses a default
64
     * fallback set of configuration options.
65
     *
66
     * This provider is automatically wrapped in a memoize function that caches
67
     * previously provided config options.
68
     *
69
     * @param array $config
70
     *
71
     * @return callable
72
     */
73
    public static function defaultProvider(array $config = [])
74
    {
75
        $configProviders = [self::env()];
76
        if (
77
            !isset($config['use_aws_shared_config_files'])
78
            || $config['use_aws_shared_config_files'] != false
79
        ) {
80
            $configProviders[] = self::ini();
81
        }
82
        $configProviders[] = self::fallback();
83
 
84
        $memo = self::memoize(
85
            call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders)
86
        );
87
 
88
        if (isset($config['defaultsMode'])
89
            && $config['defaultsMode'] instanceof CacheInterface
90
        ) {
91
            return self::cache($memo, $config['defaultsMode'], self::$cacheKey);
92
        }
93
 
94
        return $memo;
95
    }
96
 
97
    /**
98
     * Provider that creates config from environment variables.
99
     *
100
     * @return callable
101
     */
102
    public static function env()
103
    {
104
        return function () {
105
            // Use config from environment variables, if available
106
            $mode = getenv(self::ENV_MODE);
107
            if (!empty($mode)) {
108
                return Promise\Create::promiseFor(
109
                    new Configuration($mode)
110
                );
111
            }
112
 
113
            return self::reject('Could not find environment variable config'
114
                . ' in ' . self::ENV_MODE);
115
        };
116
    }
117
 
118
    /**
119
     * Fallback config options when other sources are not set.
120
     *
121
     * @return callable
122
     */
123
    public static function fallback()
124
    {
125
        return function () {
126
            return Promise\Create::promiseFor(
127
                new Configuration( self::DEFAULT_MODE)
128
            );
129
        };
130
    }
131
 
132
    /**
133
     * Config provider that creates config using a config file whose location
134
     * is specified by an environment variable 'AWS_CONFIG_FILE', defaulting to
135
     * ~/.aws/config if not specified
136
     *
137
     * @param string|null $profile  Profile to use. If not specified will use
138
     *                              the "default" profile.
139
     * @param string|null $filename If provided, uses a custom filename rather
140
     *                              than looking in the default directory.
141
     *
142
     * @return callable
143
     */
144
    public static function ini(
145
        $profile = null,
146
        $filename = null
147
    ) {
148
        $filename = $filename ?: (self::getDefaultConfigFilename());
149
        $profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default');
150
 
151
        return function () use ($profile, $filename) {
152
            if (!is_readable($filename)) {
153
                return self::reject("Cannot read configuration from $filename");
154
            }
155
            $data = \Aws\parse_ini_file($filename, true);
156
            if ($data === false) {
157
                return self::reject("Invalid config file: $filename");
158
            }
159
            if (!isset($data[$profile])) {
160
                return self::reject("'$profile' not found in config file");
161
            }
162
            if (!isset($data[$profile][self::INI_MODE])) {
163
                return self::reject("Required defaults mode config values
164
                    not present in INI profile '{$profile}' ({$filename})");
165
            }
166
            return Promise\Create::promiseFor(
167
                new Configuration(
168
                    $data[$profile][self::INI_MODE]
169
                )
170
            );
171
        };
172
    }
173
 
174
    /**
175
     * Unwraps a configuration object in whatever valid form it is in,
176
     * always returning a ConfigurationInterface object.
177
     *
178
     * @param  mixed $config
179
     * @return ConfigurationInterface
180
     * @throws \InvalidArgumentException
181
     */
182
    public static function unwrap($config)
183
    {
184
        if (is_callable($config)) {
185
            $config = $config();
186
        }
187
        if ($config instanceof PromiseInterface) {
188
            $config = $config->wait();
189
        }
190
        if ($config instanceof ConfigurationInterface) {
191
            return $config;
192
        }
193
 
194
        if (is_string($config)) {
195
            return new Configuration($config);
196
        }
197
 
198
        throw new \InvalidArgumentException('Not a valid defaults mode configuration'
199
            . ' argument.');
200
    }
201
}