razonyang / psr7-swoole
PSR-7 implementation for Swoole
1.0.0
2022-09-18 07:05 UTC
Requires
- php: ^8.0
- ext-swoole: ^4.8|^5.0
- nyholm/psr7: ^1.5
- psr/http-message: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- razonyang/swoole-unit: ^1.0
- razonyang/unit-helper: ^1.0
- swoole/ide-helper: ^5.0
This package is auto-updated.
Last update: 2024-10-24 13:15:07 UTC
README
The PSR-7 helpers for Swoole, the bridge of Swoole and PSR things, such as PSR-7 HTTP message, PSR-15 handlers and PSR-15 middlewares. It allows to integrate Swoole with PSR-compatible frameworks to speed up your applications.
- Convert
Swoole\Http\Request
toPsr\Http\Message\ServerRequestInterface
. - Emit
Psr\Http\Message\ResponseInterface
toSwoole\Http\Response
.
Installation
composer require razonyang/psr7-swoole --prefer-dist
Usage
<?php declare(strict_types=1); require __DIR__ . '/vendor/autoload.php'; use Nyholm\Psr7\Factory\Psr17Factory; use Nyholm\Psr7\Stream; use RazonYang\Psr7\Swoole\EmitterFactory; use RazonYang\Psr7\Swoole\ServerRequestFactory; use Swoole\Coroutine\Http\Server; use function Swoole\Coroutine\run; run(function () { $serverRequestFactory = new ServerRequestFactory(); $emitterFactory = new EmitterFactory(); $psr7Factory =new Psr17Factory(); $server = new Server('127.0.0.1', 9501, false); $server->handle('/', function ($request, $response) use ($emitterFactory, $serverRequestFactory, $psr7Factory) { $emitter = $emitterFactory->create($response); $psrRequest = $serverRequestFactory->create($request); $psrResponse = $psr7Factory ->createResponse(200) ->withBody(Stream::create($psrRequest->getUri()->getPath())); $emitter->emit($psrResponse); }); $server->start(); });