Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/leocavalcante/request-callback
➰ Swoole request callback for PSR compliant handlers.
https://github.com/leocavalcante/request-callback
middleware psr-15 psr-7 request-handler swoole
Last synced: 3 months ago
JSON representation
➰ Swoole request callback for PSR compliant handlers.
- Host: GitHub
- URL: https://github.com/leocavalcante/request-callback
- Owner: leocavalcante
- License: mit
- Created: 2021-01-01T23:54:04.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-11-08T11:21:09.000Z (about 2 years ago)
- Last Synced: 2024-10-03T10:49:36.519Z (4 months ago)
- Topics: middleware, psr-15, psr-7, request-handler, swoole
- Language: PHP
- Homepage:
- Size: 387 KB
- Stars: 24
- Watchers: 4
- Forks: 5
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-swoole - leocavalcante/request-callback - Swoole request callback for PSR compliant handlers. Compatible with PSR-7 and PSR-15. (HTTP and WebSocket)
README
# ➰ Request Callback
![CI](https://github.com/leocavalcante/request-callback/workflows/CI/badge.svg)
[![codecov](https://codecov.io/gh/leocavalcante/request-callback/branch/main/graph/badge.svg?token=TE1YQYNKHJ)](https://codecov.io/gh/leocavalcante/request-callback)
[![psalm](https://shepherd.dev/github/leocavalcante/request-callback/coverage.svg)](https://shepherd.dev/github/leocavalcante/request-callback)[Swoole](https://www.swoole.co.uk/) request callback for [PSR](https://www.php-fig.org/psr/) compliant handlers.
## Install
```bash
composer require leocavalcante/request-callback
```## Usage
Simply create a callback that decorates a [`RequestHandlerInterface`](https://www.php-fig.org/psr/psr-15/#11-request-handlers) and pass as a listener to the [`onRequest`](https://www.swoole.co.uk/docs/modules/swoole-http-server-on-request) event of the [`Swoole\Http\Server`](https://www.swoole.co.uk/docs/modules/swoole-http-server-doc).
```php
use Swoole\Http\RequestCallback;$callback = new RequestCallback(\Psr\Http\Server\RequestHandlerInterface);
```### Example
#### Hello, World!
```php
use Laminas\Diactoros\Response\TextResponse;
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
use Psr\Http\Server\RequestHandlerInterface;
use Swoole\Http\{RequestCallback, Server};$server = new Server('localhost', 9501);
$server->on('request', new RequestCallback(new class implements RequestHandlerInterface {
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new TextResponse(sprintf('Hello, %s!', $request->getQueryParams()['name'] ?? 'World'));
}
}));$server->start();
```To help you with the boilerplate, you can use the `RequestCallback::fromCallable(callable)` factory method:
```php
$server->on('request', RequestCallback::fromCallable(static function (ServerRequestInterface $request): ResponseInterface {
return new TextResponse(sprintf('Hello, %s!', $request->getQueryParams()['name'] ?? 'World'));
}));
```If you are like me and want even more sugar, use the `request_callback(RequestHandlerInterface|callable)` function:
```php
$server->on('request', request_callback(
static fn(ServerRequestInterface $request): ResponseInterface =>
new TextResponse(sprintf('Hello, %s!', $request->getQueryParams()['name'] ?? 'World')))
);```
You can pass either a `RequestHandlerInterface` or a `callable`, it will figure out.
#### Middleware
```php
use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Stratigility\MiddlewarePipe;
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
use Psr\Http\Server\RequestHandlerInterface;
use Swoole\Http\{RequestCallback, Server};
use function Laminas\Stratigility\middleware;$app = new MiddlewarePipe();
$app->pipe(middleware(static function (ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface {
$user = $request->getQueryParams()['user'] ?? null;if ($user === null) {
return new JsonResponse(['error' => true, 'message' => 'Unauthorized'], 401);
}return $next
->handle($request->withAttribute('user', $user))
->withAddedHeader('X-Foo', 'Bar');
}));$app->pipe(middleware(static function (ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface {
return new JsonResponse(['message' => sprintf('Hello, %s!', $request->getAttribute('user'))]);
}));$server = new Server('0.0.0.0', 9501);
$server->on('request', new RequestCallback($app));
$server->start();
```