xel/psr7bridge

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

0.1.5 2024-03-24 14:13 UTC

This package is auto-updated.

Last update: 2024-04-24 14:27:24 UTC


README


Swoole Psr7 Bridge

This library still not for production. The Idea of this library is to process copy Swoole Http response to psr 7 standart when needed and leverage this copy using Psr 17 factory. in response, it will have dynamic switching when the response content have the larger byte value, it will split and make it chunk to make it more light to send.

Explore the docs »

View Demo . Report Bug . Request Feature

Downloads Contributors Forks Stargazers Issues License

Getting Started

To get start with this library you need several prerequest

Prerequisites

  • ext-swoole => V 5.0.0
  • php => V 8.2

Installation

  1. Install with this command :
     composer require xel/psr7bridge

Usage

  1. In you server.php or in file which containt swoole server :
     <?php

use HttpSoft\Message\ServerRequestFactory;
use HttpSoft\Message\StreamFactory;
use HttpSoft\Message\UploadedFileFactory;
use HttpSoft\Message\ResponseFactory;
use Swoole\Http\Server;
use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;
use Xel\Psr7bridge\PsrFactory;


require __DIR__."/vendor/autoload.php";

$server = new Server("0.0.0.0", 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);

$server->set([
    "worker_num" => 35,
    "dispatch_mode" => 1
]);

/***
 * In this sample using Psr7 and Psr17 Provided by HttpSoft.
 * u can u other library to utilize this
 */
$psr7RequestFactory =  new PsrFactory
(
    new ServerRequestFactory(),
    new StreamFactory(),
    new UploadedFileFactory(),

);

$psr7Response = new ResponseFactory();
$psr7Stream =  new StreamFactory();

$server->on("request" , function (SwooleRequest $request, SwooleResponse $response) use ($psr7RequestFactory,$psr7Response, $psr7Stream){

    // ? Connect Swoole http request with Psr 17 factory
    $psr7RequestFactory->connectRequest($request);

    // ? Sample data in stream
    $data = $psr7Stream->createStream("Hello Swoole");
    
    // ? create response and
    $manage = $psr7Response->createResponse();
    $manage =  $manage->withBody($data);
    $manage = $manage->withStatus(200);
    
    // ?  bridge it to psr7
    $psr7RequestFactory->connectResponse($manage, $response);
});

$server->start();

For more examples, please refer to the Documentation