Skip to content

Commit

Permalink
fixing response chunk swicther and change core class to final class
Browse files Browse the repository at this point in the history
  • Loading branch information
bxel07 committed Jan 20, 2024
1 parent 7d2e425 commit 7e474bc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"require": {
"php": ">=8.2",
"dflydev/fig-cookies": "^3.1",
"nyholm/psr7": "^1.8"
"psr/http-message": "^2.0",
"psr/http-factory": "^1.0",
"httpsoft/http-message": "^1.1"
},
"require-dev": {
"phpstan/phpstan": "^1.10",
Expand All @@ -25,5 +27,4 @@
"scripts": {
"analyse": "phpstan analyse"
}

}
4 changes: 0 additions & 4 deletions src/Contract/BridgeFactoryApp.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
<?php

namespace Xel\Psr7bridge\Contract;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;
use Xel\Psr7bridge\PsrFactory;

interface BridgeFactoryApp
{

public function connectRequest
(
SwooleRequest $request,
Expand Down
14 changes: 7 additions & 7 deletions src/Core/RequestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
use Psr\Http\Message\UploadedFileInterface;
use RuntimeException;

class RequestMapper
final class RequestMapper
{
private ServerRequestFactoryInterface $serverRequestFactory;
private StreamFactoryInterface $streamFactory;
private UploadedFileFactoryInterface $uploadedFileFactory;
public array $uploadFile;
public array $uploadFile =[];

public function __invoke
(
Expand Down Expand Up @@ -63,11 +63,11 @@ public function serverMap

// ? Map List
return $mapper
->withCookieParams($swooleCookie ??[])
->withQueryParams($swooleQueryParam ?? [])
->withParsedBody($swooleParseBody ?? [])
->withCookieParams($swooleCookie)
->withQueryParams($swooleQueryParam)
->withParsedBody($swooleParseBody)
->withBody($this->streamFactory->createStream($rawContent))
->withUploadedFiles($this->uploadFile ?? [])
->withUploadedFiles($this->uploadFile)
->withProtocolVersion('1.1');
}

Expand All @@ -76,7 +76,7 @@ private function createUploadedFileStream(array $files): StreamInterface|Uploade
try {
$stream = $this->streamFactory->createStreamFromFile($files['tmp_name']);
} catch (RuntimeException) {
$stream = $this->streamFactory->createStream();
return $this->streamFactory->createStream();
}

return $this->uploadedFileFactory->createUploadedFile(
Expand Down
22 changes: 17 additions & 5 deletions src/Core/ResponseMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
use Dflydev\FigCookies\SetCookies;
use Psr\Http\Message\ResponseInterface;
use Swoole\Http\Response as SwooleResponse;
class ResponseMapper

final class ResponseMapper
{
private ResponseInterface $psr7Response;
private SwooleResponse $swooleResponse;

const THRESHOLD = 8192;
public function __invoke
(
ResponseInterface $psr7Response,
Expand Down Expand Up @@ -54,22 +57,31 @@ private function cookieMap(swooleResponse $swooleResponse, SetCookie $cookie): v
public function mapBody(ResponseInterface $response, SwooleResponse $swooleResponse): void
{
$body = $response->getBody();

$allocChunk = $body->getSize();

if ($allocChunk > self::THRESHOLD) {
$this->chuckAllocator($body,$allocChunk,$swooleResponse);
}

if($body->isSeekable()){
$body->rewind();
}

$allocChunk = $body->getSize();
$getSizeChunk = $allocChunk > 3000 ? intval($allocChunk * 0.5 ) : $allocChunk;
$swooleResponse->end($body->getContents());
}

private function chuckAllocator($body,$allocChunk, $swooleResponse): void
{
$getSizeChunk = intval($allocChunk * 0.5);
while (!$body->eof()) {
$chunk = $body->read($getSizeChunk);
if ($chunk === '') {
break;
}
$swooleResponse->write($chunk);
$swooleResponse->end();
}

$swooleResponse->end();
}

}

0 comments on commit 7e474bc

Please sign in to comment.