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