jdecool/scaleway-runtimepublic Fork
⑂ 17f12fd
Commits
⬇ Clone ▾
git clone https://git.rickub.com/jdecool/scaleway-runtime.git
git clone ssh://git@rickub.com/jdecool/scaleway-runtime.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP

feat: run HttpKernel Applications across InvocationsUnverified

Jérémy DECOOL committed 2026-11-13T21:58:07+01:00 Browse files
17f12fd parent: 446efcb
added src/Runner/HttpKernelRunner.php +43 -0
new file mode 100644
@@ -0,0 +1,43 @@
1+<?php
2+
3+declare(strict_types=1);
4+
5+namespace JDecool\ScalewayRuntime\Runner;
6+
7+use JDecool\ScalewayRuntime\HttpConverter;
8+use Symfony\Component\HttpFoundation\Request;
9+use Symfony\Component\HttpKernel\HttpKernelInterface;
10+use Symfony\Component\HttpKernel\KernelInterface;
11+use Symfony\Component\HttpKernel\TerminableInterface;
12+
13+final readonly class HttpKernelRunner implements InvocationRunner
14+{
15+ public function __construct(
16+ private HttpKernelInterface $kernel,
17+ ) {
18+ // Booting here makes a broken container a boot failure, so the Handler retries with a fresh Application.
19+ if ($kernel instanceof KernelInterface) {
20+ $kernel->boot();
21+ }
22+
23+ // The Kernel applies the app's own trusted_proxies while booting; only fill the gap when it set none.
24+ if ([] === Request::getTrustedProxies()) {
25+ Request::setTrustedProxies([HttpConverter::PROXY_ADDRESS], HttpConverter::TRUSTED_HEADERS);
26+ }
27+ }
28+
29+ public function invoke(array $event, mixed $context): array
30+ {
31+ $request = HttpConverter::toRequest($event);
32+ // The Kernel resets its services itself (services_resetter) when the next main request is handled.
33+ $response = $this->kernel->handle($request);
34+
35+ try {
36+ return HttpConverter::toFunctionResponse($response);
37+ } finally {
38+ if ($this->kernel instanceof TerminableInterface) {
39+ $this->kernel->terminate($request, $response);
40+ }
41+ }
42+ }
43+}
new file mode 100644
@@ -0,0 +1,43 @@
1+<?php
2+
3+declare(strict_types=1);
4+
5+namespace JDecool\ScalewayRuntime\Runner;
6+
7+use JDecool\ScalewayRuntime\HttpConverter;
8+use Symfony\Component\HttpFoundation\Request;
9+use Symfony\Component\HttpKernel\HttpKernelInterface;
10+use Symfony\Component\HttpKernel\KernelInterface;
11+use Symfony\Component\HttpKernel\TerminableInterface;
12+
13+final readonly class HttpKernelRunner implements InvocationRunner
14+{
15+ public function __construct(
16+ private HttpKernelInterface $kernel,
17+ ) {
18+ // Booting here makes a broken container a boot failure, so the Handler retries with a fresh Application.
19+ if ($kernel instanceof KernelInterface) {
20+ $kernel->boot();
21+ }
22+
23+ // The Kernel applies the app's own trusted_proxies while booting; only fill the gap when it set none.
24+ if ([] === Request::getTrustedProxies()) {
25+ Request::setTrustedProxies([HttpConverter::PROXY_ADDRESS], HttpConverter::TRUSTED_HEADERS);
26+ }
27+ }
28+
29+ public function invoke(array $event, mixed $context): array
30+ {
31+ $request = HttpConverter::toRequest($event);
32+ // The Kernel resets its services itself (services_resetter) when the next main request is handled.
33+ $response = $this->kernel->handle($request);
34+
35+ try {
36+ return HttpConverter::toFunctionResponse($response);
37+ } finally {
38+ if ($this->kernel instanceof TerminableInterface) {
39+ $this->kernel->terminate($request, $response);
40+ }
41+ }
42+ }
43+}
added src/Runner/InvocationRunner.php +24 -0
new file mode 100644
@@ -0,0 +1,24 @@
1+<?php
2+
3+declare(strict_types=1);
4+
5+namespace JDecool\ScalewayRuntime\Runner;
6+
7+use JDecool\ScalewayRuntime\HttpConverter;
8+
9+/**
10+ * Turns one Event into one Function Response for an Application kept across Invocations.
11+ *
12+ * Symfony's RunnerInterface::run(): int has no room for an Event nor a Function Response, hence this contract.
13+ *
14+ * @phpstan-import-type FunctionResponse from HttpConverter
15+ */
16+interface InvocationRunner
17+{
18+ /**
19+ * @param array<mixed> $event
20+ *
21+ * @return FunctionResponse
22+ */
23+ public function invoke(array $event, mixed $context): array;
24+}
new file mode 100644
@@ -0,0 +1,24 @@
1+<?php
2+
3+declare(strict_types=1);
4+
5+namespace JDecool\ScalewayRuntime\Runner;
6+
7+use JDecool\ScalewayRuntime\HttpConverter;
8+
9+/**
10+ * Turns one Event into one Function Response for an Application kept across Invocations.
11+ *
12+ * Symfony's RunnerInterface::run(): int has no room for an Event nor a Function Response, hence this contract.
13+ *
14+ * @phpstan-import-type FunctionResponse from HttpConverter
15+ */
16+interface InvocationRunner
17+{
18+ /**
19+ * @param array<mixed> $event
20+ *
21+ * @return FunctionResponse
22+ */
23+ public function invoke(array $event, mixed $context): array;
24+}
added tests/Fixtures/RecordingKernel.php +52 -0
new file mode 100644
@@ -0,0 +1,52 @@
1+<?php
2+
3+declare(strict_types=1);
4+
5+namespace JDecool\ScalewayRuntime\Tests\Fixtures;
6+
7+use Symfony\Component\HttpFoundation\Request;
8+use Symfony\Component\HttpFoundation\Response;
9+use Symfony\Component\HttpKernel\HttpKernelInterface;
10+use Symfony\Component\HttpKernel\TerminableInterface;
11+
12+final class RecordingKernel implements HttpKernelInterface, TerminableInterface
13+{
14+ public static int $builds = 0;
15+ public static int $failingBuilds = 0;
16+ public static ?self $last = null;
17+
18+ public int $handled = 0;
19+ public int $terminated = 0;
20+
21+ public static function build(): self
22+ {
23+ ++self::$builds;
24+ if (self::$failingBuilds > 0) {
25+ --self::$failingBuilds;
26+
27+ throw new \RuntimeException('Build failed on purpose.');
28+ }
29+
30+ return self::$last = new self();
31+ }
32+
33+ public static function reset(): void
34+ {
35+ self::$builds = self::$failingBuilds = 0;
36+ self::$last = null;
37+ }
38+
39+ public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = true): Response
40+ {
41+ if ('/throw' === $request->getPathInfo()) {
42+ throw new \LogicException('Escaped on purpose.');
43+ }
44+
45+ return new Response('handled #'.++$this->handled.' '.$request->getPathInfo());
46+ }
47+
48+ public function terminate(Request $request, Response $response): void
49+ {
50+ ++$this->terminated;
51+ }
52+}
new file mode 100644
@@ -0,0 +1,52 @@
1+<?php
2+
3+declare(strict_types=1);
4+
5+namespace JDecool\ScalewayRuntime\Tests\Fixtures;
6+
7+use Symfony\Component\HttpFoundation\Request;
8+use Symfony\Component\HttpFoundation\Response;
9+use Symfony\Component\HttpKernel\HttpKernelInterface;
10+use Symfony\Component\HttpKernel\TerminableInterface;
11+
12+final class RecordingKernel implements HttpKernelInterface, TerminableInterface
13+{
14+ public static int $builds = 0;
15+ public static int $failingBuilds = 0;
16+ public static ?self $last = null;
17+
18+ public int $handled = 0;
19+ public int $terminated = 0;
20+
21+ public static function build(): self
22+ {
23+ ++self::$builds;
24+ if (self::$failingBuilds > 0) {
25+ --self::$failingBuilds;
26+
27+ throw new \RuntimeException('Build failed on purpose.');
28+ }
29+
30+ return self::$last = new self();
31+ }
32+
33+ public static function reset(): void
34+ {
35+ self::$builds = self::$failingBuilds = 0;
36+ self::$last = null;
37+ }
38+
39+ public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = true): Response
40+ {
41+ if ('/throw' === $request->getPathInfo()) {
42+ throw new \LogicException('Escaped on purpose.');
43+ }
44+
45+ return new Response('handled #'.++$this->handled.' '.$request->getPathInfo());
46+ }
47+
48+ public function terminate(Request $request, Response $response): void
49+ {
50+ ++$this->terminated;
51+ }
52+}
added tests/Runner/HttpKernelRunnerTest.php +49 -0
new file mode 100644
@@ -0,0 +1,49 @@
1+<?php
2+
3+declare(strict_types=1);
4+
5+namespace JDecool\ScalewayRuntime\Tests\Runner;
6+
7+use JDecool\ScalewayRuntime\HttpConverter;
8+use JDecool\ScalewayRuntime\Runner\HttpKernelRunner;
9+use JDecool\ScalewayRuntime\Tests\Fixtures\RecordingKernel;
10+use PHPUnit\Framework\TestCase;
11+use Symfony\Component\HttpFoundation\Request;
12+
13+final class HttpKernelRunnerTest extends TestCase
14+{
15+ protected function tearDown(): void
16+ {
17+ Request::setTrustedProxies([], 0);
18+ }
19+
20+ public function testPlatformProxyIsTrustedByDefault(): void
21+ {
22+ new HttpKernelRunner(new RecordingKernel());
23+
24+ self::assertSame([HttpConverter::PROXY_ADDRESS], Request::getTrustedProxies());
25+ self::assertSame(HttpConverter::TRUSTED_HEADERS, Request::getTrustedHeaderSet());
26+ }
27+
28+ public function testTrustedProxiesConfiguredByTheAppAreKept(): void
29+ {
30+ Request::setTrustedProxies(['10.0.0.0/8'], Request::HEADER_X_FORWARDED_FOR);
31+
32+ new HttpKernelRunner(new RecordingKernel());
33+
34+ self::assertSame(['10.0.0.0/8'], Request::getTrustedProxies());
35+ self::assertSame(Request::HEADER_X_FORWARDED_FOR, Request::getTrustedHeaderSet());
36+ }
37+
38+ public function testKernelIsTerminatedAfterEachInvocation(): void
39+ {
40+ $kernel = new RecordingKernel();
41+ $runner = new HttpKernelRunner($kernel);
42+
43+ $runner->invoke(['path' => '/'], []);
44+ $functionResponse = $runner->invoke(['path' => '/'], []);
45+
46+ self::assertSame(['statusCode' => 200, 'body' => 'handled #2 /'], array_intersect_key($functionResponse, ['statusCode' => 0, 'body' => 0]));
47+ self::assertSame(2, $kernel->terminated);
48+ }
49+}
new file mode 100644
@@ -0,0 +1,49 @@
1+<?php
2+
3+declare(strict_types=1);
4+
5+namespace JDecool\ScalewayRuntime\Tests\Runner;
6+
7+use JDecool\ScalewayRuntime\HttpConverter;
8+use JDecool\ScalewayRuntime\Runner\HttpKernelRunner;
9+use JDecool\ScalewayRuntime\Tests\Fixtures\RecordingKernel;
10+use PHPUnit\Framework\TestCase;
11+use Symfony\Component\HttpFoundation\Request;
12+
13+final class HttpKernelRunnerTest extends TestCase
14+{
15+ protected function tearDown(): void
16+ {
17+ Request::setTrustedProxies([], 0);
18+ }
19+
20+ public function testPlatformProxyIsTrustedByDefault(): void
21+ {
22+ new HttpKernelRunner(new RecordingKernel());
23+
24+ self::assertSame([HttpConverter::PROXY_ADDRESS], Request::getTrustedProxies());
25+ self::assertSame(HttpConverter::TRUSTED_HEADERS, Request::getTrustedHeaderSet());
26+ }
27+
28+ public function testTrustedProxiesConfiguredByTheAppAreKept(): void
29+ {
30+ Request::setTrustedProxies(['10.0.0.0/8'], Request::HEADER_X_FORWARDED_FOR);
31+
32+ new HttpKernelRunner(new RecordingKernel());
33+
34+ self::assertSame(['10.0.0.0/8'], Request::getTrustedProxies());
35+ self::assertSame(Request::HEADER_X_FORWARDED_FOR, Request::getTrustedHeaderSet());
36+ }
37+
38+ public function testKernelIsTerminatedAfterEachInvocation(): void
39+ {
40+ $kernel = new RecordingKernel();
41+ $runner = new HttpKernelRunner($kernel);
42+
43+ $runner->invoke(['path' => '/'], []);
44+ $functionResponse = $runner->invoke(['path' => '/'], []);
45+
46+ self::assertSame(['statusCode' => 200, 'body' => 'handled #2 /'], array_intersect_key($functionResponse, ['statusCode' => 0, 'body' => 0]));
47+ self::assertSame(2, $kernel->terminated);
48+ }
49+}