Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/EndpointV2/EndpointDefinitionProvider.php

raw symbols SCIP
1 <?php
2
3 namespace Aws\EndpointV2;
4
5 use Aws\EndpointV2\Bdd\BddRuleset;
6 use Aws\EndpointV2\Ruleset\Ruleset;
7
8 /**
9 * Provides Endpoint-related artifacts used for endpoint resolution
10 * and testing.
11 */
12 class EndpointDefinitionProvider
13 {
14 /**
15 * Returns a parsed ruleset for the service — either a {@see BddRuleset}
16 * if a compiled BDD is shipped, or a {@see Ruleset} otherwise. Selection
17 * is driven by which file is packaged, so callers get a typed object
18 * rather than having to inspect the raw array.
19 *
20 * @param $service
21 * @param $apiVersion
22 * @param array $partitions
23 * @param null $baseDir
24 *
25 * @return Ruleset|BddRuleset
26 */
27 public static function getParsedRuleset(
28 $service,
29 $apiVersion,
30 array $partitions,
31 $baseDir = null
32 ): BddRuleset|Ruleset
33 {
34 $bdd = self::getEndpointBdd($service, $apiVersion, $baseDir, false);
35 if ($bdd !== null) {
36 return new BddRuleset($bdd, $partitions);
37 }
38 return new Ruleset(
39 self::getEndpointRuleset($service, $apiVersion, $baseDir),
40 $partitions
41 );
42 }
43
44 public static function getEndpointRuleset($service, $apiVersion, $baseDir = null)
45 {
46 return self::getData($service, $apiVersion, 'ruleset', $baseDir);
47 }
48
49 /**
50 * Returns the parsed endpoint BDD for a service, or null when
51 * `$throwIfMissing` is false and no BDD file is packaged.
52 */
53 public static function getEndpointBdd(
54 $service,
55 $apiVersion,
56 $baseDir = null,
57 $throwIfMissing = true
58 ) {
59 return self::getData($service, $apiVersion, 'bdd', $baseDir, $throwIfMissing);
60 }
61
62 public static function getEndpointTests($service, $apiVersion, $baseDir = null)
63 {
64 return self::getData($service, $apiVersion, 'tests', $baseDir);
65 }
66
67 public static function getPartitions()
68 {
69 $basePath = __DIR__ . '/../data';
70 $file = '/partitions.json';
71
72 if (file_exists($basePath . $file . '.php')) {
73 return require($basePath . $file . '.php');
74 } else {
75 return json_decode(file_get_contents($basePath . $file));
76 }
77 }
78
79 private static function getData($service, $apiVersion, $type, $baseDir, $throwIfMissing = true)
80 {
81 $basePath = $baseDir ?: __DIR__ . '/../data';
82 $serviceDir = $basePath . "/{$service}";
83 if (!is_dir($serviceDir)) {
84 if (!$throwIfMissing) {
85 return null;
86 }
87 throw new \InvalidArgumentException(
88 'Invalid service name.'
89 );
90 }
91
92 if ($apiVersion === 'latest') {
93 $apiVersion = self::getLatest($service);
94 }
95
96 $rulesetPath = $serviceDir . '/' . $apiVersion;
97 if (!is_dir($rulesetPath)) {
98 if (!$throwIfMissing) {
99 return null;
100 }
101 throw new \InvalidArgumentException(
102 'Invalid api version.'
103 );
104 }
105
106 $fileName = self::getFileName($type);
107
108 if (file_exists($rulesetPath . $fileName . '.json.php')) {
109 return require($rulesetPath . $fileName . '.json.php');
110 } elseif (file_exists($rulesetPath . $fileName . '.json')) {
111 return json_decode(file_get_contents($rulesetPath . $fileName . '.json'), true);
112 }
113
114 if (!$throwIfMissing) {
115 return null;
116 }
117
118 throw new \InvalidArgumentException(
119 'Specified ' . $type . ' endpoint file for ' . $service
120 . ' with api version ' . $apiVersion . ' does not exist.'
121 );
122 }
123
124 private static function getFileName($type): string
125 {
126 return match ($type) {
127 'tests' => '/endpoint-tests-1',
128 'bdd' => '/endpoint-bdd',
129 default => '/endpoint-rule-set-1',
130 };
131 }
132
133 private static function getLatest($service)
134 {
135 $manifest = \Aws\manifest();
136 return $manifest[$service]['versions']['latest'];
137 }
138 }