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