123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- namespace Aws;
- use GuzzleHttp\Promise;
- abstract class AbstractConfigurationProvider
- {
- const ENV_PROFILE = 'AWS_PROFILE';
- const ENV_CONFIG_FILE = 'AWS_CONFIG_FILE';
- public static $cacheKey;
- protected static $interfaceClass;
- protected static $exceptionClass;
-
- public static function cache(
- callable $provider,
- CacheInterface $cache,
- $cacheKey = null
- ) {
- $cacheKey = $cacheKey ?: static::$cacheKey;
- return function () use ($provider, $cache, $cacheKey) {
- $found = $cache->get($cacheKey);
- if ($found instanceof static::$interfaceClass) {
- return Promise\Create::promiseFor($found);
- }
- return $provider()
- ->then(function ($config) use (
- $cache,
- $cacheKey
- ) {
- $cache->set($cacheKey, $config);
- return $config;
- });
- };
- }
-
- public static function chain()
- {
- $links = func_get_args();
- if (empty($links)) {
- throw new \InvalidArgumentException('No providers in chain');
- }
- return function () use ($links) {
-
- $parent = array_shift($links);
- $promise = $parent();
- while ($next = array_shift($links)) {
- $promise = $promise->otherwise($next);
- }
- return $promise;
- };
- }
-
- protected static function getHomeDir()
- {
-
- if ($homeDir = getenv('HOME')) {
- return $homeDir;
- }
-
- $homeDrive = getenv('HOMEDRIVE');
- $homePath = getenv('HOMEPATH');
- return ($homeDrive && $homePath) ? $homeDrive . $homePath : null;
- }
-
- protected static function getDefaultConfigFilename()
- {
- if ($filename = getenv(self::ENV_CONFIG_FILE)) {
- return $filename;
- }
- return self::getHomeDir() . '/.aws/config';
- }
-
- public static function memoize(callable $provider)
- {
- return function () use ($provider) {
- static $result;
- static $isConstant;
-
- if ($isConstant) {
- return $result;
- }
-
- if (null === $result) {
- $result = $provider();
- }
-
- return $result
- ->then(function ($config) use (&$isConstant) {
- $isConstant = true;
- return $config;
- });
- };
- }
-
- protected static function reject($msg)
- {
- $exceptionClass = static::$exceptionClass;
- return new Promise\RejectedPromise(new $exceptionClass($msg));
- }
- }
|