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 / MultiRegionClient.php

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

190 lines 7.4 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\Aws\Endpoint\PartitionEndpointProvider;
6 use Dudlewebs\WPMCS\s3\Aws\Endpoint\PartitionInterface;
7 class MultiRegionClient implements AwsClientInterface
8 {
9 use AwsClientTrait;
10 /** @var AwsClientInterface[] A pool of clients keyed by region. */
11 private $clientPool = [];
12 /** @var callable */
13 private $factory;
14 /** @var PartitionInterface */
15 private $partition;
16 /** @var array */
17 private $args;
18 /** @var array */
19 private $config;
20 /** @var HandlerList */
21 private $handlerList;
22 /** @var array */
23 private $aliases;
24 /** @var callable */
25 private $customHandler;
26 public static function getArguments()
27 {
28 $args = \array_intersect_key(ClientResolver::getDefaultArguments(), ['service' => \true, 'region' => \true]);
29 $args['region']['required'] = \false;
30 return $args + ['client_factory' => ['type' => 'config', 'valid' => ['callable'], 'doc' => 'A callable that takes an array of client' . ' configuration arguments and returns a regionalized' . ' client.', 'required' => \true, 'internal' => \true, 'default' => function (array $args) {
31 $namespace = manifest($args['service'])['namespace'];
32 $klass = "Dudlewebs\\WPMCS\\s3\\Aws\\{$namespace}\\{$namespace}Client";
33 $region = isset($args['region']) ? $args['region'] : null;
34 return function (array $args) use($klass, $region) {
35 if ($region && empty($args['region'])) {
36 $args['region'] = $region;
37 }
38 return new $klass($args);
39 };
40 }], 'partition' => ['type' => 'config', 'valid' => ['string', PartitionInterface::class], 'doc' => 'AWS partition to connect to. Valid partitions' . ' include "aws," "aws-cn," and "aws-us-gov." Used to' . ' restrict the scope of the mapRegions method.', 'default' => function (array $args) {
41 $region = isset($args['region']) ? $args['region'] : '';
42 return PartitionEndpointProvider::defaultProvider()->getPartition($region, $args['service']);
43 }, 'fn' => function ($value, array &$args) {
44 if (\is_string($value)) {
45 $value = PartitionEndpointProvider::defaultProvider()->getPartitionByName($value);
46 }
47 if (!$value instanceof PartitionInterface) {
48 throw new \InvalidArgumentException('No valid partition' . ' was provided. Provide a concrete partition or' . ' the name of a partition (e.g., "aws," "aws-cn,"' . ' or "aws-us-gov").');
49 }
50 $args['partition'] = $value;
51 $args['endpoint_provider'] = $value;
52 }]];
53 }
54 /**
55 * The multi-region client constructor accepts the following options:
56 *
57 * - client_factory: (callable) An optional callable that takes an array of
58 * client configuration arguments and returns a regionalized client.
59 * - partition: (Aws\Endpoint\Partition|string) AWS partition to connect to.
60 * Valid partitions include "aws," "aws-cn," and "aws-us-gov." Used to
61 * restrict the scope of the mapRegions method.
62 * - region: (string) Region to connect to when no override is provided.
63 * Used to create the default client factory and determine the appropriate
64 * AWS partition when present.
65 *
66 * @param array $args Client configuration arguments.
67 */
68 public function __construct(array $args = [])
69 {
70 if (!isset($args['service'])) {
71 $args['service'] = $this->parseClass();
72 }
73 $this->handlerList = new HandlerList(function (CommandInterface $command) {
74 list($region, $args) = $this->getRegionFromArgs($command->toArray());
75 $command = $this->getClientFromPool($region)->getCommand($command->getName(), $args);
76 if ($this->isUseCustomHandler()) {
77 $command->getHandlerList()->setHandler($this->customHandler);
78 }
79 return $this->executeAsync($command);
80 });
81 $argDefinitions = static::getArguments();
82 $resolver = new ClientResolver($argDefinitions);
83 $args = $resolver->resolve($args, $this->handlerList);
84 $this->config = $args['config'];
85 $this->factory = $args['client_factory'];
86 $this->partition = $args['partition'];
87 $this->args = \array_diff_key($args, $args['config']);
88 }
89 /**
90 * Get the region to which the client is configured to send requests by
91 * default.
92 *
93 * @return string
94 */
95 public function getRegion()
96 {
97 return $this->getClientFromPool()->getRegion();
98 }
99 /**
100 * Create a command for an operation name.
101 *
102 * Special keys may be set on the command to control how it behaves,
103 * including:
104 *
105 * - @http: Associative array of transfer specific options to apply to the
106 * request that is serialized for this command. Available keys include
107 * "proxy", "verify", "timeout", "connect_timeout", "debug", "delay", and
108 * "headers".
109 * - @region: The region to which the command should be sent.
110 *
111 * @param string $name Name of the operation to use in the command
112 * @param array $args Arguments to pass to the command
113 *
114 * @return CommandInterface
115 * @throws \InvalidArgumentException if no command can be found by name
116 */
117 public function getCommand($name, array $args = [])
118 {
119 return new Command($name, $args, clone $this->getHandlerList());
120 }
121 public function getConfig($option = null)
122 {
123 if (null === $option) {
124 return $this->config;
125 }
126 if (isset($this->config[$option])) {
127 return $this->config[$option];
128 }
129 return $this->getClientFromPool()->getConfig($option);
130 }
131 public function getCredentials()
132 {
133 return $this->getClientFromPool()->getCredentials();
134 }
135 public function getHandlerList()
136 {
137 return $this->handlerList;
138 }
139 public function getApi()
140 {
141 return $this->getClientFromPool()->getApi();
142 }
143 public function getEndpoint()
144 {
145 return $this->getClientFromPool()->getEndpoint();
146 }
147 public function useCustomHandler(callable $handler)
148 {
149 $this->customHandler = $handler;
150 }
151 private function isUseCustomHandler()
152 {
153 return isset($this->customHandler);
154 }
155 /**
156 * @param string $region Omit this argument or pass in an empty string to
157 * allow the configured client factory to apply the
158 * region.
159 *
160 * @return AwsClientInterface
161 */
162 protected function getClientFromPool($region = '')
163 {
164 if (empty($this->clientPool[$region])) {
165 $factory = $this->factory;
166 $this->clientPool[$region] = $factory(\array_replace($this->args, \array_filter(['region' => $region])));
167 }
168 return $this->clientPool[$region];
169 }
170 /**
171 * Parse the class name and return the "service" name of the client.
172 *
173 * @return string
174 */
175 private function parseClass()
176 {
177 $klass = \get_class($this);
178 if ($klass === __CLASS__) {
179 return '';
180 }
181 return \strtolower(\substr($klass, \strrpos($klass, '\\') + 1, -17));
182 }
183 private function getRegionFromArgs(array $args)
184 {
185 $region = isset($args['@region']) ? $args['@region'] : $this->getRegion();
186 unset($args['@region']);
187 return [$region, $args];
188 }
189 }
190