From e97a9ca1ef6751e96be696b6ad20cf78a355202b Mon Sep 17 00:00:00 2001 From: bxel07 Date: Sun, 24 Mar 2024 21:13:16 +0700 Subject: [PATCH] fix: lazy injection --- src/Core/RequestMapper.php | 8 +++----- src/Core/ResponseMapper.php | 4 ++-- src/PsrFactory.php | 21 +++++++++------------ 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/Core/RequestMapper.php b/src/Core/RequestMapper.php index eec344e..7b1c4bd 100644 --- a/src/Core/RequestMapper.php +++ b/src/Core/RequestMapper.php @@ -11,15 +11,13 @@ use Psr\Http\Message\UploadedFileInterface; use Swoole\Http\Request as SwooleRequest; use RuntimeException; -final class RequestMapper +final readonly class RequestMapper { - - public function __construct ( private ServerRequestFactoryInterface $serverRequestFactory, - private StreamFactoryInterface $streamFactory, - private UploadedFileFactoryInterface $uploadedFileFactory, + private StreamFactoryInterface $streamFactory, + private UploadedFileFactoryInterface $uploadedFileFactory, ) { diff --git a/src/Core/ResponseMapper.php b/src/Core/ResponseMapper.php index 7eb0e0a..4181948 100644 --- a/src/Core/ResponseMapper.php +++ b/src/Core/ResponseMapper.php @@ -11,7 +11,7 @@ final class ResponseMapper private ResponseInterface $psr7Response; private SwooleResponse $swooleResponse; - const THRESHOLD = 8192; + const THRESHOLD = 4098; public function __invoke ( ResponseInterface $psr7Response, @@ -73,7 +73,7 @@ public function mapBody(ResponseInterface $response, SwooleResponse $swooleRespo private function chuckAllocator($body,$allocChunk, $swooleResponse): void { - $getSizeChunk = intval($allocChunk * 0.5); + $getSizeChunk = intval($allocChunk * 0.25); while (!$body->eof()) { $chunk = $body->read($getSizeChunk); if ($chunk === '') { diff --git a/src/PsrFactory.php b/src/PsrFactory.php index bfbd8cd..c09f59e 100644 --- a/src/PsrFactory.php +++ b/src/PsrFactory.php @@ -12,8 +12,8 @@ final class PsrFactory implements BridgeFactoryApp { - private ?RequestMapper $mapper = null; - private ?ResponseMapper $responseMapper = null; + private RequestMapper $mapper; + private ResponseMapper $responseMapper; public function __construct ( private $register @@ -26,13 +26,12 @@ public function __construct public function connectRequest(SwooleRequest $request): ServerRequestInterface { // ? Convert Swoole request to PSR-7 ServerRequest - if ($this->mapper === null){ - $this->mapper = new RequestMapper( - $this->register->get('ServerFactory'), - $this->register->get('StreamFactory'), - $this->register->get('UploadFactory') - ); - } + $this->mapper = new RequestMapper( + $this->register->get('ServerFactory'), + $this->register->get('StreamFactory'), + $this->register->get('UploadFactory') + ); + return $this->mapper->serverMap( $request ); @@ -45,9 +44,7 @@ public function connectRequest(SwooleRequest $request): ServerRequestInterface public function connectResponse(ResponseInterface $psr7, SwooleResponse $swooleResponse): void { - if ($this->responseMapper === null){ - $this->responseMapper = new ResponseMapper(); - } + $this->responseMapper = new ResponseMapper(); ($this->responseMapper)($psr7, $swooleResponse)->responseMap(); } } \ No newline at end of file