-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathDispatcher.php
More file actions
205 lines (174 loc) · 7.97 KB
/
Dispatcher.php
File metadata and controls
205 lines (174 loc) · 7.97 KB
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
<?php
namespace Utopia\Http;
/**
* Per-request request dispatcher.
*
* Owns the mutable state for a single request/response cycle so the
* {@see Http} singleton carries only bootstrap configuration. This keeps
* the library safe under concurrent request handling (Swoole coroutines).
*
* One Dispatcher instance is constructed per request by {@see Http::run()}.
* @internal
*/
final class Dispatcher
{
private ?RouteMatch $match = null;
public function __construct(
private readonly Http $http,
private readonly Request $request,
private readonly Response $response,
) {}
public function matchedRoute(): ?Route
{
return $this->match?->route;
}
public function matchedRouteMatch(): ?RouteMatch
{
return $this->match;
}
public function handle(): void
{
if ($this->http->isCompressionEnabled()) {
$this->response->setAcceptEncoding($this->request->getHeader('accept-encoding', ''));
$this->response->setCompressionMinSize($this->http->getCompressionMinSize());
$this->response->setCompressionSupported($this->http->getCompressionSupported());
}
$this->http->setRequestResource('request', fn() => $this->request);
$this->http->setRequestResource('response', fn() => $this->response);
try {
foreach (Hooks::$request as $hook) {
$arguments = $this->http->getArguments($hook, [], []);
\call_user_func_array($hook->getAction(), $arguments);
}
} catch (\Exception $e) {
$this->http->setRequestResource('error', fn() => $e);
foreach (Hooks::$errors as $error) {
if (\in_array('*', $error->getGroups())) {
try {
$arguments = $this->http->getArguments($error, [], []);
\call_user_func_array($error->getAction(), $arguments);
} catch (\Throwable $e) {
throw new Exception('Error handler had an error: ' . $e->getMessage(), 500, $e);
}
}
}
}
if ($this->http->isFileLoaded($this->request->getURI())) {
$time = (60 * 60 * 24 * 365 * 2);
$this->response
->setContentType($this->http->getFileMimeType($this->request->getURI()))
->addHeader('Cache-Control', 'public, max-age=' . $time)
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + $time) . ' GMT')
->send($this->http->getFileContents($this->request->getURI()));
return;
}
$method = $this->request->getMethod();
$this->match = Router::matchRequest($this->request);
$this->http->setRequestResource('route', fn() => $this->match?->route);
$this->http->setRequestResource('routeMatch', fn() => $this->match);
$groups = $this->match?->route->getGroups() ?? [];
if (Http::REQUEST_METHOD_HEAD === $method) {
$method = Http::REQUEST_METHOD_GET;
$this->response->disablePayload();
}
if (Http::REQUEST_METHOD_OPTIONS === $method) {
try {
foreach ($groups as $group) {
foreach (Hooks::$options as $option) {
if (\in_array($group, $option->getGroups())) {
\call_user_func_array($option->getAction(), $this->http->getArguments($option, [], $this->request->getParams()));
}
}
}
foreach (Hooks::$options as $option) {
if (\in_array('*', $option->getGroups())) {
\call_user_func_array($option->getAction(), $this->http->getArguments($option, [], $this->request->getParams()));
}
}
} catch (\Throwable $e) {
foreach (Hooks::$errors as $error) {
if (\in_array('*', $error->getGroups())) {
$this->http->setRequestResource('error', fn() => $e);
\call_user_func_array($error->getAction(), $this->http->getArguments($error, [], $this->request->getParams()));
}
}
}
return;
}
if ($this->match !== null) {
$this->execute($this->match);
return;
}
foreach (Hooks::$errors as $error) {
if (\in_array('*', $error->getGroups())) {
$this->http->setRequestResource('error', fn() => new Exception('Not Found', 404));
\call_user_func_array($error->getAction(), $this->http->getArguments($error, [], $this->request->getParams()));
}
}
}
public function execute(RouteMatch $match): void
{
$route = $match->route;
$groups = $route->getGroups();
$pathValues = $route->getPathValues($this->request, $match->preparedPath);
// Request params are re-read at each call site: init/request hooks
// may mutate the request (e.g. applying filters), and later hooks +
// the route action must see the updated view. Hoisting this into a
// local would cache stale params across the lifecycle.
try {
if ($route->getHook()) {
foreach (Hooks::$init as $hook) {
if (\in_array('*', $hook->getGroups())) {
\call_user_func_array($hook->getAction(), $this->http->getArguments($hook, $pathValues, $this->request->getParams()));
}
}
}
foreach ($groups as $group) {
foreach (Hooks::$init as $hook) {
if (\in_array($group, $hook->getGroups())) {
\call_user_func_array($hook->getAction(), $this->http->getArguments($hook, $pathValues, $this->request->getParams()));
}
}
}
if (!$this->response->isSent()) {
\call_user_func_array($route->getAction(), $this->http->getArguments($route, $pathValues, $this->request->getParams()));
}
foreach ($groups as $group) {
foreach (Hooks::$shutdown as $hook) {
if (\in_array($group, $hook->getGroups())) {
\call_user_func_array($hook->getAction(), $this->http->getArguments($hook, $pathValues, $this->request->getParams()));
}
}
}
if ($route->getHook()) {
foreach (Hooks::$shutdown as $hook) {
if (\in_array('*', $hook->getGroups())) {
\call_user_func_array($hook->getAction(), $this->http->getArguments($hook, $pathValues, $this->request->getParams()));
}
}
}
} catch (\Throwable $e) {
$this->http->setRequestResource('error', fn() => $e);
foreach ($groups as $group) {
foreach (Hooks::$errors as $error) {
if (\in_array($group, $error->getGroups())) {
try {
\call_user_func_array($error->getAction(), $this->http->getArguments($error, $pathValues, $this->request->getParams()));
} catch (\Throwable $e) {
throw new Exception('Error handler had an error: ' . $e->getMessage(), 500, $e);
}
}
}
}
foreach (Hooks::$errors as $error) {
if (\in_array('*', $error->getGroups())) {
try {
\call_user_func_array($error->getAction(), $this->http->getArguments($error, $pathValues, $this->request->getParams()));
} catch (\Throwable $e) {
throw new Exception('Error handler had an error: ' . $e->getMessage(), 500, $e);
}
}
}
}
}
}