GithubHelp home page GithubHelp logo

psr7-swoole's Introduction

Swoole + PSR7

Latest Stable Version Total Downloads Build Status Scrutinizer Code Quality Code Coverage StyleCI

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 to Psr\Http\Message\ServerRequestInterface.
  • Emit Psr\Http\Message\ResponseInterface to Swoole\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();
});

psr7-swoole's People

Contributors

razonyang avatar stylecibot avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.