-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWireframeAPI.module.php
440 lines (388 loc) · 13.4 KB
/
WireframeAPI.module.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
namespace ProcessWire;
/**
* Wireframe API
*
* This module provides a JSON API for accessing Wireframe's features. For more details check out the documentation at
* https://wireframe-framework.com/docs/wireframe-api/.
*
* @method WireframeAPI init(?string $path = null, array $args = []) Init API
*
* @version 0.2.1
* @author Teppo Koivula <[email protected]>
* @license Mozilla Public License v2.0 https://mozilla.org/MPL/2.0/
*/
class WireframeAPI extends WireData implements Module, ConfigurableModule {
/**
* Available endpoints
*
* Note: default endpoints are populated in __construct().
*
* @var array
*/
protected $available_endpoints;
/**
* Enabled endpoints
*
* @var array
*/
protected $enabled_endpoints = [];
/**
* API response
*
* @var \Wireframe\APIResponse|null
*/
protected $response = null;
/**
* API root path
*
* This value is used by the Wireframe API Hooks module to provide automatic API endpoint.
* Note that this endpoint is only intended for Tracy Debugger Wireframe panel and requires
* superuser access.
*
* @var string|null
*/
protected $api_root = null;
/**
* Constructor
*
* @throws WireException if config settings contain unrecognized properties.
*/
public function __construct() {
// populate the default endpoints
$this->available_endpoints = [
'components' => 'components',
'pages' => 'pages',
'partials' => 'partials',
];
// populate the default config data
$config = array_merge(
$this->getConfigDefaults(),
\is_array($this->config->wireframeAPI) ? $this->config->wireframeAPI : []
);
foreach ($config as $key => $value) {
switch ($key) {
case 'enabled_endpoints':
$this->setEnabledEndpoints($value);
break;
case 'api_root':
$this->api_root = $value;
break;
default:
throw new WireException(sprintf(
'Unable to set value for unrecognized property "%s"',
$key
));
}
}
}
/**
* Get default config settings
*
* If you need to customize or override any of the default config values, you can copy this array to your site
* config file (/site/config.php) as $config->wireframeAPI.
*
* @return array Default config settings.
*/
public function getConfigDefaults(): array {
return [
'enabled_endpoints' => [],
'api_root' => '',
];
}
/**
* Set config data
*
* @param array $data
*/
public function setConfigData(array $data) {
foreach ($data as $key => $value) {
switch ($key) {
case 'enabled_endpoints':
$this->setEnabledEndpoints($data['enabled_endpoints']);
break;
case 'api_root':
$this->api_root = $value === '' ? '' : '/' . trim($data['api_root'], '/') . '/';
break;
default:
$this->$key = $value;
}
}
}
/**
* Module configuration
*
* @param array $data
* @return InputfieldWrapper
*/
public function getModuleConfigInputfields(array $data): InputfieldWrapper {
$fields = $this->wire(new InputfieldWrapper());
// Merge data array with defaults.
$data = array_merge($this->getConfigDefaults(), $data);
// Configuration settings from site config
$config = $this->config->wireframeAPI ?? [];
// Enabled API endpoints
/** @var InputfieldCheckboxes */
$field = $this->modules->get('InputfieldCheckboxes');
$field->name = 'enabled_endpoints';
$field->label = $this->_('Enabled endpoints');
$field->notes = $this->_('You can find more information about [available API endpoints](https://wireframe-framework.com/docs/wireframe-api/available-api-endpoints/) and [enabling API endpoints via site config](https://wireframe-framework.com/docs/wireframe-api/configuration-settings/) from the Wireframe API docs.');
$field->addOptions([
'components' => $this->_('Components'),
'pages' => $this->_('Pages'),
'partials' => $this->_('Partials'),
]);
$field->value = $data[$field->name];
if (isset($config[$field->name])) {
$field->notes = $this->_('Enabled endpoints are currently defined in site config. You cannot override site config settings here.');
$field->value = $config[$field->name];
$field->collapsed = Inputfield::collapsedNoLocked;
}
$fields->add($field);
// API root
/** @var InputfieldText */
$field = $this->modules->get('InputfieldText');
$field->name = 'api_root';
$field->label = $this->_('API root path');
$field->description = $this->_('Define the base path for the API.');
$field->notes = $this->_('Accessing the path defined here requires the Wireframe Hooks module and is currently only available for superusers.')
. ' *' . $this->_('This setting is primarily intended for the API debugger found from the Wireframe Tracy panel.') . '*';
$field->value = $data[$field->name];
if (isset($config[$field->name])) {
$field->notes = $this->_('API root path is defined in site config. You cannot override site config settings here.');
$field->value = $config[$field->name];
$field->collapsed = Inputfield::collapsedNoLocked;
}
$fields->add($field);
return $fields;
}
/**
* Init API
*
* @param string|null $path API path; leave null to use current URL.
* @param array $args Optional array of arguments for the endpoint.
* @return WireframeAPI Self-reference.
*/
public function ___init(?string $path = null, array $args = []): WireframeAPI {
// define path
if ($path === null) {
$path = trim($this->input->url, '/');
}
// make sure that Wireframe is initialized
if (!Wireframe::isInitialized()) {
$this->modules->get('Wireframe')->init($this->page === null ? [
'page' => $this->pages->get($this->config->http404PageID),
] : []);
}
// instantiate a response object
$this->response = (new \Wireframe\APIResponse())
->setPath($path);
// if debug mode is enabled, improve response readability by enabling JSON pretty print
if ($this->config->debug) {
$this->response->setPretty(true);
}
// split path into parts and remove API root path if present
if (!empty($path)) {
$path = explode('/', trim($path, '/'));
if ($this->page !== null && !empty($path) && $path[0] === $this->page->name) {
array_shift($path);
}
}
// bail out early if path is empty
if (empty($path)) {
$this->response->setData([
'endpoints' => $this->enabled_endpoints,
]);
return $this;
}
try {
// validate endpoint and remove it from path
$endpoint = $this->validateEndpoint($path);
array_shift($path);
// prepare arguments and store them in the response object
$this->response->setArgs($this->prepareArgs($endpoint, $path, $args));
// check access
if (!$this->checkAccess($endpoint, $path, $this->response->getArgs())) {
throw (new \Wireframe\APIException('Unauthorized'))
->setResponseCode(401);
}
// call endpoint method
$data = [];
$method = $this->available_endpoints[$endpoint];
if (\is_string($method) && strpos($method, '::') === false) {
$data = (new \Wireframe\APIEndpoints())->$method($path, $this->response->getArgs());
} else {
$data = \call_user_func($method, $path, $this->response->getArgs());
}
$this->response->setData(\is_array($data) ? $data : [$data]);
} catch (\Exception $e) {
// handle exception
$this->response
->setMessage($e->getMessage())
->setStatusCode($e instanceof \Wireframe\APIException ? $e->getResponseCode() : 500);
}
return $this;
}
/**
* Validate endpoint
*
* @param array $path
* @return string
*
* @throws \Wireframe\APIException if no API endpoint was specified (HTTP 400).
* @throws \Wireframe\APIException if API endpoint is unknown/unavailable (HTTP 404).
*/
protected function validateEndpoint(array $path): string {
if (empty($path)) {
throw (new \Wireframe\APIException('Missing API endpoint'))->setResponseCode(400);
}
$endpoint = $path[0];
$endpoints = array_intersect(array_keys($this->available_endpoints), $this->enabled_endpoints);
if (!\in_array($endpoint, $endpoints)) {
throw (new \Wireframe\APIException(sprintf(
'Unknown API endpoint (%s)',
$endpoint
)))->setResponseCode(404);
}
return $endpoint;
}
/**
* Get API response
*
* @return \Wireframe\APIResponse|null
*/
public function getResponse(): ?\Wireframe\APIResponse {
return $this->response;
}
/**
* Send headers
*
* @return WireframeAPI Self-reference.
*/
public function sendHeaders(): WireframeAPI {
header('Content-Type: application/json');
if ($this->response) {
http_response_code($this->response->getStatusCode());
}
return $this;
}
/**
* Render API response
*
* @return string
*/
public function render(): string {
if ($this->response) {
return $this->response->render();
}
return '';
}
/**
* Set enabled endpoints
*
* @param array $endpoints
* @return WireframeAPI Self-reference.
*/
public function setEnabledEndpoints(array $endpoints): WireframeAPI {
$this->enabled_endpoints = array_intersect(array_keys($this->available_endpoints), $endpoints);
return $this;
}
/**
* Get enabled endpoints
*
* @return array
*/
public function getEnabledEndpoints(): array {
return $this->enabled_endpoints;
}
/**
* Get Debugger API root path
*
* @return null|string
*/
public function getAPIRoot(): ?string {
return $this->api_root;
}
/**
* Enable single endpoint
*
* @param string $endpoint
* @return WireframeAPI Self-reference.
*/
public function enableEndpoint(string $endpoint): WireframeAPI {
if (array_key_exists($endpoint, $this->available_endpoints) && !\in_array($endpoint, $this->enabled_endpoints)) {
$this->enabled_endpoints[] = $endpoint;
}
return $this;
}
/**
* Disable single endpoint
*
* @param string $endpoint
* @return WireframeAPI Self-reference.
*/
public function disableEndpoint(string $endpoint): WireframeAPI {
$key = array_search($endpoint, $this->enabled_endpoints);
if ($key !== false) {
unset($this->enabled_endpoints[$key]);
}
return $this;
}
/**
* Add and enable a custom endpoint
*
* @param string $endpoint
* @param callable $callable
* @return WireframeAPI Self-reference.
*
* @throws WireException if specified endpoint already exists.
*/
public function addEndpoint(string $endpoint, callable $callable): WireframeAPI {
if (array_key_exists($endpoint, $this->available_endpoints)) {
throw new WireException(sprintf(
'Unable to add endpoint: an endpoint with this name already exists (%s)',
$endpoint
));
}
$this->available_endpoints[$endpoint] = $callable;
$this->enableEndpoint($endpoint);
return $this;
}
/**
* Disable and remove an endpoint
*
* @param string $endpoint Endpoint name
* @return WireframeAPI Self-reference.
*/
public function removeEndpoint(string $endpoint): WireframeAPI {
$this->disableEndpoint($endpoint);
unset($this->available_endpoints[$endpoint]);
return $this;
}
/**
* Check access to API
*
* You can hook into this method and provide your own access management logic.
*
* @param string $endpoint
* @param array $path
* @param array $args
* @return bool
*/
protected function ___checkAccess(string $endpoint, array $path, array $args = []): bool {
return true;
}
/**
* Prepare arguments
*
* You can hook into this method and provide your own argument handling logic.
*
* @param string $endpoint
* @param array $path
* @param array $args
* @return array
*/
protected function ___prepareArgs(string $endpoint, array $path, array $args = []): array {
return $args;
}
}