PluginProbe
Media Cloud Sync / 1.2.12
Media Cloud Sync v1.2.12
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / s3 / Aws / AbstractConfigurationProvider.php

AbstractConfigurationProvider.php in Media Cloud Sync 1.2.12, at includes/sdk/s3/Aws/AbstractConfigurationProvider.php

134 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\Aws;
4
5 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise;
6 /**
7 * A configuration provider is a function that returns a promise that is
8 * fulfilled with a configuration object. This class provides base functionality
9 * usable by specific configuration provider implementations
10 */
11 abstract class AbstractConfigurationProvider
12 {
13 const ENV_PROFILE = 'AWS_PROFILE';
14 const ENV_CONFIG_FILE = 'AWS_CONFIG_FILE';
15 public static $cacheKey;
16 protected static $interfaceClass;
17 protected static $exceptionClass;
18 /**
19 * Wraps a config provider and saves provided configuration in an
20 * instance of Aws\CacheInterface. Forwards calls when no config found
21 * in cache and updates cache with the results.
22 *
23 * @param callable $provider Configuration provider function to wrap
24 * @param CacheInterface $cache Cache to store configuration
25 * @param string|null $cacheKey (optional) Cache key to use
26 *
27 * @return callable
28 */
29 public static function cache(callable $provider, CacheInterface $cache, $cacheKey = null)
30 {
31 $cacheKey = $cacheKey ?: static::$cacheKey;
32 return function () use($provider, $cache, $cacheKey) {
33 $found = $cache->get($cacheKey);
34 if ($found instanceof static::$interfaceClass) {
35 return Promise\Create::promiseFor($found);
36 }
37 return $provider()->then(function ($config) use($cache, $cacheKey) {
38 $cache->set($cacheKey, $config);
39 return $config;
40 });
41 };
42 }
43 /**
44 * Creates an aggregate configuration provider that invokes the provided
45 * variadic providers one after the other until a provider returns
46 * configuration.
47 *
48 * @return callable
49 */
50 public static function chain()
51 {
52 $links = \func_get_args();
53 if (empty($links)) {
54 throw new \InvalidArgumentException('No providers in chain');
55 }
56 return function () use($links) {
57 /** @var callable $parent */
58 $parent = \array_shift($links);
59 $promise = $parent();
60 while ($next = \array_shift($links)) {
61 $promise = $promise->otherwise($next);
62 }
63 return $promise;
64 };
65 }
66 /**
67 * Gets the environment's HOME directory if available.
68 *
69 * @return null|string
70 */
71 protected static function getHomeDir()
72 {
73 // On Linux/Unix-like systems, use the HOME environment variable
74 if ($homeDir = \getenv('HOME')) {
75 return $homeDir;
76 }
77 // Get the HOMEDRIVE and HOMEPATH values for Windows hosts
78 $homeDrive = \getenv('HOMEDRIVE');
79 $homePath = \getenv('HOMEPATH');
80 return $homeDrive && $homePath ? $homeDrive . $homePath : null;
81 }
82 /**
83 * Gets default config file location from environment, falling back to aws
84 * default location
85 *
86 * @return string
87 */
88 protected static function getDefaultConfigFilename()
89 {
90 if ($filename = \getenv(self::ENV_CONFIG_FILE)) {
91 return $filename;
92 }
93 return self::getHomeDir() . '/.aws/config';
94 }
95 /**
96 * Wraps a config provider and caches previously provided configuration.
97 *
98 * @param callable $provider Config provider function to wrap.
99 *
100 * @return callable
101 */
102 public static function memoize(callable $provider)
103 {
104 return function () use($provider) {
105 static $result;
106 static $isConstant;
107 // Constant config will be returned constantly.
108 if ($isConstant) {
109 return $result;
110 }
111 // Create the initial promise that will be used as the cached value
112 if (null === $result) {
113 $result = $provider();
114 }
115 // Return config and set flag that provider is already set
116 return $result->then(function ($config) use(&$isConstant) {
117 $isConstant = \true;
118 return $config;
119 });
120 };
121 }
122 /**
123 * Reject promise with standardized exception.
124 *
125 * @param $msg
126 * @return Promise\RejectedPromise
127 */
128 protected static function reject($msg)
129 {
130 $exceptionClass = static::$exceptionClass;
131 return new Promise\RejectedPromise(new $exceptionClass($msg));
132 }
133 }
134