woody/http-message

dev-master 2019-01-07 23:52 UTC

This package is auto-updated.

Last update: 2024-11-29 05:27:00 UTC


README

Implements PSR-7 PHP Standard.

Presentation

Both Swoole and Symfony, for request and response objects, do not implement PSR-7 which is required to work with PSR-15.

Only Guzzle implements it correctly.

This library creates a bridge between Swoole and PSR-7 implementation.

Implementation

Swoole Server

include 'vendor/autoload.php';

use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Server;
use Woody\Http\Message\ServerRequest;

$server = new Server('0.0.0.0', 9501);

$server->on('request', function (Request $swooleRequest, Response $swooleResponse) {

    $request = ServerRequest::createFromSwoole($swooleRequest);

    // ...
    $response = new \Woody\Http\Message\Response(200, [], 'Hello World');
    $response = $response->withHeader('Server', 'My Server Name');
    // ...

    \Woody\Http\Message\Response::send($response, $swooleResponse);
});

$server->start();

Swoole and Middleware

include 'vendor/autoload.php';

use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Server;
use Woody\Http\Message\ServerRequest;
use Woody\Http\Server\Middleware\Dispatcher;

$server = new Server('0.0.0.0', 9501);

$server->on('request', function (Request $swooleRequest, Response $swooleResponse) {

    $request = ServerRequest::createFromSwoole($swooleRequest);

    $dispatcher = new Dispatcher();
    $dispatcher->pipe(new LogMiddleware());
    $dispatcher->pipe(function($request, $dispatcher) {
        $response = new \Woody\Http\Message\Response(200, [], 'Hello World');
        $response = $response->withHeader('Server', 'My Server Name');
        
        return $response;
    });

    $response = $dispatcher->handle($request);

    \Woody\Http\Message\Response::send($response, $swooleResponse);
});

$server->start();