https://github.com/ahmard/reaponse
ReactPHP response helper.
https://github.com/ahmard/reaponse
php reactphp reactphp-components reaponse
Last synced: about 1 month ago
JSON representation
ReactPHP response helper.
- Host: GitHub
- URL: https://github.com/ahmard/reaponse
- Owner: Ahmard
- Created: 2021-02-06T15:08:59.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2021-02-06T17:08:29.000Z (about 4 years ago)
- Last Synced: 2024-11-13T03:23:45.849Z (5 months ago)
- Topics: php, reactphp, reactphp-components, reaponse
- Language: PHP
- Homepage:
- Size: 15.6 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ReactPHP Response Helper
This library provides beautiful syntax for [ReactPHP HTTP](https://reactphp.org/http)
component, this library provides syntax very similar to that of NodeJS.## Installation
You will need [Composer](https://getcomposer.org) to install this library.```bash
composer require ahmard/reactphp-response
```## Usage
### Registering middleware
- Test\Counter```php
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
```php
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
```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
```bash
php server.php
```### Request object
```php
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()}
");
$response->end('Bye!');
}
}
```### Listens to response events
```php
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](src/Http/HandlerInterface.php)
- A handler is a middleware, handler is just a fancy name given to it.### [Example](example)
## Licence
**Reaponse** is **MIT** licenced.