leocavalcante / request-callback
Swoole request callback for PSR compliant handlers.
Installs: 1 161
Dependents: 1
Suggesters: 0
Security: 0
Stars: 24
Watchers: 4
Forks: 5
Open Issues: 8
Requires
- php: >=7.4
- ext-swoole: ^4.5
- laminas/laminas-diactoros: ^2.5
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
- psr/http-server-handler: ^1.0
Requires (Dev)
- laminas/laminas-stratigility: ^3.3
- pestphp/pest: ^0.3.19 || ^1.0.0
- swoole/ide-helper: ^4.5
- vimeo/psalm: ^4.3
- dev-main
- v0.1.1
- v0.1.0
- dev-dependabot/composer/main/vimeo/psalm-4.30.0
- dev-dependabot/composer/main/filp/whoops-2.14.6
- dev-dependabot/composer/main/symfony/console-5.4.15
- dev-dependabot/composer/main/pestphp/pest-1.22.1
- dev-dependabot/composer/main/composer/xdebug-handler-2.0.5
- dev-dependabot/composer/main/nunomaduro/collision-5.11.0
- dev-dependabot/composer/main/swoole/ide-helper-4.8.1
- dev-dependabot/add-v2-config-file
This package is auto-updated.
Last update: 2024-11-08 15:45:45 UTC
README
Swoole request callback for PSR compliant handlers.
Install
composer require leocavalcante/request-callback
Usage
Simply create a callback that decorates a RequestHandlerInterface
and pass as a listener to the onRequest
event of the Swoole\Http\Server
.
use Swoole\Http\RequestCallback; $callback = new RequestCallback(\Psr\Http\Server\RequestHandlerInterface);
Example
Hello, World!
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:
$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:
$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
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();