-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Api.php
executable file
·105 lines (90 loc) · 2.9 KB
/
Api.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
<?php
namespace JohannSchopplich\Headless\Api;
use Kirby\Cms\App;
use Kirby\Cms\File;
use Kirby\Exception\Exception;
use Kirby\Http\Response;
use Kirby\Toolkit\A;
class Api
{
/**
* Create an API handler
*/
public static function createHandler(callable ...$fns)
{
$context = [
'kirby' => App::instance()
];
return function (...$args) use ($fns, $context) {
foreach ($fns as $fn) {
$result = $fn($context, $args);
if ($result instanceof Response || $result instanceof File) {
return $result;
}
if (is_array($result)) {
$context = A::merge($context, $result);
}
}
};
}
/**
* Create an API response
*
* @remarks
* Enforces consistent JSON responses by wrapping Kirby's `Response` class
*/
public static function createResponse(int $code, $data = null): Response
{
$kirby = App::instance();
$body = [
'code' => $code,
'status' => static::getStatusMessage($code)
];
if ($data !== null) {
$body['result'] = $data;
}
return Response::json($body, $code, null, [
'Access-Control-Allow-Origin' => $kirby->option('headless.cors.allowOrigin', '*')
]);
}
/**
* Get the status message for the given code
*
* @throws \Kirby\Exception\Exception
*/
private static function getStatusMessage(int $code): string
{
$messages = [
200 => 'OK',
201 => 'Created',
204 => 'No Content',
400 => 'Bad Request',
401 => 'Unauthorized',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
409 => 'Conflict',
422 => 'Unprocessable Entity',
500 => 'Internal Server Error'
];
if (!isset($messages[$code])) {
throw new Exception('Unknown status code: ' . $code);
}
return $messages[$code];
}
/**
* Respond to CORS preflight requests
*/
public static function createPreflightResponse(): Response
{
$kirby = App::instance();
// 204 responses **must not** have a `Content-Length` header
// See: https://www.rfc-editor.org/rfc/rfc7230#section-3.3.2
return new Response('', null, 204, [
'Access-Control-Allow-Origin' => $kirby->option('headless.cors.allowOrigin', '*'),
'Access-Control-Allow-Methods' => $kirby->option('headless.cors.allowMethods', 'GET, POST, OPTIONS'),
'Access-Control-Allow-Headers' => $kirby->option('headless.cors.allowHeaders', 'Accept, Content-Type, Authorization, X-Language'),
'Access-Control-Max-Age' => $kirby->option('headless.cors.maxAge', '86400'),
]);
}
}