EndpointProvider.php 3.2 KB

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