| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Endpoint; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\JmesPath\Env; |
| 6 |
class PartitionEndpointProvider |
| 7 |
{ |
| 8 |
/** @var Partition[] */ |
| 9 |
private $partitions; |
| 10 |
/** @var string */ |
| 11 |
private $defaultPartition; |
| 12 |
/** @var array */ |
| 13 |
private $options; |
| 14 |
/** |
| 15 |
* The 'options' parameter accepts the following arguments: |
| 16 |
* |
| 17 |
* - sts_regional_endpoints: For STS legacy regions, set to 'regional' to |
| 18 |
* use regional endpoints, 'legacy' to use the legacy global endpoint. |
| 19 |
* Defaults to 'legacy'. |
| 20 |
* - s3_us_east_1_regional_endpoint: For S3 us-east-1 region, set to 'regional' |
| 21 |
* to use the regional endpoint, 'legacy' to use the legacy global endpoint. |
| 22 |
* Defaults to 'legacy'. |
| 23 |
* |
| 24 |
* @param array $partitions |
| 25 |
* @param string $defaultPartition |
| 26 |
* @param array $options |
| 27 |
*/ |
| 28 |
public function __construct(array $partitions, $defaultPartition = 'aws', $options = []) |
| 29 |
{ |
| 30 |
$this->partitions = \array_map(function (array $definition) { |
| 31 |
return new Partition($definition); |
| 32 |
}, \array_values($partitions)); |
| 33 |
$this->defaultPartition = $defaultPartition; |
| 34 |
$this->options = $options; |
| 35 |
} |
| 36 |
public function __invoke(array $args = []) |
| 37 |
{ |
| 38 |
$partition = $this->getPartition(isset($args['region']) ? $args['region'] : '', isset($args['service']) ? $args['service'] : ''); |
| 39 |
$args['options'] = $this->options; |
| 40 |
return $partition($args); |
| 41 |
} |
| 42 |
/** |
| 43 |
* Returns the partition containing the provided region or the default |
| 44 |
* partition if no match is found. |
| 45 |
* |
| 46 |
* @param string $region |
| 47 |
* @param string $service |
| 48 |
* |
| 49 |
* @return Partition |
| 50 |
*/ |
| 51 |
public function getPartition($region, $service) |
| 52 |
{ |
| 53 |
foreach ($this->partitions as $partition) { |
| 54 |
if ($partition->isRegionMatch($region, $service)) { |
| 55 |
return $partition; |
| 56 |
} |
| 57 |
} |
| 58 |
return $this->getPartitionByName($this->defaultPartition); |
| 59 |
} |
| 60 |
/** |
| 61 |
* Returns the partition with the provided name or null if no partition with |
| 62 |
* the provided name can be found. |
| 63 |
* |
| 64 |
* @param string $name |
| 65 |
* |
| 66 |
* @return Partition|null |
| 67 |
*/ |
| 68 |
public function getPartitionByName($name) |
| 69 |
{ |
| 70 |
foreach ($this->partitions as $partition) { |
| 71 |
if ($name === $partition->getName()) { |
| 72 |
return $partition; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
/** |
| 77 |
* Creates and returns the default SDK partition provider. |
| 78 |
* |
| 79 |
* @param array $options |
| 80 |
* @return PartitionEndpointProvider |
| 81 |
*/ |
| 82 |
public static function defaultProvider($options = []) |
| 83 |
{ |
| 84 |
$data = \Dudlewebs\WPMCS\s3\Aws\load_compiled_json(__DIR__ . '/../data/endpoints.json'); |
| 85 |
$prefixData = \Dudlewebs\WPMCS\s3\Aws\load_compiled_json(__DIR__ . '/../data/endpoints_prefix_history.json'); |
| 86 |
$mergedData = self::mergePrefixData($data, $prefixData); |
| 87 |
return new self($mergedData['partitions'], 'aws', $options); |
| 88 |
} |
| 89 |
/** |
| 90 |
* Copy endpoint data for other prefixes used by a given service |
| 91 |
* |
| 92 |
* @param $data |
| 93 |
* @param $prefixData |
| 94 |
* @return array |
| 95 |
*/ |
| 96 |
public static function mergePrefixData($data, $prefixData) |
| 97 |
{ |
| 98 |
$prefixGroups = $prefixData['prefix-groups']; |
| 99 |
foreach ($data["partitions"] as $index => $partition) { |
| 100 |
foreach ($prefixGroups as $current => $old) { |
| 101 |
$serviceData = Env::search("services.\"{$current}\"", $partition); |
| 102 |
if (!empty($serviceData)) { |
| 103 |
foreach ($old as $prefix) { |
| 104 |
if (empty(Env::search("services.\"{$prefix}\"", $partition))) { |
| 105 |
$data["partitions"][$index]["services"][$prefix] = $serviceData; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
return $data; |
| 112 |
} |
| 113 |
} |
| 114 |
|