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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
<?php
namespace dokuwiki\Remote;
class ApiCall
{
/** @var callable The method to be called for this endpoint */
protected $method;
/** @var bool Whether this call can be called without authentication */
protected bool $isPublic = false;
/** @var array Metadata on the accepted parameters */
protected array $args = [];
/** @var array Metadata on the return value */
protected array $return = [
'type' => 'string',
'description' => '',
];
/** @var string The summary of the method */
protected string $summary = '';
/** @var string The description of the method */
protected string $description = '';
/**
* Make the given method available as an API call
*
* @param string|array $method Either [object,'method'] or 'function'
* @throws \ReflectionException
*/
public function __construct($method)
{
if (!is_callable($method)) {
throw new \InvalidArgumentException('Method is not callable');
}
$this->method = $method;
$this->parseData();
}
/**
* Call the method
*
* Important: access/authentication checks need to be done before calling this!
*
* @param array $args
* @return mixed
*/
public function __invoke($args)
{
if (!array_is_list($args)) {
$args = $this->namedArgsToPositional($args);
}
return call_user_func_array($this->method, $args);
}
/**
* @return bool
*/
public function isPublic(): bool
{
return $this->isPublic;
}
/**
* @param bool $isPublic
* @return $this
*/
public function setPublic(bool $isPublic = true): self
{
$this->isPublic = $isPublic;
return $this;
}
/**
* @return array
*/
public function getArgs(): array
{
return $this->args;
}
/**
* Limit the arguments to the given ones
*
* @param string[] $args
* @return $this
*/
public function limitArgs($args): self
{
foreach ($args as $arg) {
if (!isset($this->args[$arg])) {
throw new \InvalidArgumentException("Unknown argument $arg");
}
}
$this->args = array_intersect_key($this->args, array_flip($args));
return $this;
}
/**
* Set the description for an argument
*
* @param string $arg
* @param string $description
* @return $this
*/
public function setArgDescription(string $arg, string $description): self
{
if (!isset($this->args[$arg])) {
throw new \InvalidArgumentException('Unknown argument');
}
$this->args[$arg]['description'] = $description;
return $this;
}
/**
* @return array
*/
public function getReturn(): array
{
return $this->return;
}
/**
* Set the description for the return value
*
* @param string $description
* @return $this
*/
public function setReturnDescription(string $description): self
{
$this->return['description'] = $description;
return $this;
}
/**
* @return string
*/
public function getSummary(): string
{
return $this->summary;
}
/**
* @param string $summary
* @return $this
*/
public function setSummary(string $summary): self
{
$this->summary = $summary;
return $this;
}
/**
* @return string
*/
public function getDescription(): string
{
return $this->description;
}
/**
* @param string $description
* @return $this
*/
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
/**
* Fill in the metadata
*
* This uses Reflection to inspect the method signature and doc block
*
* @throws \ReflectionException
*/
protected function parseData()
{
if (is_array($this->method)) {
$reflect = new \ReflectionMethod($this->method[0], $this->method[1]);
} else {
$reflect = new \ReflectionFunction($this->method);
}
$docInfo = $this->parseDocBlock($reflect->getDocComment());
$this->summary = $docInfo['summary'];
$this->description = $docInfo['description'];
foreach ($reflect->getParameters() as $parameter) {
$name = $parameter->name;
$realType = $parameter->getType();
if ($realType) {
$type = $realType->getName();
} elseif (isset($docInfo['args'][$name]['type'])) {
$type = $docInfo['args'][$name]['type'];
} else {
$type = 'string';
}
if (isset($docInfo['args'][$name]['description'])) {
$description = $docInfo['args'][$name]['description'];
} else {
$description = '';
}
$this->args[$name] = [
'type' => $type,
'description' => trim($description),
];
}
$returnType = $reflect->getReturnType();
if ($returnType) {
$this->return['type'] = $returnType->getName();
} elseif (isset($docInfo['return']['type'])) {
$this->return['type'] = $docInfo['return']['type'];
} else {
$this->return['type'] = 'string';
}
if (isset($docInfo['return']['description'])) {
$this->return['description'] = $docInfo['return']['description'];
}
}
/**
* Parse a doc block
*
* @param string $doc
* @return array
*/
protected function parseDocBlock($doc)
{
// strip asterisks and leading spaces
$doc = preg_replace(
['/^[ \t]*\/\*+[ \t]*/m', '/[ \t]*\*+[ \t]*/m', '/\*+\/\s*$/m', '/\s*\/\s*$/m'],
['', '', '', ''],
$doc
);
$doc = trim($doc);
// get all tags
$tags = [];
if (preg_match_all('/^@(\w+)\s+(.*)$/m', $doc, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$tags[$match[1]][] = trim($match[2]);
}
}
$params = $this->extractDocTags($tags);
// strip the tags from the doc
$doc = preg_replace('/^@(\w+)\s+(.*)$/m', '', $doc);
[$summary, $description] = sexplode("\n\n", $doc, 2, '');
return array_merge(
[
'summary' => trim($summary),
'description' => trim($description),
'tags' => $tags,
],
$params
);
}
/**
* Process the param and return tags
*
* @param array $tags
* @return array
*/
protected function extractDocTags(&$tags)
{
$result = [];
if (isset($tags['param'])) {
foreach ($tags['param'] as $param) {
[$type, $name, $description] = array_map('trim', sexplode(' ', $param, 3, ''));
if ($name[0] !== '$') continue;
$name = substr($name, 1);
$result['args'][$name] = [
'type' => $this->cleanTypeHint($type),
'description' => $description,
];
}
unset($tags['param']);
}
if (isset($tags['return'])) {
$return = $tags['return'][0];
[$type, $description] = array_map('trim', sexplode(' ', $return, 2, ''));
$result['return'] = [
'type' => $this->cleanTypeHint($type),
'description' => $description
];
unset($tags['return']);
}
return $result;
}
/**
* Matches the given type hint against the valid options for the remote API
*
* @param string $hint
* @return string
*/
protected function cleanTypeHint($hint)
{
$types = explode('|', $hint);
foreach ($types as $t) {
if (str_ends_with($t, '[]')) {
return 'array';
}
if ($t === 'boolean' || $t === 'true' || $t === 'false') {
return 'bool';
}
if (in_array($t, ['array', 'string', 'int', 'double', 'bool', 'null', 'date', 'file'])) {
return $t;
}
}
return 'string';
}
/**
* Converts named arguments to positional arguments
*
* @fixme with PHP 8 we can use named arguments directly using the spread operator
* @param array $params
* @return array
*/
protected function namedArgsToPositional($params)
{
$args = [];
foreach (array_keys($this->args) as $arg) {
if (isset($params[$arg])) {
$args[] = $params[$arg];
} else {
$args[] = null;
}
}
return $args;
}
}
|