Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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