Skip to content

Commit

Permalink
fix: lazy injection
Browse files Browse the repository at this point in the history
  • Loading branch information
bxel07 committed Mar 24, 2024
1 parent e8a6b1e commit e97a9ca
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
8 changes: 3 additions & 5 deletions src/Core/RequestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
{

Expand Down
4 changes: 2 additions & 2 deletions src/Core/ResponseMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class ResponseMapper
private ResponseInterface $psr7Response;
private SwooleResponse $swooleResponse;

const THRESHOLD = 8192;
const THRESHOLD = 4098;
public function __invoke
(
ResponseInterface $psr7Response,
Expand Down Expand Up @@ -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 === '') {
Expand Down
21 changes: 9 additions & 12 deletions src/PsrFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
);
Expand All @@ -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();
}
}

0 comments on commit e97a9ca

Please sign in to comment.