Skip to content

Ahmard/reaponse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ReactPHP Response Helper

This library provides beautiful syntax for ReactPHP HTTP component, this library provides syntax very similar to that of NodeJS.

Installation

You will need Composer to install this library.

composer require ahmard/reactphp-response

Usage

Registering middleware

  • Test\Counter
namespace Test;

use Reaponse\Http\HandlerInterface;
use Reaponse\Http\ResponseInterface;

class CountHandler implements HandlerInterface
{
    protected static int $counts = 0;

    public function handle(ResponseInterface $response): void
    {
        self::$counts++;
        $response->write('Count: ' . self::$counts);
        $response->handler()->next();
    }
}
  • Test\Server
namespace Test;

use Reaponse\Http\HandlerInterface;
use Reaponse\Http\ResponseInterface;

class ServerHandler implements HandlerInterface
{
    public function handle(ResponseInterface $response): void
    {
        $response->html(', Time: ' . date('H:i:s'));
        $response->end('.');
    }
}
  • server.php
use React\EventLoop\Factory;
use React\Socket\Server;
use Reaponse\Http\Middleware;
use Test\CounterHandler;
use Test\ServerHandler;

require 'vendor/autoload.php';

$loop = Factory::create();
$uri = '0.0.0.0:9200';

$myServer = new ServerHandler();
$myCounter = new CounterHandler();

$httpServer = new \React\Http\Server($loop, new Middleware($myCounter, $myServer));
$socketServer = new Server($uri, $loop);

$httpServer->listen($socketServer);
$httpServer->on('error', function (Throwable $throwable){
    echo $throwable;
});

echo "Server started at http://{$uri}\n";
$loop->run();

Start the server

php server.php

Request object

use Reaponse\Http\HandlerInterface;
use Reaponse\Http\ResponseInterface;


class TestHandler implements HandlerInterface
{
    public function handle(ResponseInterface $response): void
    {
        //psr-7 compliant object
        $request = $response->request();
        
        $response->html("Method: {$request->getMethod()}<br/>");
        
        $response->end('Bye!');
    }
}

Listens to response events

use Reaponse\Http\HandlerInterface;
use Reaponse\Http\Response;
use Reaponse\Http\ResponseInterface;


class TestHandler implements HandlerInterface
{
    public function handle(ResponseInterface $response): void
    {
        //listens to write event
        $response->on(Response::ON_WRITE, function (){
            echo "Writing...\n";
        });
        //Listens to headers event
        $response->on(Response::ON_HEADERS, function (){
            echo "Headers...\n";
        });
        //Listens to next handler event
        $response->on(Response::ON_NEXT_HANDLER, function (){
            echo "Next handler...\n";
        });
        //Listens to response sending event
        $response->on(Response::ON_BEFORE_SEND, function (){
            echo "Sending...\n";
        });
        
        $response->end('Hello World');
    }
}
  • All handlers must implement HandlerInterface
  • A handler is a middleware, handler is just a fancy name given to it.

Licence

Reaponse is MIT licenced.