Skip to content

Commit

Permalink
feature #52047 [HttpFoundation][Runtime] Add $flush parameter to Resp…
Browse files Browse the repository at this point in the history
…onse::send() (fancyweb)

This PR was merged into the 6.4 branch.

Discussion
----------

[HttpFoundation][Runtime] Add $flush parameter to Response::send()

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | -
| License       | MIT

Inspired by #51912 and #45205 subjects

Adds optional argument `$flush` to `Response::send()`. If `$flush === false`, output buffers are not flushed/`*_finish_request()` and alike functions are not called.

We leverage that in the Runtime component by not flushing the output buffers when debug mode is on in order to see exceptions thrown in listeners listening on the `TerminateEvent` and also to feel a more "real" processing time of things happening on terminate.

Commits
-------

a3304cc [HttpFoundation] Add $flush parameter to Response::send()
  • Loading branch information
nicolas-grekas committed Oct 18, 2023
2 parents e4c4856 + a3304cc commit 923ecdb
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Support root-level `Generator` in `StreamedJsonResponse`
* Add `UriSigner` from the HttpKernel component
* Add `partitioned` flag to `Cookie` (CHIPS Cookie)
* Add argument `bool $flush = true` to `Response::send()`

6.3
---
Expand Down
9 changes: 8 additions & 1 deletion src/Symfony/Component/HttpFoundation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,13 +415,20 @@ public function sendContent(): static
/**
* Sends HTTP headers and content.
*
* @param bool $flush Whether output buffers should be flushed
*
* @return $this
*/
public function send(): static
public function send(/* bool $flush = true */): static
{
$this->sendHeaders();
$this->sendContent();

$flush = 1 <= \func_num_args() ? func_get_arg(0) : true;
if (!$flush) {
return $this;
}

if (\function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
} elseif (\function_exists('litespeed_finish_request')) {
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Runtime/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.4
---

* Add argument `bool $debug = false` to `HttpKernelRunner::__construct()`

5.4
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ class HttpKernelRunner implements RunnerInterface
public function __construct(
private readonly HttpKernelInterface $kernel,
private readonly Request $request,
private readonly bool $debug = false,
) {
}

public function run(): int
{
$response = $this->kernel->handle($this->request);
$response->send();
$response->send(!$this->debug);

if ($this->kernel instanceof TerminableInterface) {
$this->kernel->terminate($this->request, $response);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Runtime/SymfonyRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function __construct(array $options = [])
public function getRunner(?object $application): RunnerInterface
{
if ($application instanceof HttpKernelInterface) {
return new HttpKernelRunner($application, Request::createFromGlobals());
return new HttpKernelRunner($application, Request::createFromGlobals(), $this->options['debug'] ?? false);
}

if ($application instanceof Response) {
Expand Down

0 comments on commit 923ecdb

Please sign in to comment.