123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- namespace Aws\ClientSideMonitoring;
- use Aws\AbstractConfigurationProvider;
- use Aws\CacheInterface;
- use Aws\ClientSideMonitoring\Exception\ConfigurationException;
- use Aws\ConfigurationProviderInterface;
- use GuzzleHttp\Promise;
- use GuzzleHttp\Promise\PromiseInterface;
- class ConfigurationProvider extends AbstractConfigurationProvider
- implements ConfigurationProviderInterface
- {
- const DEFAULT_CLIENT_ID = '';
- const DEFAULT_ENABLED = false;
- const DEFAULT_HOST = '127.0.0.1';
- const DEFAULT_PORT = 31000;
- const ENV_CLIENT_ID = 'AWS_CSM_CLIENT_ID';
- const ENV_ENABLED = 'AWS_CSM_ENABLED';
- const ENV_HOST = 'AWS_CSM_HOST';
- const ENV_PORT = 'AWS_CSM_PORT';
- const ENV_PROFILE = 'AWS_PROFILE';
- public static $cacheKey = 'aws_cached_csm_config';
- protected static $interfaceClass = ConfigurationInterface::class;
- protected static $exceptionClass = ConfigurationException::class;
-
- public static function defaultProvider(array $config = [])
- {
- $configProviders = [self::env()];
- if (
- !isset($config['use_aws_shared_config_files'])
- || $config['use_aws_shared_config_files'] != false
- ) {
- $configProviders[] = self::ini();
- }
- $configProviders[] = self::fallback();
- $memo = self::memoize(
- call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders)
- );
- if (isset($config['csm']) && $config['csm'] instanceof CacheInterface) {
- return self::cache($memo, $config['csm'], self::$cacheKey);
- }
- return $memo;
- }
-
- public static function env()
- {
- return function () {
-
- $enabled = getenv(self::ENV_ENABLED);
- if ($enabled !== false) {
- return Promise\Create::promiseFor(
- new Configuration(
- $enabled,
- getenv(self::ENV_HOST) ?: self::DEFAULT_HOST,
- getenv(self::ENV_PORT) ?: self::DEFAULT_PORT,
- getenv(self:: ENV_CLIENT_ID) ?: self::DEFAULT_CLIENT_ID
- )
- );
- }
- return self::reject('Could not find environment variable CSM config'
- . ' in ' . self::ENV_ENABLED. '/' . self::ENV_HOST . '/'
- . self::ENV_PORT . '/' . self::ENV_CLIENT_ID);
- };
- }
-
- public static function fallback()
- {
- return function() {
- return Promise\Create::promiseFor(
- new Configuration(
- self::DEFAULT_ENABLED,
- self::DEFAULT_HOST,
- self::DEFAULT_PORT,
- self::DEFAULT_CLIENT_ID
- )
- );
- };
- }
-
- public static function ini($profile = null, $filename = null)
- {
- $filename = $filename ?: (self::getDefaultConfigFilename());
- $profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'aws_csm');
- return function () use ($profile, $filename) {
- if (!@is_readable($filename)) {
- return self::reject("Cannot read CSM config from $filename");
- }
- $data = \Aws\parse_ini_file($filename, true);
- if ($data === false) {
- return self::reject("Invalid config file: $filename");
- }
- if (!isset($data[$profile])) {
- return self::reject("'$profile' not found in config file");
- }
- if (!isset($data[$profile]['csm_enabled'])) {
- return self::reject("Required CSM config values not present in
- INI profile '{$profile}' ({$filename})");
- }
-
- if (empty($data[$profile]['csm_host'])) {
- $data[$profile]['csm_host'] = self::DEFAULT_HOST;
- }
-
- if (empty($data[$profile]['csm_port'])) {
- $data[$profile]['csm_port'] = self::DEFAULT_PORT;
- }
-
- if (empty($data[$profile]['csm_client_id'])) {
- $data[$profile]['csm_client_id'] = self::DEFAULT_CLIENT_ID;
- }
- return Promise\Create::promiseFor(
- new Configuration(
- $data[$profile]['csm_enabled'],
- $data[$profile]['csm_host'],
- $data[$profile]['csm_port'],
- $data[$profile]['csm_client_id']
- )
- );
- };
- }
-
- public static function unwrap($config)
- {
- if (is_callable($config)) {
- $config = $config();
- }
- if ($config instanceof PromiseInterface) {
- $config = $config->wait();
- }
- if ($config instanceof ConfigurationInterface) {
- return $config;
- } elseif (is_array($config) && isset($config['enabled'])) {
- $client_id = isset($config['client_id']) ? $config['client_id']
- : self::DEFAULT_CLIENT_ID;
- $host = isset($config['host']) ? $config['host']
- : self::DEFAULT_HOST;
- $port = isset($config['port']) ? $config['port']
- : self::DEFAULT_PORT;
- return new Configuration($config['enabled'], $host, $port, $client_id);
- }
- throw new \InvalidArgumentException('Not a valid CSM configuration '
- . 'argument.');
- }
- }
|