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

Feat/streamed response pr fix #1

Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Bridge\Symfony\Bundle\DependencyInjection\CompilerPass;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Removes Symfony's native StreamedResponseListener
*/
final class StreamedResponseListenerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if ($container->hasDefinition('streamed_response_listener')) {
$container->removeDefinition('streamed_response_listener');
}
}
}
2 changes: 2 additions & 0 deletions src/Bridge/Symfony/Bundle/SwooleBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -13,5 +14,6 @@ final class SwooleBundle extends Bundle
public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new DebugLogProcessorPass());
$container->addCompilerPass(new StreamedResponseListenerPass());
}
}
16 changes: 11 additions & 5 deletions src/Bridge/Symfony/HttpFoundation/ResponseProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
Expand All @@ -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());
}
Expand Down
7 changes: 5 additions & 2 deletions src/Bridge/Symfony/HttpKernel/HttpKernelRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Annotation\Route;

final class StreamedResponseController
{
/**
* @Route(methods={"GET"}, path="/stream/string")
*/
public function sendStreamFromRequest(Request $request): StreamedResponse
{
return new StreamedResponse(function () use ($request): void {
echo \sprintf('Full URI: %s', $request->getUri());
});
}
}