diff --git a/src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/StreamedResponseListenerPass.php b/src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/StreamedResponseListenerPass.php new file mode 100644 index 00000000..64934bbb --- /dev/null +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/StreamedResponseListenerPass.php @@ -0,0 +1,21 @@ +hasDefinition('streamed_response_listener')) { + $container->removeDefinition('streamed_response_listener'); + } + } +} diff --git a/src/Bridge/Symfony/Bundle/SwooleBundle.php b/src/Bridge/Symfony/Bundle/SwooleBundle.php index 814ea724..965c9583 100644 --- a/src/Bridge/Symfony/Bundle/SwooleBundle.php +++ b/src/Bridge/Symfony/Bundle/SwooleBundle.php @@ -5,6 +5,7 @@ namespace K911\Swoole\Bridge\Symfony\Bundle; use K911\Swoole\Bridge\Symfony\Bundle\DependencyInjection\CompilerPass\DebugLogProcessorPass; +use K911\Swoole\Bridge\Symfony\Bundle\DependencyInjection\CompilerPass\StreamedResponseListenerPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -13,5 +14,6 @@ final class SwooleBundle extends Bundle public function build(ContainerBuilder $container): void { $container->addCompilerPass(new DebugLogProcessorPass()); + $container->addCompilerPass(new StreamedResponseListenerPass()); } } diff --git a/src/Bridge/Symfony/HttpFoundation/ResponseProcessor.php b/src/Bridge/Symfony/HttpFoundation/ResponseProcessor.php index cbc6f1c5..34e07045 100644 --- a/src/Bridge/Symfony/HttpFoundation/ResponseProcessor.php +++ b/src/Bridge/Symfony/HttpFoundation/ResponseProcessor.php @@ -4,7 +4,6 @@ namespace K911\Swoole\Bridge\Symfony\HttpFoundation; -use RuntimeException; use Swoole\Http\Response as SwooleResponse; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse; @@ -17,10 +16,6 @@ final class ResponseProcessor implements ResponseProcessorInterface */ public function process(HttpFoundationResponse $httpFoundationResponse, SwooleResponse $swooleResponse): void { - if ($httpFoundationResponse instanceof StreamedResponse) { - throw new RuntimeException(\sprintf('HttpFoundation "StreamedResponse" response object is not yet supported')); - } - foreach ($httpFoundationResponse->headers->allPreserveCaseWithoutCookies() as $name => $values) { $swooleResponse->header($name, \implode(', ', $values)); } @@ -42,6 +37,17 @@ public function process(HttpFoundationResponse $httpFoundationResponse, SwooleRe if ($httpFoundationResponse instanceof BinaryFileResponse) { $swooleResponse->sendfile($httpFoundationResponse->getFile()->getRealPath()); + } elseif ($httpFoundationResponse instanceof StreamedResponse) { + \ob_start(function (string $payload) use ($swooleResponse) { + if ('' !== $payload) { + $swooleResponse->write($payload); + } + + return ''; + }, 8192); + $httpFoundationResponse->sendContent(); + \ob_end_clean(); + $swooleResponse->end(); } else { $swooleResponse->end($httpFoundationResponse->getContent()); } diff --git a/src/Bridge/Symfony/HttpKernel/HttpKernelRequestHandler.php b/src/Bridge/Symfony/HttpKernel/HttpKernelRequestHandler.php index b8e56130..2422b6d7 100644 --- a/src/Bridge/Symfony/HttpKernel/HttpKernelRequestHandler.php +++ b/src/Bridge/Symfony/HttpKernel/HttpKernelRequestHandler.php @@ -19,8 +19,11 @@ final class HttpKernelRequestHandler implements RequestHandlerInterface, Bootabl private $requestFactory; private $responseProcessor; - public function __construct(KernelInterface $kernel, RequestFactoryInterface $requestFactory, ResponseProcessorInterface $responseProcessor) - { + public function __construct( + KernelInterface $kernel, + RequestFactoryInterface $requestFactory, + ResponseProcessorInterface $responseProcessor + ) { $this->kernel = $kernel; $this->requestFactory = $requestFactory; $this->responseProcessor = $responseProcessor; diff --git a/tests/Fixtures/Symfony/TestBundle/Controller/StreamedResponseController.php b/tests/Fixtures/Symfony/TestBundle/Controller/StreamedResponseController.php new file mode 100644 index 00000000..1822e679 --- /dev/null +++ b/tests/Fixtures/Symfony/TestBundle/Controller/StreamedResponseController.php @@ -0,0 +1,22 @@ +getUri()); + }); + } +}