diff --git a/.gitignore b/.gitignore index 1cf303f..772be7f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ build/ composer.lock vendor/ +.phpunit.result.cache diff --git a/README.md b/README.md index d0412f7..6faa44f 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ A request handler adapter for workerman, using PSR-7, PSR-15 and PSR-17. Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-workerman-request-handler][1]. ```sh -composer require chubbyphp/chubbyphp-workerman-request-handler "^1.2" +composer require chubbyphp/chubbyphp-workerman-request-handler "^2.0" ``` ## Usage diff --git a/composer.json b/composer.json index 6b2adc4..570c572 100644 --- a/composer.json +++ b/composer.json @@ -50,7 +50,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.2-dev" + "dev-master": "2.0-dev" } }, "scripts": { diff --git a/src/OnMessage.php b/src/OnMessage.php index 2b80471..2d5d823 100644 --- a/src/OnMessage.php +++ b/src/OnMessage.php @@ -20,7 +20,7 @@ public function __construct( public function __invoke(WorkermanTcpConnection $workermanTcpConnection, WorkermanRequest $workermanRequest): void { $this->workermanResponseEmitter->emit( - $this->requestHander->handle($this->psrRequestFactory->create($workermanRequest)), + $this->requestHander->handle($this->psrRequestFactory->create($workermanTcpConnection, $workermanRequest)), $workermanTcpConnection ); } diff --git a/src/PsrRequestFactory.php b/src/PsrRequestFactory.php index 93c0451..df7acef 100644 --- a/src/PsrRequestFactory.php +++ b/src/PsrRequestFactory.php @@ -9,6 +9,7 @@ use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\UploadedFileFactoryInterface; use Psr\Http\Message\UploadedFileInterface; +use Workerman\Connection\TcpConnection as WorkermanTcpConnection; use Workerman\Protocols\Http\Request as WorkermanRequest; final class PsrRequestFactory implements PsrRequestFactoryInterface @@ -20,11 +21,12 @@ public function __construct( ) { } - public function create(WorkermanRequest $workermanRequest): ServerRequestInterface + public function create(WorkermanTcpConnection $workermanTcpConnection, WorkermanRequest $workermanRequest): ServerRequestInterface { $request = $this->serverRequestFactory->createServerRequest( $workermanRequest->method(), - $workermanRequest->uri() + $workermanRequest->uri(), + $this->createServerParams($workermanTcpConnection), ); /** @var array $headers */ @@ -47,6 +49,17 @@ public function create(WorkermanRequest $workermanRequest): ServerRequestInterfa return $request; } + /** + * @return array + */ + private function createServerParams(WorkermanTcpConnection $workermanTcpConnection): array + { + return [ + 'REMOTE_ADDR' => $workermanTcpConnection->getRemoteIp(), + 'REMOTE_PORT' => (string) $workermanTcpConnection->getRemotePort(), + ]; + } + /** * @param array> $files * diff --git a/src/PsrRequestFactoryInterface.php b/src/PsrRequestFactoryInterface.php index 7d35ac4..74e9ae7 100644 --- a/src/PsrRequestFactoryInterface.php +++ b/src/PsrRequestFactoryInterface.php @@ -5,9 +5,10 @@ namespace Chubbyphp\WorkermanRequestHandler; use Psr\Http\Message\ServerRequestInterface; +use Workerman\Connection\TcpConnection as WorkermanTcpConnection; use Workerman\Protocols\Http\Request as WorkermanRequest; interface PsrRequestFactoryInterface { - public function create(WorkermanRequest $workermanRequest): ServerRequestInterface; + public function create(WorkermanTcpConnection $workermanTcpConnection, WorkermanRequest $workermanRequest): ServerRequestInterface; } diff --git a/tests/Unit/OnMessageTest.php b/tests/Unit/OnMessageTest.php index bc5827f..f0e638e 100644 --- a/tests/Unit/OnMessageTest.php +++ b/tests/Unit/OnMessageTest.php @@ -42,7 +42,7 @@ public function testInvoke(): void /** @var MockObject|PsrRequestFactoryInterface $psrRequestFactory */ $psrRequestFactory = $this->getMockByCalls(PsrRequestFactoryInterface::class, [ - Call::create('create')->with($workermanRequest)->willReturn($request), + Call::create('create')->with($workermanTcpConnection, $workermanRequest)->willReturn($request), ]); /** @var MockObject|WorkermanResponseEmitterInterface $workermanResponseEmitter */ diff --git a/tests/Unit/PsrRequestFactoryTest.php b/tests/Unit/PsrRequestFactoryTest.php index 3802680..6655f31 100644 --- a/tests/Unit/PsrRequestFactoryTest.php +++ b/tests/Unit/PsrRequestFactoryTest.php @@ -16,6 +16,7 @@ use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UploadedFileFactoryInterface; use Psr\Http\Message\UploadedFileInterface; +use Workerman\Connection\TcpConnection as WorkermanTcpConnection; use Workerman\Protocols\Http\Request as WorkermanRequest; /** @@ -70,6 +71,12 @@ public function testInvoke(): void Call::create('rawBody')->with()->willReturn('This is the body.'), ]); + /** @var MockObject|WorkermanTcpConnection $workermanTcpConnection */ + $workermanTcpConnection = $this->getMockByCalls(WorkermanTcpConnection::class, [ + Call::create('getRemoteIp')->with()->willReturn('172.16.89.64'), + Call::create('getRemotePort')->with()->willReturn(10817), + ]); + /** @var MockObject|StreamInterface $requestBody */ $requestBody = $this->getMockByCalls(StreamInterface::class, [ Call::create('write')->with('This is the body.'), @@ -157,11 +164,11 @@ static function (array $uploadedFiles) use ($uploadedFile1, $uploadedFile2, $upl /** @var MockObject|ServerRequestFactoryInterface $serverRequestFactory */ $serverRequestFactory = $this->getMockByCalls(ServerRequestFactoryInterface::class, [ Call::create('createServerRequest') - ->with('POST', '/application', []) + ->with('POST', '/application', ['REMOTE_ADDR' => '172.16.89.64', 'REMOTE_PORT' => '10817']) ->willReturn($request), ]); $psrRequestFactory = new PsrRequestFactory($serverRequestFactory, $streamFactory, $uploadedFileFactory); - $psrRequestFactory->create($workermanRequest); + $psrRequestFactory->create($workermanTcpConnection, $workermanRequest); } }