From e1fb383d1fce9ff9460ef45399b897cc972d113c Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Sun, 8 Dec 2024 17:59:42 +0900 Subject: [PATCH] feat: Add stream support --- source/app/src/DiBinder.php | 5 +++++ source/app/src/RequestDispatcher.php | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/source/app/src/DiBinder.php b/source/app/src/DiBinder.php index cafbf70..1e76eec 100644 --- a/source/app/src/DiBinder.php +++ b/source/app/src/DiBinder.php @@ -27,6 +27,7 @@ use Aura\SqlQuery\QueryFactory; use Koriym\QueryLocator\QueryLocator; use Koriym\QueryLocator\QueryLocatorInterface; +use Laminas\Diactoros\StreamFactory; use MyVendor\MyPackage\Auth\AdminAuthenticationHandler; use MyVendor\MyPackage\Auth\AdminAuthenticator; use MyVendor\MyPackage\Auth\AdminAuthenticatorInterface; @@ -48,6 +49,7 @@ use MyVendor\MyPackage\TemplateEngine\QiqCustomHelper; use MyVendor\MyPackage\TemplateEngine\QiqRenderer; use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Message\StreamFactoryInterface; use Qiq\Template; use function assert; @@ -186,6 +188,8 @@ private function request(Container $di): void $di->set(ServerRequestInterface::class, $di->lazy(static fn () => ServerRequestFactory::fromGlobals())); $di->types[ServerRequestInterface::class] = $di->lazyGet(ServerRequestInterface::class); + $di->set(StreamFactoryInterface::class, $di->lazyNew(StreamFactory::class)); + $di->set(Accept::class, $di->lazy(static fn () => (new AcceptFactory($_SERVER))->newInstance())); } @@ -197,6 +201,7 @@ private function requestDispatcher(Container $di): void $di->params[RequestDispatcher::class]['adminAuthenticationHandler'] = $di->lazyNew(AdminAuthenticationHandler::class); $di->params[RequestDispatcher::class]['di'] = $di->lazy(static fn () => $di); $di->params[RequestDispatcher::class]['accept'] = $di->lazyGet(Accept::class); + $di->params[RequestDispatcher::class]['streamFactory'] = $di->lazyGet(StreamFactoryInterface::class); $di->types[RouterInterface::class] = $di->lazyGet(RouterInterface::class); diff --git a/source/app/src/RequestDispatcher.php b/source/app/src/RequestDispatcher.php index dab47ea..d9f3802 100644 --- a/source/app/src/RequestDispatcher.php +++ b/source/app/src/RequestDispatcher.php @@ -33,6 +33,7 @@ use MyVendor\MyPackage\Router\RouterMatch; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Message\StreamFactoryInterface; use Throwable; use function assert; @@ -57,6 +58,7 @@ public function __construct( private readonly Container $di, private readonly RouterInterface $router, private readonly ServerRequestInterface $serverRequest, + private readonly StreamFactoryInterface $streamFactory, private readonly HtmlRenderer $htmlRenderer, private readonly JsonRenderer $jsonRenderer, private readonly TextRenderer $textRenderer, @@ -198,12 +200,12 @@ private function getResponse(RequestHandler $object): ResponseInterface } if (is_resource($object->stream)) { - $response = $response->withBody(new Stream($object->stream)); + $response = $response->withBody($this->streamFactory->createStreamFromResource($object->stream)); return $response->withStatus($object->code); } - $response->getBody()->write($renderer->render($object)); + $response = $response->withBody($this->streamFactory->createStream($renderer->render($object))); return $response->withStatus($object->code); }