Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/EndpointV2/EndpointProviderV2.php

raw symbols SCIP
1 <?php
2
3 namespace Aws\EndpointV2;
4
5 use Aws\EndpointV2\Bdd\BddEvaluator;
6 use Aws\EndpointV2\Bdd\BddResultResolver;
7 use Aws\EndpointV2\Bdd\BddRuleset;
8 use Aws\EndpointV2\Ruleset\Ruleset;
9 use Aws\EndpointV2\Ruleset\RulesetEndpoint;
10 use Aws\Exception\UnresolvedEndpointException;
11 use Aws\LruArrayCache;
12
13 /**
14 * Given a service's ruleset and client-provided input parameters, provides
15 * either an object reflecting the properties of a resolved endpoint,
16 * or throws an error.
17 *
18 * Supports both the classic decision tree ruleset (`endpointRuleSet` trait)
19 * and the binary decision diagram ruleset (`endpointBdd` trait). A raw
20 * definition array is always interpreted as a tree ruleset; to use a BDD,
21 * construct a {@see BddRuleset} and hand it in directly.
22 */
23 class EndpointProviderV2
24 {
25 /** @var Ruleset|null */
26 private $ruleset;
27
28 /** @var BddRuleset|null */
29 private $bddRuleset;
30
31 /** @var BddEvaluator|null */
32 private $bddEvaluator;
33
34 /** @var LruArrayCache */
35 private $cache;
36
37 /**
38 * @param array|Ruleset|BddRuleset $ruleset A parsed ruleset instance, or
39 * a raw tree ruleset array from the service model.
40 * @param array $partitions AWS partitions data. Ignored when $ruleset is
41 * already a parsed instance, since the instance carries its own
42 * partition data.
43 */
44 public function __construct($ruleset, array $partitions)
45 {
46 if ($ruleset instanceof BddRuleset) {
47 $this->bddRuleset = $ruleset;
48 $this->bddEvaluator = new BddEvaluator(
49 $ruleset,
50 new BddResultResolver($ruleset)
51 );
52 } elseif ($ruleset instanceof Ruleset) {
53 $this->ruleset = $ruleset;
54 } elseif (is_array($ruleset)) {
55 $this->ruleset = new Ruleset($ruleset, $partitions);
56 } else {
57 throw new \InvalidArgumentException(
58 'EndpointProviderV2 expects an array, Ruleset, or BddRuleset'
59 . ' but received ' . (is_object($ruleset)
60 ? get_class($ruleset)
61 : gettype($ruleset))
62 );
63 }
64
65 $this->cache = new LruArrayCache(100);
66 }
67
68 /**
69 * Returns the parsed tree ruleset for services using the legacy
70 * `endpointRuleSet` trait. Returns null when the provider was built from
71 * an `endpointBdd` trait.
72 *
73 * @return Ruleset|null
74 */
75 public function getRuleset()
76 {
77 return $this->ruleset;
78 }
79
80 /**
81 * Returns the parsed BDD ruleset for services using the `endpointBdd`
82 * trait, or null when the provider was built from a tree ruleset.
83 */
84 public function getBddRuleset(): ?BddRuleset
85 {
86 return $this->bddRuleset;
87 }
88
89 /**
90 * Given input parameters, determines the correct endpoint or an error
91 * to be thrown for a given request.
92 *
93 * @return RulesetEndpoint
94 * @throws UnresolvedEndpointException
95 */
96 public function resolveEndpoint(array $inputParameters)
97 {
98 $hashedParams = $this->hashInputParameters($inputParameters);
99 $match = $this->cache->get($hashedParams);
100
101 if (!is_null($match)) {
102 return $match;
103 }
104
105 $endpoint = $this->bddEvaluator !== null
106 ? $this->bddEvaluator->evaluate($inputParameters)
107 : $this->ruleset->evaluate($inputParameters);
108
109 // This condition just applies to endpoint resolution
110 // through the decision tree evaluation process.
111 if ($endpoint === false) {
112 throw new UnresolvedEndpointException(
113 'Unable to resolve an endpoint using the provider arguments: '
114 . json_encode($inputParameters)
115 );
116 }
117
118 $this->cache->set($hashedParams, $endpoint);
119
120 return $endpoint;
121 }
122
123 private function hashInputParameters($inputParameters)
124 {
125 return md5(serialize($inputParameters));
126 }
127
128 /**
129 * @return array
130 */
131 public function getActiveParameters(): array
132 {
133 if ($this->bddRuleset !== null) {
134 return $this->bddRuleset->getParameters();
135 }
136
137 return $this->ruleset->getParameters();
138 }
139 }