-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding PSR RequestHandlerInterface for Swoole
- Loading branch information
1 parent
834b864
commit c3b4c34
Showing
7 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Runtime\Swoole; | ||
|
||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Swoole\Http\Request; | ||
use Swoole\Http\Response; | ||
use Symfony\Component\Runtime\RunnerInterface; | ||
|
||
class RequestHandlerRunner implements RunnerInterface | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private const CHUNK_SIZE = 2097152; // 2MB | ||
/** | ||
* @var ServerFactory | ||
*/ | ||
private $serverFactory; | ||
/** | ||
* @var RequestHandlerInterface | ||
*/ | ||
private $application; | ||
|
||
public function __construct(ServerFactory $serverFactory, RequestHandlerInterface $application) | ||
{ | ||
$this->serverFactory = $serverFactory; | ||
$this->application = $application; | ||
} | ||
|
||
public function run(): int | ||
{ | ||
$this->serverFactory->createServer([$this, 'handle'])->start(); | ||
|
||
return 0; | ||
} | ||
|
||
public function handle(Request $request, Response $response): void | ||
{ | ||
$psrRequest = (new \Nyholm\Psr7\ServerRequest( | ||
$request->getMethod(), | ||
$request->server['request_uri'] ?? '/', | ||
array_change_key_case($request->server ?? [], CASE_UPPER), | ||
$request->rawContent(), | ||
'1.1', | ||
$request->server ?? [] | ||
)) | ||
->withQueryParams($request->get ?? []); | ||
|
||
$psrResponse = $this->application->handle($psrRequest); | ||
|
||
$response->setStatusCode($psrResponse->getStatusCode(), $psrResponse->getReasonPhrase()); | ||
|
||
foreach ($psrResponse->getHeaders() as $name => $values) { | ||
foreach ($values as $value) { | ||
$response->setHeader($name, $value); | ||
} | ||
} | ||
|
||
$body = $psrResponse->getBody(); | ||
$body->rewind(); | ||
|
||
if ($body->isReadable()) { | ||
if ($body->getSize() <= self::CHUNK_SIZE) { | ||
if ($contents = $body->getContents()) { | ||
$response->write($contents); | ||
} | ||
} else { | ||
while (!$body->eof() && ($contents = $body->read(self::CHUNK_SIZE))) { | ||
$response->write($contents); | ||
} | ||
} | ||
|
||
$response->end(); | ||
} else { | ||
$response->end((string) $body); | ||
} | ||
|
||
$body->close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Runtime\Swoole; | ||
|
||
use Nyholm\Psr7\Stream; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Swoole\Http\Request; | ||
use Swoole\Http\Response; | ||
use Swoole\Http\Server; | ||
|
||
class RequestHandlerRunnerTest extends TestCase | ||
{ | ||
public function testRun(): void | ||
{ | ||
$server = $this->createMock(Server::class); | ||
$factory = $this->createMock(ServerFactory::class); | ||
$application = $this->createMock(RequestHandlerInterface::class); | ||
|
||
$factory->expects(self::once())->method('createServer')->willReturn($server); | ||
$server->expects(self::once())->method('start'); | ||
|
||
$runner = new RequestHandlerRunner($factory, $application); | ||
|
||
self::assertSame(0, $runner->run()); | ||
} | ||
|
||
public function testHandle(): void | ||
{ | ||
$factory = $this->createMock(ServerFactory::class); | ||
$application = $this->createMock(RequestHandlerInterface::class); | ||
$psrResponse = $this->createMock(ResponseInterface::class); | ||
$request = $this->createMock(Request::class); | ||
$response = $this->createMock(Response::class); | ||
|
||
$request->expects(self::once())->method('getMethod')->willReturn('POST'); | ||
$request->expects(self::once())->method('rawContent')->willReturn('Test'); | ||
|
||
$application->expects(self::once())->method('handle')->willReturn($psrResponse); | ||
|
||
$psrResponse->expects(self::once())->method('getHeaders')->willReturn([ | ||
'X-Test' => ['Swoole-Runtime'], | ||
]); | ||
$psrResponse->expects(self::once())->method('getBody')->willReturn(Stream::create('Test')); | ||
|
||
$response->expects(self::once())->method('setHeader')->with('X-Test', 'Swoole-Runtime'); | ||
$response->expects(self::once())->method('write')->with('Test'); | ||
$response->expects(self::once())->method('end')->with(null); | ||
|
||
$runner = new RequestHandlerRunner($factory, $application); | ||
$runner->handle($request, $response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters