-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathRunner.php
More file actions
78 lines (61 loc) · 2.45 KB
/
Runner.php
File metadata and controls
78 lines (61 loc) · 2.45 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
<?php
declare(strict_types=1);
namespace Runtime\FrankenPhpSymfony;
use Runtime\FrankenPhpSymfony\Exception\InvalidMiddlewareException;
use Runtime\FrankenPhpSymfony\Middleware\MiddlewareInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface;
use Symfony\Component\Runtime\RunnerInterface;
/**
* A runner for FrankenPHP.
*
* @author Kévin Dunglas <kevin@dunglas.dev>
*/
class Runner implements RunnerInterface
{
public function __construct(
private HttpKernelInterface $kernel,
private int $loopMax,
private array $middlewares = []
) {
}
/**
* @throws InvalidMiddlewareException
*/
public function run(): int
{
// Prevent worker script termination when a client connection is interrupted
ignore_user_abort(true);
$xdebugConnectToClient = function_exists('xdebug_connect_to_client');
$server = array_filter($_SERVER, static fn (string $key) => !str_starts_with($key, 'HTTP_'), ARRAY_FILTER_USE_KEY);
$server['APP_RUNTIME_MODE'] = 'web=1&worker=1';
$handler = function () use ($server, &$sfRequest, &$sfResponse, $xdebugConnectToClient): void {
// Connect to the Xdebug client if it's available
if ($xdebugConnectToClient) {
xdebug_connect_to_client();
}
// Merge the environment variables coming from DotEnv with the ones tied to the current request
$_SERVER += $server;
$sfRequest = Request::createFromGlobals();
$sfResponse = $this->kernel->handle($sfRequest);
$sfResponse->send();
};
foreach ($this->middlewares as $middlewareClass) {
if (!is_a($middlewareClass, MiddlewareInterface::class, true)) {
throw new InvalidMiddlewareException($middlewareClass, 1761117929733);
}
$middleware = new $middlewareClass();
$handler = fn () => $middleware->wrap($handler, $server);
}
$loops = 0;
do {
$ret = \frankenphp_handle_request($handler);
if ($this->kernel instanceof TerminableInterface && $sfRequest && $sfResponse) {
$this->kernel->terminate($sfRequest, $sfResponse);
}
gc_collect_cycles();
} while ($ret && (-1 === $this->loopMax || ++$loops < $this->loopMax));
return 0;
}
}