1
|
|
|
<?php |
2
|
|
|
namespace GuzzleHttp\Command\Guzzle; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Psr7\Uri; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Represents a Guzzle service description |
8
|
|
|
*/ |
9
|
|
|
class Description implements DescriptionInterface |
10
|
|
|
{ |
11
|
|
|
/** @var array Array of {@see OperationInterface} objects */ |
12
|
|
|
private $operations = []; |
13
|
|
|
|
14
|
|
|
/** @var array Array of API models */ |
15
|
|
|
private $models = []; |
16
|
|
|
|
17
|
|
|
/** @var string Name of the API */ |
18
|
|
|
private $name; |
19
|
|
|
|
20
|
|
|
/** @var string API version */ |
21
|
|
|
private $apiVersion; |
22
|
|
|
|
23
|
|
|
/** @var string Summary of the API */ |
24
|
|
|
private $description; |
25
|
|
|
|
26
|
|
|
/** @var array Any extra API data */ |
27
|
|
|
private $extraData = []; |
28
|
|
|
|
29
|
|
|
/** @var Uri baseUri/basePath */ |
30
|
|
|
private $baseUri; |
31
|
|
|
|
32
|
|
|
/** @var SchemaFormatter */ |
33
|
|
|
private $formatter; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param array $config Service description data |
37
|
|
|
* @param array $options Custom options to apply to the description |
38
|
|
|
* - formatter: Can provide a custom SchemaFormatter class |
39
|
|
|
* |
40
|
|
|
* @throws \InvalidArgumentException |
41
|
|
|
*/ |
42
|
14 |
|
public function __construct(array $config, array $options = []) |
43
|
|
|
{ |
44
|
|
|
// Keep a list of default keys used in service descriptions that is |
45
|
|
|
// later used to determine extra data keys. |
46
|
14 |
|
static $defaultKeys = ['name', 'models', 'apiVersion', 'description']; |
47
|
|
|
|
48
|
|
|
// Pull in the default configuration values |
49
|
14 |
|
foreach ($defaultKeys as $key) { |
50
|
14 |
|
if (isset($config[$key])) { |
51
|
7 |
|
$this->{$key} = $config[$key]; |
52
|
7 |
|
} |
53
|
14 |
|
} |
54
|
|
|
|
55
|
|
|
// Set the baseUri |
56
|
|
|
// Account for the old style of using baseUrl |
57
|
14 |
|
if (isset($config['baseUrl'])) { |
58
|
1 |
|
$config['baseUri'] = $config['baseUrl']; |
59
|
1 |
|
} |
60
|
14 |
|
$this->baseUri = isset($config['baseUri']) ? new Uri($config['baseUri']) : new Uri(); |
61
|
|
|
|
62
|
|
|
// Ensure that the models and operations properties are always arrays |
63
|
14 |
|
$this->models = (array) $this->models; |
64
|
14 |
|
$this->operations = (array) $this->operations; |
65
|
|
|
|
66
|
|
|
// We want to add operations differently than adding the other properties |
67
|
14 |
|
$defaultKeys[] = 'operations'; |
68
|
|
|
|
69
|
|
|
// Create operations for each operation |
70
|
14 |
|
if (isset($config['operations'])) { |
71
|
7 |
|
foreach ($config['operations'] as $name => $operation) { |
72
|
6 |
|
if (!is_array($operation)) { |
73
|
1 |
|
throw new \InvalidArgumentException('Operations must be arrays'); |
74
|
|
|
} |
75
|
5 |
|
$this->operations[$name] = $operation; |
76
|
6 |
|
} |
77
|
6 |
|
} |
78
|
|
|
|
79
|
|
|
// Get all of the additional properties of the service description and |
80
|
|
|
// store them in a data array |
81
|
13 |
|
foreach (array_diff(array_keys($config), $defaultKeys) as $key) { |
82
|
3 |
|
$this->extraData[$key] = $config[$key]; |
83
|
13 |
|
} |
84
|
|
|
|
85
|
|
|
// Configure the schema formatter |
86
|
13 |
|
if (isset($options['formatter'])) { |
87
|
1 |
|
$this->formatter = $options['formatter']; |
88
|
1 |
|
} else { |
89
|
12 |
|
static $defaultFormatter; |
90
|
12 |
|
if (!$defaultFormatter) { |
91
|
1 |
|
$defaultFormatter = new SchemaFormatter(); |
92
|
1 |
|
} |
93
|
12 |
|
$this->formatter = $defaultFormatter; |
94
|
|
|
} |
95
|
13 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the basePath/baseUri of the description |
99
|
|
|
* |
100
|
|
|
* @return Uri |
101
|
|
|
*/ |
102
|
2 |
|
public function getBaseUri() |
103
|
|
|
{ |
104
|
2 |
|
return $this->baseUri; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the API operations of the service |
109
|
|
|
* |
110
|
|
|
* @return Operation[] Returns an array of {@see Operation} objects |
111
|
|
|
*/ |
112
|
1 |
|
public function getOperations() |
113
|
|
|
{ |
114
|
1 |
|
return $this->operations; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Check if the service has an operation by name |
119
|
|
|
* |
120
|
|
|
* @param string $name Name of the operation to check |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
4 |
|
public function hasOperation($name) |
125
|
|
|
{ |
126
|
4 |
|
return isset($this->operations[$name]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get an API operation by name |
131
|
|
|
* |
132
|
|
|
* @param string $name Name of the command |
133
|
|
|
* |
134
|
|
|
* @return Operation |
135
|
|
|
* @throws \InvalidArgumentException if the operation is not found |
136
|
|
|
*/ |
137
|
3 |
|
public function getOperation($name) |
138
|
|
|
{ |
139
|
3 |
|
if (!$this->hasOperation($name)) { |
140
|
1 |
|
throw new \InvalidArgumentException("No operation found named $name"); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// Lazily create operations as they are retrieved |
144
|
2 |
|
if (!($this->operations[$name] instanceof Operation)) { |
145
|
2 |
|
$this->operations[$name]['name'] = $name; |
146
|
2 |
|
$this->operations[$name] = new Operation($this->operations[$name], $this); |
147
|
2 |
|
} |
148
|
|
|
|
149
|
2 |
|
return $this->operations[$name]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get a shared definition structure. |
154
|
|
|
* |
155
|
|
|
* @param string $id ID/name of the model to retrieve |
156
|
|
|
* |
157
|
|
|
* @return Parameter |
158
|
|
|
* @throws \InvalidArgumentException if the model is not found |
159
|
|
|
*/ |
160
|
3 |
|
public function getModel($id) |
161
|
|
|
{ |
162
|
3 |
|
if (!$this->hasModel($id)) { |
163
|
1 |
|
throw new \InvalidArgumentException("No model found named $id"); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// Lazily create models as they are retrieved |
167
|
2 |
|
if (!($this->models[$id] instanceof Parameter)) { |
168
|
2 |
|
$this->models[$id] = new Parameter( |
169
|
2 |
|
$this->models[$id], |
170
|
2 |
|
['description' => $this] |
171
|
2 |
|
); |
172
|
2 |
|
} |
173
|
|
|
|
174
|
2 |
|
return $this->models[$id]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get all models of the service description. |
179
|
|
|
* |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
1 |
|
public function getModels() |
183
|
|
|
{ |
184
|
1 |
|
$models = []; |
185
|
1 |
|
foreach ($this->models as $name => $model) { |
186
|
1 |
|
$models[$name] = $this->getModel($name); |
187
|
1 |
|
} |
188
|
|
|
|
189
|
1 |
|
return $models; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Check if the service description has a model by name. |
194
|
|
|
* |
195
|
|
|
* @param string $id Name/ID of the model to check |
196
|
|
|
* |
197
|
|
|
* @return bool |
198
|
|
|
*/ |
199
|
3 |
|
public function hasModel($id) |
200
|
|
|
{ |
201
|
3 |
|
return isset($this->models[$id]); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get the API version of the service |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
1 |
|
public function getApiVersion() |
210
|
|
|
{ |
211
|
1 |
|
return $this->apiVersion; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get the name of the API |
216
|
|
|
* |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
1 |
|
public function getName() |
220
|
|
|
{ |
221
|
1 |
|
return $this->name; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get a summary of the purpose of the API |
226
|
|
|
* |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
1 |
|
public function getDescription() |
230
|
|
|
{ |
231
|
1 |
|
return $this->description; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Format a parameter using named formats. |
236
|
|
|
* |
237
|
|
|
* @param string $format Format to convert it to |
238
|
|
|
* @param mixed $input Input string |
239
|
|
|
* |
240
|
|
|
* @return mixed |
241
|
|
|
*/ |
242
|
2 |
|
public function format($format, $input) |
243
|
|
|
{ |
244
|
2 |
|
return $this->formatter->format($format, $input); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Get arbitrary data from the service description that is not part of the |
249
|
|
|
* Guzzle service description specification. |
250
|
|
|
* |
251
|
|
|
* @param string $key Data key to retrieve or null to retrieve all extra |
252
|
|
|
* |
253
|
|
|
* @return null|mixed |
254
|
|
|
*/ |
255
|
1 |
|
public function getData($key = null) |
256
|
|
|
{ |
257
|
1 |
|
if ($key === null) { |
258
|
1 |
|
return $this->extraData; |
259
|
1 |
|
} elseif (isset($this->extraData[$key])) { |
260
|
1 |
|
return $this->extraData[$key]; |
261
|
|
|
} else { |
262
|
1 |
|
return null; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|