| 1 | 
           efrain | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
              | 
        
        
            | 
            | 
           3 | 
           namespace 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 | 
              | 
        
        
            | 
            | 
           16 | 
               public static function getEndpointTests($service, $apiVersion, $baseDir = null)
  | 
        
        
            | 
            | 
           17 | 
               {
  | 
        
        
            | 
            | 
           18 | 
                   return self::getData($service, $apiVersion, 'tests', $baseDir);
  | 
        
        
            | 
            | 
           19 | 
               }
  | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
               public static function getPartitions()
  | 
        
        
            | 
            | 
           22 | 
               {
  | 
        
        
            | 
            | 
           23 | 
                   $basePath = __DIR__ . '/../data';
  | 
        
        
            | 
            | 
           24 | 
                   $file = '/partitions.json';
  | 
        
        
            | 
            | 
           25 | 
              | 
        
        
            | 
            | 
           26 | 
                   if (file_exists($basePath . $file . '.php')) {
  | 
        
        
            | 
            | 
           27 | 
                      return require($basePath . $file . '.php');
  | 
        
        
            | 
            | 
           28 | 
                   } else {
  | 
        
        
            | 
            | 
           29 | 
                       return json_decode(file_get_contents($basePath . $file));
  | 
        
        
            | 
            | 
           30 | 
                   }
  | 
        
        
            | 
            | 
           31 | 
               }
  | 
        
        
            | 
            | 
           32 | 
              | 
        
        
            | 
            | 
           33 | 
               private static function getData($service, $apiVersion, $type, $baseDir)
  | 
        
        
            | 
            | 
           34 | 
               {
  | 
        
        
            | 
            | 
           35 | 
                   $basePath = $baseDir ? $baseDir :  __DIR__ . '/../data';
  | 
        
        
            | 
            | 
           36 | 
                   $serviceDir = $basePath . "/{$service}";
  | 
        
        
            | 
            | 
           37 | 
                   if (!is_dir($serviceDir)) {
  | 
        
        
            | 
            | 
           38 | 
                       throw new \InvalidArgumentException(
  | 
        
        
            | 
            | 
           39 | 
                           'Invalid service name.'
  | 
        
        
            | 
            | 
           40 | 
                       );
  | 
        
        
            | 
            | 
           41 | 
                   }
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
                   if ($apiVersion === 'latest') {
  | 
        
        
            | 
            | 
           44 | 
                       $apiVersion = self::getLatest($service);
  | 
        
        
            | 
            | 
           45 | 
                   }
  | 
        
        
            | 
            | 
           46 | 
              | 
        
        
            | 
            | 
           47 | 
                   $rulesetPath = $serviceDir . '/' . $apiVersion;
  | 
        
        
            | 
            | 
           48 | 
                   if (!is_dir($rulesetPath)) {
  | 
        
        
            | 
            | 
           49 | 
                       throw new \InvalidArgumentException(
  | 
        
        
            | 
            | 
           50 | 
                           'Invalid api version.'
  | 
        
        
            | 
            | 
           51 | 
                       );
  | 
        
        
            | 
            | 
           52 | 
                   }
  | 
        
        
            | 
            | 
           53 | 
                   $fileName = $type === 'tests' ? '/endpoint-tests-1' : '/endpoint-rule-set-1';
  | 
        
        
            | 
            | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
                   if (file_exists($rulesetPath . $fileName . '.json.php')) {
  | 
        
        
            | 
            | 
           56 | 
                       return require($rulesetPath . $fileName . '.json.php');
  | 
        
        
            | 
            | 
           57 | 
                   } elseif (file_exists($rulesetPath . $fileName . '.json')) {
  | 
        
        
            | 
            | 
           58 | 
                       return json_decode(file_get_contents($rulesetPath . $fileName . '.json'), true);
  | 
        
        
            | 
            | 
           59 | 
                   } else {
  | 
        
        
            | 
            | 
           60 | 
                       throw new \InvalidArgumentException(
  | 
        
        
            | 
            | 
           61 | 
                           'Specified ' . $type . ' endpoint file for ' . $service . ' with api version ' . $apiVersion . ' does not exist.'
  | 
        
        
            | 
            | 
           62 | 
                       );
  | 
        
        
            | 
            | 
           63 | 
                   }
  | 
        
        
            | 
            | 
           64 | 
               }
  | 
        
        
            | 
            | 
           65 | 
              | 
        
        
            | 
            | 
           66 | 
               private static function getLatest($service)
  | 
        
        
            | 
            | 
           67 | 
               {
  | 
        
        
            | 
            | 
           68 | 
                   $manifest = \Aws\manifest();
  | 
        
        
            | 
            | 
           69 | 
                   return $manifest[$service]['versions']['latest'];
  | 
        
        
            | 
            | 
           70 | 
               }
  | 
        
        
            | 
            | 
           71 | 
           }
  |