compwright/swoole-psr7

There is no license information available for the latest version (0.1.0) of this package.

0.1.0 2020-10-29 16:35 UTC

This package is auto-updated.

Last update: 2024-10-29 05:59:42 UTC


README

Build Status Coverage Status

Use any PSR 17 Factory to convert to PSR 7 Response/Request.

Install

Via Composer

$ composer require compwright/swoole-psr7

Usage

<?php declare(strict_types=1);

use Slim\App;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Http\Message\ResponseInterface;
use Compwright\SwoolePsr7\SwooleServerRequestConverter;
use Compwright\SwoolePsr7\SwooleResponseConverter;
use Swoole\Http\Response;
use Swoole\Http\Request;

include 'vendor/autoload.php';

$serverRequestFactory = new SwooleServerRequestConverter(
    new Psr17Factory(),
    new Psr17Factory(),
    new Psr17Factory(),
    new Psr17Factory()
);

$app = new App(new Psr17Factory());
$app->get('/hello/{name}', function ($request, ResponseInterface $response, $args) {
    $response->getBody()->write("Hello, " . $args['name']);
    return $response->withHeader('X-Powered-By','compwright');
});

$http = new swoole_http_server('0.0.0.0', 9501);
$http->on('start', function ($server) {
    echo "Swoole server 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 SwooleResponseConverter($response);
    $converter->send($psr7Response);
});

$http->start();

Reference