| 1 |
<?php |
| 2 |
namespace Aws; |
| 3 |
|
| 4 |
use GuzzleHttp\Promise; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
abstract class AbstractConfigurationProvider |
| 12 |
{ |
| 13 |
const ENV_PROFILE = 'AWS_PROFILE'; |
| 14 |
const ENV_CONFIG_FILE = 'AWS_CONFIG_FILE'; |
| 15 |
|
| 16 |
public static $cacheKey; |
| 17 |
|
| 18 |
protected static $interfaceClass; |
| 19 |
protected static $exceptionClass; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public static function cache( |
| 33 |
callable $provider, |
| 34 |
CacheInterface $cache, |
| 35 |
$cacheKey = null |
| 36 |
) { |
| 37 |
$cacheKey = $cacheKey ?: static::$cacheKey; |
| 38 |
|
| 39 |
return function () use ($provider, $cache, $cacheKey) { |
| 40 |
$found = $cache->get($cacheKey); |
| 41 |
if ($found instanceof static::$interfaceClass) { |
| 42 |
return Promise\Create::promiseFor($found); |
| 43 |
} |
| 44 |
|
| 45 |
return $provider() |
| 46 |
->then(function ($config) use ( |
| 47 |
$cache, |
| 48 |
$cacheKey |
| 49 |
) { |
| 50 |
$cache->set($cacheKey, $config); |
| 51 |
return $config; |
| 52 |
}); |
| 53 |
}; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static function chain() |
| 64 |
{ |
| 65 |
$links = func_get_args(); |
| 66 |
if (empty($links)) { |
| 67 |
throw new \InvalidArgumentException('No providers in chain'); |
| 68 |
} |
| 69 |
|
| 70 |
return function () use ($links) { |
| 71 |
|
| 72 |
$parent = array_shift($links); |
| 73 |
$promise = $parent(); |
| 74 |
while ($next = array_shift($links)) { |
| 75 |
$promise = $promise->otherwise($next); |
| 76 |
} |
| 77 |
return $promise; |
| 78 |
}; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
protected static function getHomeDir() |
| 87 |
{ |
| 88 |
|
| 89 |
if ($homeDir = getenv('HOME')) { |
| 90 |
return $homeDir; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
$homeDrive = getenv('HOMEDRIVE'); |
| 95 |
$homePath = getenv('HOMEPATH'); |
| 96 |
|
| 97 |
return ($homeDrive && $homePath) ? $homeDrive . $homePath : null; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
protected static function getDefaultConfigFilename() |
| 107 |
{ |
| 108 |
if ($filename = getenv(self::ENV_CONFIG_FILE)) { |
| 109 |
return $filename; |
| 110 |
} |
| 111 |
return self::getHomeDir() . '/.aws/config'; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public static function memoize(callable $provider) |
| 122 |
{ |
| 123 |
return function () use ($provider) { |
| 124 |
static $result; |
| 125 |
static $isConstant; |
| 126 |
|
| 127 |
|
| 128 |
if ($isConstant) { |
| 129 |
return $result; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
if (null === $result) { |
| 134 |
$result = $provider(); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
return $result |
| 139 |
->then(function ($config) use (&$isConstant) { |
| 140 |
$isConstant = true; |
| 141 |
return $config; |
| 142 |
}); |
| 143 |
}; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
protected static function reject($msg) |
| 153 |
{ |
| 154 |
$exceptionClass = static::$exceptionClass; |
| 155 |
return new Promise\RejectedPromise(new $exceptionClass($msg)); |
| 156 |
} |
| 157 |
} |