Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/MultiRegionClient.php

raw symbols SCIP
1 <?php
2 namespace Aws;
3
4 use Aws\Endpoint\PartitionEndpointProvider;
5 use Aws\Endpoint\PartitionInterface;
6 use Aws\EndpointV2\EndpointProviderV2;
7 use Aws\EndpointV2\EndpointDefinitionProvider;
8
9 class MultiRegionClient implements AwsClientInterface
10 {
11 use AwsClientTrait;
12
13 /** @var AwsClientInterface[] A pool of clients keyed by region. */
14 private $clientPool = [];
15
16 /** @var callable */
17 private $factory;
18
19 /** @var PartitionInterface */
20 private $partition;
21
22 /** @var array */
23 private $args;
24
25 /** @var array */
26 private $config;
27
28 /** @var HandlerList */
29 private $handlerList;
30
31 /** @var array */
32 private $aliases;
33
34 /** @var callable */
35 private $customHandler;
36
37 public static function getArguments()
38 {
39 $args = array_intersect_key(
40 ClientResolver::getDefaultArguments(),
41 ['service' => true, 'region' => true]
42 );
43 $args['region']['required'] = false;
44 unset($args['region']['fn']);
45 unset($args['region']['default']);
46
47 return $args + [
48 'client_factory' => [
49 'type' => 'config',
50 'valid' => ['callable'],
51 'doc' => 'A callable that takes an array of client'
52 . ' configuration arguments and returns a regionalized'
53 . ' client.',
54 'required' => true,
55 'internal' => true,
56 'default' => function (array $args) {
57 $namespace = manifest($args['service'])['namespace'];
58 $klass = "Aws\\{$namespace}\\{$namespace}Client";
59 $region = isset($args['region']) ? $args['region'] : null;
60
61 return function (array $args) use ($klass, $region) {
62 if ($region && empty($args['region'])) {
63 $args['region'] = $region;
64 }
65
66 return new $klass($args);
67 };
68 },
69 ],
70 'partition' => [
71 'type' => 'config',
72 'valid' => ['string', PartitionInterface::class],
73 'doc' => 'AWS partition to connect to. Valid partitions'
74 . ' include "aws," "aws-cn," and "aws-us-gov." Used to'
75 . ' restrict the scope of the mapRegions method.',
76 'default' => function (array $args) {
77 $region = isset($args['region']) ? $args['region'] : '';
78 return PartitionEndpointProvider::defaultProvider()
79 ->getPartition($region, $args['service']);
80 },
81 'fn' => function ($value, array &$args) {
82 if (is_string($value)) {
83 $value = PartitionEndpointProvider::defaultProvider()
84 ->getPartitionByName($value);
85 }
86
87 if (!$value instanceof PartitionInterface) {
88 throw new \InvalidArgumentException('No valid partition'
89 . ' was provided. Provide a concrete partition or'
90 . ' the name of a partition (e.g., "aws," "aws-cn,"'
91 . ' or "aws-us-gov").'
92 );
93 }
94 $partitions = EndpointDefinitionProvider::getPartitions();
95 $parsed = EndpointDefinitionProvider::getParsedRuleset(
96 $args['service'],
97 isset($args['version']) ? $args['version'] : 'latest',
98 $partitions
99 );
100 $args['endpoint_provider'] = new EndpointProviderV2($parsed, $partitions);
101 }
102 ],
103 ];
104 }
105
106 /**
107 * The multi-region client constructor accepts the following options:
108 *
109 * - client_factory: (callable) An optional callable that takes an array of
110 * client configuration arguments and returns a regionalized client.
111 * - partition: (Aws\Endpoint\Partition|string) AWS partition to connect to.
112 * Valid partitions include "aws," "aws-cn," and "aws-us-gov." Used to
113 * restrict the scope of the mapRegions method.
114 * - region: (string) Region to connect to when no override is provided.
115 * Used to create the default client factory and determine the appropriate
116 * AWS partition when present.
117 *
118 * @param array $args Client configuration arguments.
119 */
120 public function __construct(array $args = [])
121 {
122 if (!isset($args['service'])) {
123 $args['service'] = $this->parseClass();
124 }
125
126 $this->handlerList = new HandlerList(function (
127 CommandInterface $command
128 ) {
129 list($region, $args) = $this->getRegionFromArgs($command->toArray());
130 $command = $this->getClientFromPool($region)
131 ->getCommand($command->getName(), $args);
132
133 if ($this->isUseCustomHandler()) {
134 $command->getHandlerList()->setHandler($this->customHandler);
135 }
136
137 return $this->executeAsync($command);
138 });
139
140 $argDefinitions = static::getArguments();
141 $resolver = new ClientResolver($argDefinitions);
142 $args = $resolver->resolve($args, $this->handlerList);
143 $this->config = $args['config'];
144 $this->factory = $args['client_factory'];
145 $this->partition = $args['partition'];
146 $this->args = array_diff_key($args, $args['config']);
147 }
148
149 /**
150 * Get the region to which the client is configured to send requests by
151 * default.
152 *
153 * @return string
154 */
155 public function getRegion()
156 {
157 return $this->getClientFromPool()->getRegion();
158 }
159
160 /**
161 * Create a command for an operation name.
162 *
163 * Special keys may be set on the command to control how it behaves,
164 * including:
165 *
166 * - @http: Associative array of transfer specific options to apply to the
167 * request that is serialized for this command. Available keys include
168 * "proxy", "verify", "timeout", "connect_timeout", "debug", "delay", and
169 * "headers".
170 * - @region: The region to which the command should be sent.
171 *
172 * @param string $name Name of the operation to use in the command
173 * @param array $args Arguments to pass to the command
174 *
175 * @return CommandInterface
176 * @throws \InvalidArgumentException if no command can be found by name
177 */
178 public function getCommand($name, array $args = [])
179 {
180 return new Command($name, $args, clone $this->getHandlerList());
181 }
182
183 public function getConfig($option = null)
184 {
185 if (null === $option) {
186 return $this->config;
187 }
188
189 if (isset($this->config[$option])) {
190 return $this->config[$option];
191 }
192
193 return $this->getClientFromPool()->getConfig($option);
194 }
195
196 public function getCredentials()
197 {
198 return $this->getClientFromPool()->getCredentials();
199 }
200
201 public function getHandlerList()
202 {
203 return $this->handlerList;
204 }
205
206 public function getApi()
207 {
208 return $this->getClientFromPool()->getApi();
209 }
210
211 public function getEndpoint()
212 {
213 return $this->getClientFromPool()->getEndpoint();
214 }
215
216 public function useCustomHandler(callable $handler)
217 {
218 $this->customHandler = $handler;
219 }
220
221 private function isUseCustomHandler()
222 {
223 return isset($this->customHandler);
224 }
225
226 /**
227 * @param string $region Omit this argument or pass in an empty string to
228 * allow the configured client factory to apply the
229 * region.
230 *
231 * @return AwsClientInterface
232 */
233 protected function getClientFromPool($region = '')
234 {
235 $region = $region ?? '';
236 if (empty($this->clientPool[$region])) {
237 $factory = $this->factory;
238 $this->clientPool[$region] = $factory(
239 array_replace($this->args, array_filter(['region' => $region]))
240 );
241 }
242
243 return $this->clientPool[$region];
244 }
245
246 /**
247 * Parse the class name and return the "service" name of the client.
248 *
249 * @return string
250 */
251 private function parseClass()
252 {
253 $klass = get_class($this);
254
255 if ($klass === __CLASS__) {
256 return '';
257 }
258
259 return strtolower(substr($klass, strrpos($klass, '\\') + 1, -17));
260 }
261
262 private function getRegionFromArgs(array $args)
263 {
264 $region = isset($args['@region'])
265 ? $args['@region']
266 : $this->getRegion();
267 unset($args['@region']);
268
269 return [$region, $args];
270 }
271 }