123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace Aws\Endpoint;
- use JmesPath\Env;
- class PartitionEndpointProvider
- {
-
- private $partitions;
-
- private $defaultPartition;
-
- private $options;
-
- public function __construct(
- array $partitions,
- $defaultPartition = 'aws',
- $options = []
- ) {
- $this->partitions = array_map(function (array $definition) {
- return new Partition($definition);
- }, array_values($partitions));
- $this->defaultPartition = $defaultPartition;
- $this->options = $options;
- }
- public function __invoke(array $args = [])
- {
- $partition = $this->getPartition(
- isset($args['region']) ? $args['region'] : '',
- isset($args['service']) ? $args['service'] : ''
- );
- $args['options'] = $this->options;
- return $partition($args);
- }
-
- public function getPartition($region, $service)
- {
- foreach ($this->partitions as $partition) {
- if ($partition->isRegionMatch($region, $service)) {
- return $partition;
- }
- }
- return $this->getPartitionByName($this->defaultPartition);
- }
-
- public function getPartitionByName($name)
- {
- foreach ($this->partitions as $partition) {
- if ($name === $partition->getName()) {
- return $partition;
- }
- }
- }
-
- public static function defaultProvider($options = [])
- {
- $data = \Aws\load_compiled_json(__DIR__ . '/../data/endpoints.json');
- $prefixData = \Aws\load_compiled_json(__DIR__ . '/../data/endpoints_prefix_history.json');
- $mergedData = self::mergePrefixData($data, $prefixData);
- return new self($mergedData['partitions'], 'aws', $options);
- }
-
- public static function mergePrefixData($data, $prefixData)
- {
- $prefixGroups = $prefixData['prefix-groups'];
- foreach ($data["partitions"] as $index => $partition) {
- foreach ($prefixGroups as $current => $old) {
- $serviceData = Env::search("services.\"{$current}\"", $partition);
- if (!empty($serviceData)) {
- foreach ($old as $prefix) {
- if (empty(Env::search("services.\"{$prefix}\"", $partition))) {
- $data["partitions"][$index]["services"][$prefix] = $serviceData;
- }
- }
- }
- }
- }
- return $data;
- }
- }
|