| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\EndpointV2; |
| 4 |
|
| 5 |
/** |
| 6 |
* Provides Endpoint-related artifacts used for endpoint resolution |
| 7 |
* and testing. |
| 8 |
*/ |
| 9 |
class EndpointDefinitionProvider |
| 10 |
{ |
| 11 |
public static function getEndpointRuleset($service, $apiVersion, $baseDir = null) |
| 12 |
{ |
| 13 |
return self::getData($service, $apiVersion, 'ruleset', $baseDir); |
| 14 |
} |
| 15 |
public static function getEndpointTests($service, $apiVersion, $baseDir = null) |
| 16 |
{ |
| 17 |
return self::getData($service, $apiVersion, 'tests', $baseDir); |
| 18 |
} |
| 19 |
public static function getPartitions() |
| 20 |
{ |
| 21 |
$basePath = __DIR__ . '/../data'; |
| 22 |
$file = '/partitions.json'; |
| 23 |
if (\file_exists($basePath . $file . '.php')) { |
| 24 |
return require $basePath . $file . '.php'; |
| 25 |
} else { |
| 26 |
return \json_decode(\file_get_contents($basePath . $file)); |
| 27 |
} |
| 28 |
} |
| 29 |
private static function getData($service, $apiVersion, $type, $baseDir) |
| 30 |
{ |
| 31 |
$basePath = $baseDir ? $baseDir : __DIR__ . '/../data'; |
| 32 |
$serviceDir = $basePath . "/{$service}"; |
| 33 |
if (!\is_dir($serviceDir)) { |
| 34 |
throw new \InvalidArgumentException('Invalid service name.'); |
| 35 |
} |
| 36 |
if ($apiVersion === 'latest') { |
| 37 |
$apiVersion = self::getLatest($service); |
| 38 |
} |
| 39 |
$rulesetPath = $serviceDir . '/' . $apiVersion; |
| 40 |
if (!\is_dir($rulesetPath)) { |
| 41 |
throw new \InvalidArgumentException('Invalid api version.'); |
| 42 |
} |
| 43 |
$fileName = $type === 'tests' ? '/endpoint-tests-1' : '/endpoint-rule-set-1'; |
| 44 |
if (\file_exists($rulesetPath . $fileName . '.json.php')) { |
| 45 |
return require $rulesetPath . $fileName . '.json.php'; |
| 46 |
} elseif (\file_exists($rulesetPath . $fileName . '.json')) { |
| 47 |
return \json_decode(\file_get_contents($rulesetPath . $fileName . '.json'), \true); |
| 48 |
} else { |
| 49 |
throw new \InvalidArgumentException('Specified ' . $type . ' endpoint file for ' . $service . ' with api version ' . $apiVersion . ' does not exist.'); |
| 50 |
} |
| 51 |
} |
| 52 |
private static function getLatest($service) |
| 53 |
{ |
| 54 |
$manifest = \Dudlewebs\WPMCS\s3\Aws\manifest(); |
| 55 |
return $manifest[$service]['versions']['latest']; |
| 56 |
} |
| 57 |
} |
| 58 |
|