| 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 |
|
| 14 |
private $clientPool = []; |
| 15 |
|
| 16 |
|
| 17 |
private $factory; |
| 18 |
|
| 19 |
|
| 20 |
private $partition; |
| 21 |
|
| 22 |
|
| 23 |
private $args; |
| 24 |
|
| 25 |
|
| 26 |
private $config; |
| 27 |
|
| 28 |
|
| 29 |
private $handlerList; |
| 30 |
|
| 31 |
|
| 32 |
private $aliases; |
| 33 |
|
| 34 |
|
| 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 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 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 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function getRegion() |
| 156 |
{ |
| 157 |
return $this->getClientFromPool()->getRegion(); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 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 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 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 |
|
| 248 |
|
| 249 |
|
| 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 |
} |