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 46
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
c90e03b
commit 3123f7e
Showing
7 changed files
with
140 additions
and
8 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\Bundle\DependencyInjection\CompilerPass; | ||
|
||
use K911\Swoole\Bridge\Symfony\HttpFoundation\StreamedResponseListener; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* 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 { | ||
$container | ||
->autowire('streamed_response_listener', StreamedResponseListener::class) | ||
->setAutoconfigured(true) | ||
; | ||
} | ||
} | ||
} |
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
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], | ||
]; | ||
} | ||
} |
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