EndpointDefinitionProvider.php 2.2 KB

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