| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Endpoint; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\Exception\UnresolvedEndpointException; |
| 6 |
/** |
| 7 |
* Endpoint providers. |
| 8 |
* |
| 9 |
* An endpoint provider is a function that accepts a hash of endpoint options, |
| 10 |
* including but not limited to "service" and "region" key value pairs. The |
| 11 |
* endpoint provider function returns a hash of endpoint data, which MUST |
| 12 |
* include an "endpoint" key value pair that represents the resolved endpoint |
| 13 |
* or NULL if an endpoint cannot be determined. |
| 14 |
* |
| 15 |
* You can wrap your calls to an endpoint provider with the |
| 16 |
* {@see EndpointProvider::resolve} function to ensure that an endpoint hash is |
| 17 |
* created. If an endpoint hash is not created, then the resolve() function |
| 18 |
* will throw an {@see Aws\Exception\UnresolvedEndpointException}. |
| 19 |
* |
| 20 |
* use Aws\Endpoint\EndpointProvider; |
| 21 |
* $provider = EndpointProvider::defaultProvider(); |
| 22 |
* // Returns an array or NULL. |
| 23 |
* $endpoint = $provider(['service' => 'ec2', 'region' => 'us-west-2']); |
| 24 |
* // Returns an endpoint array or throws. |
| 25 |
* $endpoint = EndpointProvider::resolve($provider, [ |
| 26 |
* 'service' => 'ec2', |
| 27 |
* 'region' => 'us-west-2' |
| 28 |
* ]); |
| 29 |
* |
| 30 |
* You can compose multiple providers into a single provider using |
| 31 |
* {@see Aws\or_chain}. This function accepts providers as arguments and |
| 32 |
* returns a new function that will invoke each provider until a non-null value |
| 33 |
* is returned. |
| 34 |
* |
| 35 |
* $a = function (array $args) { |
| 36 |
* if ($args['region'] === 'my-test-region') { |
| 37 |
* return ['endpoint' => 'http://localhost:123/api']; |
| 38 |
* } |
| 39 |
* }; |
| 40 |
* $b = EndpointProvider::defaultProvider(); |
| 41 |
* $c = \Aws\or_chain($a, $b); |
| 42 |
* $config = ['service' => 'ec2', 'region' => 'my-test-region']; |
| 43 |
* $res = $c($config); // $a handles this. |
| 44 |
* $config['region'] = 'us-west-2'; |
| 45 |
* $res = $c($config); // $b handles this. |
| 46 |
*/ |
| 47 |
class EndpointProvider |
| 48 |
{ |
| 49 |
/** |
| 50 |
* Resolves and endpoint provider and ensures a non-null return value. |
| 51 |
* |
| 52 |
* @param callable $provider Provider function to invoke. |
| 53 |
* @param array $args Endpoint arguments to pass to the provider. |
| 54 |
* |
| 55 |
* @return array |
| 56 |
* @throws UnresolvedEndpointException |
| 57 |
*/ |
| 58 |
public static function resolve(callable $provider, array $args = []) |
| 59 |
{ |
| 60 |
$result = $provider($args); |
| 61 |
if (\is_array($result)) { |
| 62 |
return $result; |
| 63 |
} |
| 64 |
throw new UnresolvedEndpointException('Unable to resolve an endpoint using the provider arguments: ' . \json_encode($args) . '. Note: you can provide an "endpoint" ' . 'option to a client constructor to bypass invoking an endpoint ' . 'provider.'); |
| 65 |
} |
| 66 |
/** |
| 67 |
* Creates and returns the default SDK endpoint provider. |
| 68 |
* |
| 69 |
* @deprecated Use an instance of \Aws\Endpoint\Partition instead. |
| 70 |
* |
| 71 |
* @return callable |
| 72 |
*/ |
| 73 |
public static function defaultProvider() |
| 74 |
{ |
| 75 |
return PartitionEndpointProvider::defaultProvider(); |
| 76 |
} |
| 77 |
/** |
| 78 |
* Creates and returns an endpoint provider that uses patterns from an |
| 79 |
* array. |
| 80 |
* |
| 81 |
* @param array $patterns Endpoint patterns |
| 82 |
* |
| 83 |
* @return callable |
| 84 |
*/ |
| 85 |
public static function patterns(array $patterns) |
| 86 |
{ |
| 87 |
return new PatternEndpointProvider($patterns); |
| 88 |
} |
| 89 |
} |
| 90 |
|