| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Endpoint; |
| 4 |
|
| 5 |
/** |
| 6 |
* Provides endpoints based on an endpoint pattern configuration array. |
| 7 |
*/ |
| 8 |
class PatternEndpointProvider |
| 9 |
{ |
| 10 |
/** @var array */ |
| 11 |
private $patterns; |
| 12 |
/** |
| 13 |
* @param array $patterns Hash of endpoint patterns mapping to endpoint |
| 14 |
* configurations. |
| 15 |
*/ |
| 16 |
public function __construct(array $patterns) |
| 17 |
{ |
| 18 |
$this->patterns = $patterns; |
| 19 |
} |
| 20 |
public function __invoke(array $args = []) |
| 21 |
{ |
| 22 |
$service = isset($args['service']) ? $args['service'] : ''; |
| 23 |
$region = isset($args['region']) ? $args['region'] : ''; |
| 24 |
$keys = ["{$region}/{$service}", "{$region}/*", "*/{$service}", "*/*"]; |
| 25 |
foreach ($keys as $key) { |
| 26 |
if (isset($this->patterns[$key])) { |
| 27 |
return $this->expand($this->patterns[$key], isset($args['scheme']) ? $args['scheme'] : 'https', $service, $region); |
| 28 |
} |
| 29 |
} |
| 30 |
return null; |
| 31 |
} |
| 32 |
private function expand(array $config, $scheme, $service, $region) |
| 33 |
{ |
| 34 |
$config['endpoint'] = $scheme . '://' . \strtr($config['endpoint'], ['{service}' => $service, '{region}' => $region]); |
| 35 |
return $config; |
| 36 |
} |
| 37 |
} |
| 38 |
|