-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
143 lines (114 loc) · 2.63 KB
/
index.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
<?php
declare(strict_types=1);
namespace Haku;
error_reporting(E_ALL & ~E_NOTICE);
define('HAKU_ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
define('HAKU_PHP_VERSION', '8.3.0');
if (version_compare(PHP_VERSION, HAKU_PHP_VERSION, '<')) {
echo sprintf("Haku requires PHP %s, version %s installed.", HAKU_PHP_VERSION, PHP_VERSION);
exit;
}
use Throwable;
use Haku\Exceptions\FrameworkException;
use Haku\Http\{
Status,
Headers,
Method,
Message,
Messages\Json,
Exceptions\StatusException,
};
use function Haku\{
autoloadResolver,
loadEnvironment,
loadBootstrap,
resolvePath,
config,
};
use function Haku\Generic\Url\path;
use function Haku\Delegation\delegate;
/* @willResolve Haku\Http\Message */
$__outputBuffer = null;
/* @willResolve Haku\Http\Headers */
$__outputHeaders = null;
/* @willResolve \Throwable */
$__throwable = null;
try
{
require_once implode(DIRECTORY_SEPARATOR, [
rtrim(HAKU_ROOT_PATH, DIRECTORY_SEPARATOR),
'vendor', 'Haku', 'bootstrap.php'
]);
autoloadResolver();
loadEnvironment();
loadBootstrap();
$__outputHeaders = new Headers([
'Content-Type' => 'application/json',
'Vary' => 'Origin',
'Access-Control-Max-Age' => HAKU_CORS_MAX_AGE,
'Access-Control-Allow-Origin' => HAKU_CORS_ALLOW_ORIGIN,
'Access-Control-Allow-Methods' => HAKU_CORS_ALLOW_METHODS,
'Access-Control-Allow-Headers' => HAKU_CORS_ALLOW_HEADERS,
'Access-Control-Allow-Credentials' => HAKU_CORS_ALLOW_CREDENTIALS
]);
ob_start();
[$request, $response, $headers] = delegate(path(), $__outputHeaders);
if (!$response || $response?->size() === 0)
{
throw new StatusException(500);
}
$__outputHeaders = $headers;
echo $response;
$__outputBuffer = ob_get_clean();
}
catch(StatusException $throwable)
{
$__throwable = $throwable;
$__outputHeaders->status(
Status::from($throwable->getCode())
);
$__outputBuffer = Json::from([
'code' => $throwable->getCode(),
'error' => $throwable->getMessage(),
]);
}
catch(Throwable $throwable)
{
$__throwable = $throwable;
$__outputHeaders->status(
Status::from(500)
);
$__outputBuffer = Json::from([
'code' => 500,
'error' => $throwable->getMessage(),
'trace' => $throwable->getTrace(),
]);
}
finally
{
if ($__outputHeaders instanceof Headers)
{
$__outputHeaders->send();
}
if (Method::resolve() === Method::Options)
{
exit;
}
/**
* Output errors captured outside of output buffer
*/
if ($__throwable instanceof \Throwable)
{
header('Content-Type: application/json');
echo Json::from([
'code' => $throwable->getCode(),
'error' => $__throwable->getMessage(),
'trace' => $__throwable->getTrace(),
]);
}
else
{
echo $__outputBuffer;
}
exit;
}