This repository has been archived by the owner on Jan 17, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(response-processor): add support for StreamedResponse
- Loading branch information
1 parent
807ba9f
commit 89fc7ca
Showing
10 changed files
with
268 additions
and
27 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/StreamedResponseListenerPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\Bundle\DependencyInjection\CompilerPass; | ||
|
||
use K911\Swoole\Bridge\Symfony\HttpFoundation\StreamedResponseListener; | ||
use K911\Swoole\Bridge\Symfony\HttpFoundation\StreamedResponseProcessor; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Replaces Symfony's native StreamedResponseListener with a custom one compatible with Swoole. | ||
*/ | ||
final class StreamedResponseListenerPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
if ($container->hasDefinition('streamed_response_listener')) { | ||
$definition = $container->getDefinition('streamed_response_listener'); | ||
$definition | ||
->setClass(StreamedResponseListener::class) | ||
->setAutowired(true) | ||
; | ||
} else { | ||
$definition = $container | ||
->autowire('streamed_response_listener', StreamedResponseListener::class) | ||
->setAutoconfigured(true) | ||
; | ||
} | ||
$definition->setArgument(1, new Reference(StreamedResponseProcessor::class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/Bridge/Symfony/HttpFoundation/NoOpStreamedResponseProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\HttpFoundation; | ||
|
||
use Swoole\Http\Response as SwooleResponse; | ||
use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse; | ||
use Symfony\Component\HttpFoundation\StreamedResponse; | ||
|
||
final class NoOpStreamedResponseProcessor implements ResponseProcessorInterface | ||
{ | ||
/** | ||
* @var ResponseProcessorInterface | ||
*/ | ||
private $decorated; | ||
|
||
public function __construct(ResponseProcessorInterface $decorated) | ||
{ | ||
$this->decorated = $decorated; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(HttpFoundationResponse $httpFoundationResponse, SwooleResponse $swooleResponse): void | ||
{ | ||
if ($httpFoundationResponse instanceof StreamedResponse) { | ||
return; | ||
} | ||
|
||
$this->decorated->process($httpFoundationResponse, $swooleResponse); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Bridge/Symfony/HttpFoundation/ResponseHeadersAndStatusProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\HttpFoundation; | ||
|
||
use Swoole\Http\Response as SwooleResponse; | ||
use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse; | ||
|
||
final class ResponseHeadersAndStatusProcessor implements ResponseProcessorInterface | ||
{ | ||
/** | ||
* @var ResponseProcessorInterface | ||
*/ | ||
private $decorated; | ||
|
||
public function __construct(ResponseProcessorInterface $decorated) | ||
{ | ||
$this->decorated = $decorated; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(HttpFoundationResponse $httpFoundationResponse, SwooleResponse $swooleResponse): void | ||
{ | ||
foreach ($httpFoundationResponse->headers->allPreserveCaseWithoutCookies() as $name => $values) { | ||
$swooleResponse->header($name, \implode(', ', $values)); | ||
} | ||
|
||
foreach ($httpFoundationResponse->headers->getCookies() as $cookie) { | ||
$swooleResponse->cookie( | ||
$cookie->getName(), | ||
$cookie->getValue() ?? '', | ||
$cookie->getExpiresTime(), | ||
$cookie->getPath(), | ||
$cookie->getDomain() ?? '', | ||
$cookie->isSecure(), | ||
$cookie->isHttpOnly(), | ||
$cookie->getSameSite() ?? '' | ||
); | ||
} | ||
|
||
$swooleResponse->status($httpFoundationResponse->getStatusCode()); | ||
|
||
$this->decorated->process($httpFoundationResponse, $swooleResponse); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/Bridge/Symfony/HttpFoundation/StreamedResponseListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\HttpFoundation; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\StreamedResponse; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
class StreamedResponseListener implements EventSubscriberInterface | ||
{ | ||
private $contextManager; | ||
private $responseProcessor; | ||
|
||
public function __construct( | ||
SwooleRequestResponseContextManager $contextManager, | ||
ResponseProcessorInterface $responseProcessor | ||
) { | ||
$this->responseProcessor = $responseProcessor; | ||
$this->contextManager = $contextManager; | ||
} | ||
|
||
public function onKernelResponse(ResponseEvent $event): void | ||
{ | ||
if (!$event->isMasterRequest()) { | ||
return; | ||
} | ||
|
||
$response = $event->getResponse(); | ||
if ($response instanceof StreamedResponse) { | ||
$swooleResponse = $this->contextManager->findResponse($event->getRequest()); | ||
$this->responseProcessor->process($response, $swooleResponse); | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
KernelEvents::RESPONSE => ['onKernelResponse', -1024], | ||
]; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Bridge/Symfony/HttpFoundation/StreamedResponseProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\HttpFoundation; | ||
|
||
use Assert\Assertion; | ||
use Swoole\Http\Response as SwooleResponse; | ||
use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse; | ||
use Symfony\Component\HttpFoundation\StreamedResponse; | ||
|
||
final class StreamedResponseProcessor implements ResponseProcessorInterface | ||
{ | ||
private $bufferOutputSize; | ||
|
||
public function __construct(int $bufferOutputSize = 8192) | ||
{ | ||
$this->bufferOutputSize = $bufferOutputSize; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(HttpFoundationResponse $httpFoundationResponse, SwooleResponse $swooleResponse): void | ||
{ | ||
Assertion::isInstanceOf($httpFoundationResponse, StreamedResponse::class); | ||
|
||
\ob_start(static function (string $payload) use ($swooleResponse) { | ||
if ('' !== $payload) { | ||
$swooleResponse->write($payload); | ||
} | ||
|
||
return ''; | ||
}, $this->bufferOutputSize); | ||
$httpFoundationResponse->sendContent(); | ||
\ob_end_clean(); | ||
$swooleResponse->end(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Bridge/Symfony/HttpFoundation/SwooleRequestResponseContextManager.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\HttpFoundation; | ||
|
||
use Swoole\Http\Request as SwooleRequest; | ||
use Swoole\Http\Response as SwooleResponse; | ||
use Symfony\Component\HttpFoundation\Request as HttpFoundationRequest; | ||
|
||
final class SwooleRequestResponseContextManager | ||
{ | ||
private const REQUEST_ATTR_KEY = 'swoole_request'; | ||
private const RESPONSE_ATTR_KEY = 'swoole_response'; | ||
|
||
public function attachRequestResponseAttributes( | ||
HttpFoundationRequest $request, | ||
SwooleRequest $swooleRequest, | ||
SwooleResponse $swooleResponse | ||
): void { | ||
$request->attributes->set(static::REQUEST_ATTR_KEY, $swooleRequest); | ||
$request->attributes->set(static::RESPONSE_ATTR_KEY, $swooleResponse); | ||
} | ||
|
||
public function findRequest( | ||
HttpFoundationRequest $request | ||
): SwooleRequest { | ||
return $request->attributes->get(static::REQUEST_ATTR_KEY); | ||
} | ||
|
||
public function findResponse( | ||
HttpFoundationRequest $request | ||
): SwooleResponse { | ||
return $request->attributes->get(static::RESPONSE_ATTR_KEY); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters