Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dd6342a

Browse files
bug #51972 [HttpKernel] Handle nullable callback of StreamedResponse (elementaire)
This PR was merged into the 6.3 branch. Discussion ---------- [HttpKernel] Handle nullable callback of `StreamedResponse` | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT This PR fixes a bug introduced by #51396. Having a callback in `StreamResponsed` is not a mandatory by design. So the method `getCallback` must check it like `sendContent` does. When we are dealing with cacheable ressource, only headers are sent from `StreamResponsed`, for example: ```php #[Route(path: '/download')] public function download(Request $request): StreamedResponse { // Get the last modified of a file. $lastModified = \DateTime::createFromFormat('d/m/Y H:i:s', '10/10/2023 13:22:00'); $response = (new StreamedResponse())->setLastModified($lastModified); if ($response->isNotModified($request)) { return $response; } // Get the stream of a file and attach it to the callback. // ... return $response; } ``` `HttpKernel` class have to handle it else if you obtain `HTTP 500` with message `Value of type null is not callable`. cc `@nicolas`-grekas Commits ------- 66648c5 [HttpKernel] Handle nullable callback of StreamedResponse
2 parents fb4f862 + 66648c5 commit dd6342a

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/Symfony/Component/HttpFoundation/StreamedResponse.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ public function setCallback(callable $callback): static
5656
return $this;
5757
}
5858

59-
public function getCallback(): \Closure
59+
public function getCallback(): ?\Closure
6060
{
61+
if (!isset($this->callback)) {
62+
return null;
63+
}
64+
6165
return ($this->callback)(...);
6266
}
6367

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ public function handle(Request $request, int $type = HttpKernelInterface::MAIN_R
9292
} finally {
9393
$this->requestStack->pop();
9494

95-
if ($response instanceof StreamedResponse) {
96-
$callback = $response->getCallback();
95+
if ($response instanceof StreamedResponse && $callback = $response->getCallback()) {
9796
$requestStack = $this->requestStack;
9897

9998
$response->setCallback(static function () use ($request, $callback, $requestStack) {

0 commit comments

Comments
 (0)