-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
App.php
354 lines (315 loc) · 12.7 KB
/
App.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
<?php
namespace FrameworkX;
use FrameworkX\Io\MiddlewareHandler;
use FrameworkX\Io\ReactiveHandler;
use FrameworkX\Io\RedirectHandler;
use FrameworkX\Io\RouteHandler;
use FrameworkX\Io\SapiHandler;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Message\Response;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
use function React\Async\await;
class App
{
/** @var MiddlewareHandler */
private $handler;
/** @var RouteHandler */
private $router;
/** @var ReactiveHandler|SapiHandler */
private $sapi;
/**
* Instantiate new X application
*
* ```php
* // instantiate
* $app = new App();
*
* // instantiate with global middleware
* $app = new App($middleware);
* $app = new App($middleware1, $middleware2);
* ```
*
* @param callable|class-string ...$middleware
*/
public function __construct(...$middleware)
{
// new MiddlewareHandler([$fiberHandler, $accessLogHandler, $errorHandler, ...$middleware, $routeHandler])
$handlers = [];
$container = $needsErrorHandler = new Container();
// only log for built-in webserver and PHP development webserver by default, others have their own access log
$needsAccessLog = (\PHP_SAPI === 'cli' || \PHP_SAPI === 'cli-server') ? $container : null;
if ($middleware) {
$needsErrorHandlerNext = false;
foreach ($middleware as $handler) {
// load AccessLogHandler and ErrorHandler instance from last Container
if ($handler === AccessLogHandler::class) {
$handler = $container->getAccessLogHandler();
} elseif ($handler === ErrorHandler::class) {
$handler = $container->getErrorHandler();
}
// ensure AccessLogHandler is always followed by ErrorHandler
if ($needsErrorHandlerNext && !$handler instanceof ErrorHandler) {
break;
}
$needsErrorHandlerNext = false;
if ($handler instanceof Container) {
// remember last Container to load any following class names
$container = $handler;
// add default ErrorHandler from last Container before adding any other handlers, may be followed by other Container instances (unlikely)
if (!$handlers) {
$needsErrorHandler = $needsAccessLog = $container;
}
} elseif (!\is_callable($handler)) {
$handlers[] = $container->callable($handler);
} else {
// don't need a default ErrorHandler if we're adding one as first handler or AccessLogHandler as first followed by one
if ($needsErrorHandler && ($handler instanceof ErrorHandler || $handler instanceof AccessLogHandler) && !$handlers) {
$needsErrorHandler = null;
}
// only add to list of handlers if this is not a NOOP
if (!$handler instanceof AccessLogHandler || !$handler->isDevNull()) {
$handlers[] = $handler;
}
if ($handler instanceof AccessLogHandler) {
$needsAccessLog = null;
$needsErrorHandlerNext = true;
}
}
}
if ($needsErrorHandlerNext) {
throw new \TypeError('AccessLogHandler must be followed by ErrorHandler');
}
}
// add default ErrorHandler as first handler unless it is already added explicitly
if ($needsErrorHandler instanceof Container) {
\array_unshift($handlers, $needsErrorHandler->getErrorHandler());
}
// only log for built-in webserver and PHP development webserver by default, others have their own access log
if ($needsAccessLog instanceof Container) {
$handler = $needsAccessLog->getAccessLogHandler();
if (!$handler->isDevNull()) {
\array_unshift($handlers, $handler);
}
}
$this->router = new RouteHandler($container);
$handlers[] = $this->router;
$this->handler = new MiddlewareHandler($handlers);
$this->sapi = \PHP_SAPI === 'cli' ? new ReactiveHandler($container->getEnv('X_LISTEN')) : new SapiHandler();
}
/**
* @param string $route
* @param callable|class-string $handler
* @param callable|class-string ...$handlers
*/
public function get(string $route, $handler, ...$handlers): void
{
$this->map(['GET'], $route, $handler, ...$handlers);
}
/**
* @param string $route
* @param callable|class-string $handler
* @param callable|class-string ...$handlers
*/
public function head(string $route, $handler, ...$handlers): void
{
$this->map(['HEAD'], $route, $handler, ...$handlers);
}
/**
* @param string $route
* @param callable|class-string $handler
* @param callable|class-string ...$handlers
*/
public function post(string $route, $handler, ...$handlers): void
{
$this->map(['POST'], $route, $handler, ...$handlers);
}
/**
* @param string $route
* @param callable|class-string $handler
* @param callable|class-string ...$handlers
*/
public function put(string $route, $handler, ...$handlers): void
{
$this->map(['PUT'], $route, $handler, ...$handlers);
}
/**
* @param string $route
* @param callable|class-string $handler
* @param callable|class-string ...$handlers
*/
public function patch(string $route, $handler, ...$handlers): void
{
$this->map(['PATCH'], $route, $handler, ...$handlers);
}
/**
* @param string $route
* @param callable|class-string $handler
* @param callable|class-string ...$handlers
*/
public function delete(string $route, $handler, ...$handlers): void
{
$this->map(['DELETE'], $route, $handler, ...$handlers);
}
/**
* @param string $route
* @param callable|class-string $handler
* @param callable|class-string ...$handlers
*/
public function options(string $route, $handler, ...$handlers): void
{
// backward compatibility: `OPTIONS * HTTP/1.1` can be matched with empty path (legacy)
if ($route === '') {
$route = '*';
}
$this->map(['OPTIONS'], $route, $handler, ...$handlers);
}
/**
* @param string $route
* @param callable|class-string $handler
* @param callable|class-string ...$handlers
*/
public function any(string $route, $handler, ...$handlers): void
{
$this->map(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], $route, $handler, ...$handlers);
}
/**
*
* @param string[] $methods
* @param string $route
* @param callable|class-string $handler
* @param callable|class-string ...$handlers
*/
public function map(array $methods, string $route, $handler, ...$handlers): void
{
$this->router->map($methods, $route, $handler, ...$handlers);
}
/**
* @param string $route
* @param string $target
* @param int $code
*/
public function redirect(string $route, string $target, int $code = Response::STATUS_FOUND): void
{
$this->any($route, new RedirectHandler($target, $code));
}
/**
* Runs the app to handle HTTP requests according to any registered routes and middleware.
*
* This is where the magic happens: When executed on the command line (CLI),
* this will run the powerful reactive request handler built on top of
* ReactPHP. This works by running the efficient built-in HTTP web server to
* handle incoming HTTP requests through ReactPHP's HTTP and socket server.
* This async execution mode is usually recommended as it can efficiently
* process a large number of concurrent connections and process multiple
* incoming requests simultaneously. The long-running server process will
* continue to run until it is interrupted by a signal.
*
* When executed behind traditional PHP SAPIs (PHP-FPM, FastCGI, Apache, etc.),
* this will handle a single request and run until a single response is sent.
* This is particularly useful because it allows you to run the exact same
* app in any environment.
*
* @see ReactiveHandler::run()
* @see SapiHandler::run()
*/
public function run(): void
{
$this->sapi->run(\Closure::fromCallable([$this, 'handleRequest']));
}
/**
* Invokes the app to handle a single HTTP request according to any registered routes and middleware.
*
* This method allows you to pass in a single HTTP request object that will
* be processed according to any registered routes and middleware and will
* return an HTTP response object as a result.
*
* ```php
* $app = new FrameworkX\App();
* $app->get('/', fn() => React\Http\Message\Response::plaintext("Hello!\n"));
*
* $request = new React\Http\Message\ServerRequest('GET', 'https://example.com/');
* $response = $app($request);
*
* assert($response instanceof Psr\Http\Message\ResponseInterface);
* assert($response->getStatusCode() === 200);
* assert($response->getBody()->getContents() === "Hello\n");
* ```
*
* This is particularly useful for higher-level integration test suites and
* for custom integrations with other runtime environments like serverless
* functions or other frameworks. Otherwise, most applications would likely
* want to use the `run()` method to run the application and automatically
* accept incoming HTTP requests according to the PHP SAPI in use.
*
* @param ServerRequestInterface $request The HTTP request object to process.
* @return ResponseInterface This method returns an HTTP response object
* according to any registered routes and middleware. If any handler is
* async, it will await its execution before returning, running the
* event loop as needed. If the request can not be routed or any handler
* fails, it will return a matching HTTP error response object.
* @throws void This method never throws. If the request can not be routed
* or any handler fails, it will be turned into a valid error response
* before returning.
* @see self::run()
*/
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
$response = $this->handleRequest($request);
if ($response instanceof PromiseInterface) {
/** @throws void */
$response = await($response);
assert($response instanceof ResponseInterface);
}
return $response;
}
/**
* @param ServerRequestInterface $request
* @return ResponseInterface|PromiseInterface<ResponseInterface>
* Returns a response or a Promise which eventually fulfills with a
* response. This method never throws or resolves a rejected promise.
* If the request can not be routed or the handler fails, it will be
* turned into a valid error response before returning.
* @throws void
*/
private function handleRequest(ServerRequestInterface $request)
{
$response = ($this->handler)($request);
assert($response instanceof ResponseInterface || $response instanceof PromiseInterface || $response instanceof \Generator);
if ($response instanceof \Generator) {
if ($response->valid()) {
$response = $this->coroutine($response);
} else {
$response = $response->getReturn();
assert($response instanceof ResponseInterface);
}
}
return $response;
}
/**
* @return PromiseInterface<ResponseInterface>
*/
private function coroutine(\Generator $generator): PromiseInterface
{
$next = null;
$deferred = new Deferred();
$next = function () use ($generator, &$next, $deferred) {
if (!$generator->valid()) {
$deferred->resolve($generator->getReturn());
return;
}
$promise = $generator->current();
assert($promise instanceof PromiseInterface);
$promise->then(function ($value) use ($generator, $next) {
$generator->send($value);
$next();
}, function ($reason) use ($generator, $next) {
$generator->throw($reason);
$next();
});
};
$next();
return $deferred->promise();
}
}