Skip to content

Commit

Permalink
feat: Add stream support
Browse files Browse the repository at this point in the history
  • Loading branch information
apple-x-co committed Dec 8, 2024
1 parent 6e9af7e commit e1fb383
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions source/app/src/DiBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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()));
}

Expand All @@ -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);

Expand Down
6 changes: 4 additions & 2 deletions source/app/src/RequestDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit e1fb383

Please # to comment.