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 6, 2024
1 parent e38877f commit c31fc14
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
36 changes: 36 additions & 0 deletions source/app/src/Handler/Download.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace MyVendor\MyPackage\Handler;

use AppCore\Exception\RuntimeException;
use MyVendor\MyPackage\RequestHandler;

use function fopen;
use function fputcsv;

final class Download extends RequestHandler
{
public function onGet(): self
{
$stream = fopen('php://temp', 'wb');
if ($stream === false) {
throw new RuntimeException();
}

$this->headers = [
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename=dummy.csv',
'Content-Transfer-Encoding' => 'binary',
];
$this->stream = $stream;

fputcsv($stream, [
'ID',
'NAME',
]);

return $this;
}
}
21 changes: 17 additions & 4 deletions source/app/src/RequestDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Response\RedirectResponse;
use Laminas\Diactoros\Response\TextResponse;
use Laminas\Diactoros\Stream;
use MyVendor\MyPackage\Auth\AdminAuthenticationHandler;
use MyVendor\MyPackage\Auth\AdminAuthenticationRequestHandlerInterface;
use MyVendor\MyPackage\Auth\AuthenticationException;
Expand All @@ -40,6 +41,7 @@
use function is_array;
use function is_bool;
use function is_callable;
use function is_resource;
use function is_string;
use function method_exists;
use function sprintf;
Expand Down Expand Up @@ -141,12 +143,13 @@ public function __invoke(): ResponseInterface|null

// NOTE: Request handling
$action = sprintf('on%s', ucfirst(strtolower($this->getMethod($routerMatch))));
$parsedBody = $serverRequest->getParsedBody();
if (
$serverRequest->getMethod() === Method::POST &&
is_array($serverRequest->getParsedBody()) &&
isset($serverRequest->getParsedBody()['_method'])
is_array($parsedBody) &&
isset($parsedBody['_method']) &&
$serverRequest->getMethod() === Method::POST
) {
$action = sprintf('on%s', ucfirst(strtolower($serverRequest->getParsedBody()['_method'])));
$action = sprintf('on%s', ucfirst(strtolower($parsedBody['_method'])));
}

if (! method_exists($object, $action)) {
Expand Down Expand Up @@ -190,6 +193,16 @@ private function getResponse(RequestHandler $object): ResponseInterface
assert($renderer instanceof RendererInterface);

$response = new Response();

if (is_resource($object->stream)) {
$response = $response->withBody(new Stream($object->stream));
foreach ($object->headers as $name => $value) {
$response = $response->withHeader($name, $value);
}

return $response->withStatus($object->code);
}

$response->getBody()->write($renderer->render($object));
foreach ($object->headers as $name => $value) {
$response = $response->withHeader($name, $value);
Expand Down
3 changes: 3 additions & 0 deletions source/app/src/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class RequestHandler implements Stringable

/** @var array<string, mixed>|null */
public array|null $body = null;

/** @var resource|null */
public $stream;
public RendererInterface|null $renderer = null;
public string|null $string = null;

Expand Down
4 changes: 1 addition & 3 deletions source/app/src/Responder/WebResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use function http_response_code;
use function sprintf;

use const PHP_EOL;

final class WebResponder implements ResponderInterface
{
public function handle(ResponseInterface $response): void
Expand All @@ -24,6 +22,6 @@ public function handle(ResponseInterface $response): void
}
}

echo $response->getBody() . PHP_EOL;
echo $response->getBody();
}
}
2 changes: 2 additions & 0 deletions source/app/var/conf/aura.route.web.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

$map->get('hello', '/hello', Handler\Hello::class)
->extras(['a' => 'b']);

$map->get('download', '/download', Handler\Download::class);
});

$map->attach('/admin', '/' . $adminPrefix, function (Map $map) {
Expand Down

0 comments on commit c31fc14

Please sign in to comment.