Add FrankenPhpWorkerResponseRunner for simple response return in symfony/runtime

This commit is contained in:
Guillaume Sainthillier
2026-03-15 13:10:27 +01:00
parent 510fb07f79
commit 9b80296b32
4 changed files with 26 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ CHANGELOG
---
* Add `resolveType()` for customizing how types are resolved in runtimes extending `SymfonyRuntime`
* Add support for apps returning a `Response` object in `FrankenPhpWorkerRunner`
7.4
---

View File

@@ -12,6 +12,7 @@
namespace Symfony\Component\Runtime\Runner;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface;
use Symfony\Component\Runtime\RunnerInterface;
@@ -24,7 +25,7 @@ use Symfony\Component\Runtime\RunnerInterface;
class FrankenPhpWorkerRunner implements RunnerInterface
{
public function __construct(
private HttpKernelInterface $kernel,
private HttpKernelInterface|Response $application,
private int $loopMax,
) {
}
@@ -46,8 +47,12 @@ class FrankenPhpWorkerRunner implements RunnerInterface
// 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);
if ($this->application instanceof HttpKernelInterface) {
$sfRequest = Request::createFromGlobals();
$sfResponse = $this->application->handle($sfRequest);
} else {
$sfResponse = $this->application;
}
$sfResponse->send();
};
@@ -56,8 +61,8 @@ class FrankenPhpWorkerRunner implements RunnerInterface
do {
$ret = frankenphp_handle_request($handler);
if ($this->kernel instanceof TerminableInterface && $sfRequest && $sfResponse) {
$this->kernel->terminate($sfRequest, $sfResponse);
if ($this->application instanceof TerminableInterface && $sfRequest && $sfResponse) {
$this->application->terminate($sfRequest, $sfResponse);
}
gc_collect_cycles();

View File

@@ -177,6 +177,10 @@ class SymfonyRuntime extends GenericRuntime
}
if ($application instanceof Response) {
if ($_SERVER['FRANKENPHP_WORKER'] ?? false) {
return new FrankenPhpWorkerRunner($application, $this->options['worker_loop_max']);
}
return new ResponseRunner($application);
}

View File

@@ -44,4 +44,15 @@ class FrankenPhpWorkerRunnerTest extends TestCase
$runner = new FrankenPhpWorkerRunner($application, 500);
$this->assertSame(0, $runner->run());
}
public function testRunWithResponse()
{
$response = $this->createMock(Response::class);
$response
->expects($this->once())
->method('send');
$runner = new FrankenPhpWorkerRunner($response, 500);
$this->assertSame(0, $runner->run());
}
}