Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Generator-based response streaming #138

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/Http/StreamedResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Baldinof\RoadRunnerBundle\Http;

use Symfony\Component\HttpFoundation\StreamedResponse as SymfonyStreamedResponse;

class StreamedResponse extends SymfonyStreamedResponse
{
private \Closure $obControl;

/**
* @param callable|null $obControl Sets the output buffering control. Called only when sending response trough sendResponse().
*/
public function __construct(?callable $callback = null, int $status = 200, array $headers = [], ?callable $obControl = null)
{
parent::__construct($callback, $status, $headers);

$this->obControl = $obControl ?? function () {
if (\ob_get_status()) {
\ob_flush();
}
\flush();
};
}

public function getGenerator(): \Generator
{
if (!isset($this->callback)) {
throw new \LogicException('The Response callback must be set.');
}

$generator = ($this->callback)();
if (!$generator instanceof \Generator) {
throw new \LogicException('The Response callback is not a valid generator.');
}

return $generator;
}

public function sendContent(): static
{
if ($this->streamed) {
return $this;
}

$this->streamed = true;

if (!isset($this->callback)) {
throw new \LogicException('The Response callback must be set.');
}

foreach ($this->getGenerator() as $chunk) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make it compatible with Symfony StreamedResponse by allowing non generator callbacks?

echo $chunk;
($this->obControl)();
}

return $this;
}
}
13 changes: 8 additions & 5 deletions src/RoadRunnerBridge/HttpFoundationWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

namespace Baldinof\RoadRunnerBundle\RoadRunnerBridge;

use Baldinof\RoadRunnerBundle\Http\StreamedResponse;
use Spiral\RoadRunner\Http\HttpWorkerInterface;
use Spiral\RoadRunner\Http\Request as RoadRunnerRequest;
use Spiral\RoadRunner\WorkerInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\StreamedResponse as SymfonyStreamedResponse;

final class HttpFoundationWorker implements HttpFoundationWorkerInterface
{
Expand Down Expand Up @@ -42,8 +43,10 @@ public function respond(SymfonyResponse $symfonyResponse): void
if ($content === false) {
throw new \RuntimeException(sprintf("Cannot read file '%s'", $symfonyResponse->getFile()->getPathname())); // TODO: custom error
}
} else {
if ($symfonyResponse instanceof StreamedResponse || $symfonyResponse instanceof BinaryFileResponse) {
} elseif ($symfonyResponse instanceof SymfonyStreamedResponse || $symfonyResponse instanceof BinaryFileResponse) {
if ($symfonyResponse instanceof StreamedResponse) {
$content = $symfonyResponse->getGenerator();
} else {
$content = '';
ob_start(function ($buffer) use (&$content) {
$content .= $buffer;
Expand All @@ -53,9 +56,9 @@ public function respond(SymfonyResponse $symfonyResponse): void

$symfonyResponse->sendContent();
ob_end_clean();
} else {
$content = (string) $symfonyResponse->getContent();
}
} else {
$content = (string) $symfonyResponse->getContent();
}

$headers = $this->stringifyHeaders($symfonyResponse->headers->all());
Expand Down