-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathClientAnnotator.php
163 lines (135 loc) · 4.41 KB
/
ClientAnnotator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
use Aws\Api\ApiProvider;
class ClientAnnotator
{
/** @var ReflectionClass */
private $reflection;
/** @var string */
private $endpoint;
/** @var string[] */
private $versions;
/** @var array */
private $methods;
/** @var array */
private $aliases;
public function __construct($clientClassName)
{
$this->reflection = new ReflectionClass($clientClassName);
$this->aliases = \Aws\load_compiled_json(__DIR__ . '/../src/data/aliases.json');
}
/**
* Adds @method annotations to a client class.
*
* @return bool TRUE on success, FALSE on failure
*/
public function updateApiMethodAnnotations()
{
$updater = new ClassAnnotationUpdater(
$this->reflection,
$this->getMethodAnnotations(),
$this->getDefaultDocComment(),
'/^\* @method (\\\\Aws\\\\Result|\\\\GuzzleHttp\\\\Promise\\\\Promise) /'
);
return $updater->update();
}
private function getMethodAnnotations()
{
$annotations = [];
foreach ($this->getMethods() as $command => $apiVersions) {
$commandMethods = [
$command => '\\Aws\\Result',
"{$command}Async" => '\\GuzzleHttp\\Promise\\Promise',
];
foreach ($commandMethods as $method => $returnType) {
$annotations []= $this->getAnnotationLine(
$method,
$returnType,
$apiVersions
);
}
}
return $annotations;
}
private function getAnnotationLine($method, $return, array $versionsWithSupport)
{
$signature = lcfirst($method) . '(array $args = [])';
$annotation = " * @method $return $signature";
if ($versionsWithSupport !== $this->getVersions()) {
$supportedIn = implode(', ', $versionsWithSupport);
$annotation .= " (supported in versions $supportedIn)";
}
return $annotation;
}
private function getMethods()
{
if (empty($this->methods)) {
$this->methods = [];
foreach ($this->getVersions() as $version) {
$methodsInVersion = array_keys(
$this->getApiDefinition($version)['operations']
);
$api = $this->getApiDefinition($version);
$serviceId = !empty($api['metadata']['serviceId'])
? $api['metadata']['serviceId']
: null;
foreach ($methodsInVersion as $method) {
if (!empty($serviceId)
&& !empty($this->aliases['operations'][$serviceId][$version][$method])
) {
$method = $this->aliases['operations'][$serviceId][$version][$method];
}
if (empty($this->methods[$method])) {
$this->methods[$method] = [];
}
$this->methods[$method] []= $version;
}
}
}
return $this->methods;
}
private function getVersions()
{
if (empty($this->versions)) {
$this->versions = ApiProvider::defaultProvider()
->getVersions($this->getEndpoint());
// ensure that versions are always iterated from oldest to newest
sort($this->versions);
}
return $this->versions;
}
private function getApiDefinition($version = 'latest')
{
$provider = ApiProvider::defaultProvider();
return $provider('api', $this->getEndpoint(), $version);
}
private function getEndpoint()
{
if (empty($this->endpoint)) {
$service = strtolower(
preg_replace('/(MultiRegion)?Client$/', '', $this->reflection->getShortName())
);
$this->endpoint = Aws\manifest($service)['endpoint'];
}
return $this->endpoint;
}
private function getDefaultDocComment()
{
$serviceName = $this->getApiDefinition()['metadata']['serviceFullName'];
switch ($this->reflection->getParentClass()->getShortName()) {
case 'MultiRegionClient':
return <<<EODC
/**
* **{$serviceName}** multi-region client.
*
*/
EODC;
default:
return <<<EODC
/**
* **{$serviceName}** client.
*
*/
EODC;
}
}
}