ilexn / swoole-convert-psr7
convert swoole to psr7 request response
Fund package maintenance!
iLexN
Installs: 18 964
Dependents: 2
Suggesters: 0
Security: 0
Stars: 9
Watchers: 2
Forks: 4
Open Issues: 3
Requires
- php: >=8.1
- dflydev/fig-cookies: ^3.0
Requires (Dev)
- eaglewu/swoole-ide-helper: dev-master
- nyholm/psr7: ^1.5
- phpstan/phpstan: ^1.5
- phpstan/phpstan-phpunit: ^1.1
- phpstan/phpstan-strict-rules: ^1.1
- phpunit/phpunit: ^9.5
- rector/rector: ^0.12.23
- roave/security-advisories: dev-latest
- symfony/var-dumper: ^6.0
Suggests
- ext-openswoole: This package requires at least one of Open Swoole or Swoole
- ext-swoole: This package requires at least one of Open Swoole or Swoole
This package is auto-updated.
Last update: 2024-10-24 09:51:47 UTC
README
Use any PSR 17 Factory to convert to PSR 7 Response/Request.
Note: not production use yet. Just for leaning.
Start from 0.5.0, CI test also include swoole and openswoole
Install
Via Composer
$ composer require ilexn/swoole-convert-psr7
Upgrade from old package
Remove the old package
$ composer remove ilexn/swoole-convent-psr7
Install the new package
$ composer require ilexn/swoole-convert-psr7
Two package using the same namespace, no other code need to change.
Usage
use Slim 4 and Nyholm/psr7 as example.
<?php declare(strict_types=1); use Nyholm\Psr7\Factory\Psr17Factory; use Psr\Http\Message\ResponseInterface; use Swoole\Http\Response; use Swoole\Http\Request; include 'vendor/autoload.php'; $http = new swoole_http_server('0.0.0.0', 9501); $psr17Factory = new Psr17Factory(); $serverRequestFactory = new \Ilex\SwoolePsr7\SwooleServerRequestConverter( $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory ); $app = new Slim\App($psr17Factory); $app->get('/hello/{name}', function ($request, ResponseInterface $response, $args) { //dump($args); $response->getBody()->write("Hello, " . $args['name']); return $response->withHeader('X-Powered-By','ilexn'); }); $http->on('start', function ($server) { echo "Swoole http server is started at http://127.0.0.1:9501\n"; }); $http->on('request', function (Request $request, Response $response) use ($serverRequestFactory , $app ) { $psr7Request = $serverRequestFactory->createFromSwoole($request); $psr7Response = $app->handle($psr7Request); $converter = new \Ilex\SwoolePsr7\SwooleResponseConverter($response); $converter->send($psr7Response); }); $http->start();