All Projects → leocavalcante → request-callback

leocavalcante / request-callback

Licence: MIT license
➰ Swoole request callback for PSR compliant handlers.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to request-callback

chubbyphp-swoole-request-handler
A request handler adapter for swoole, using PSR-7, PSR-15 and PSR-17.
Stars: ✭ 22 (+0%)
Mutual labels:  swoole, request-handler
swoole-ide-helper
💪 IDE helper for Swoft and Swoole
Stars: ✭ 42 (+90.91%)
Mutual labels:  swoole
swoole
Swoole Api references
Stars: ✭ 14 (-36.36%)
Mutual labels:  swoole
app
Buggregator is a beautiful, lightweight debug server build on Laravel that helps you catch your smpt, sentry, var-dump, monolog, ray outputs. It runs without installation on multiple platforms.
Stars: ✭ 259 (+1077.27%)
Mutual labels:  swoole
awesome-swoole
💎 A curated list of awesome things related to Swoole.
Stars: ✭ 129 (+486.36%)
Mutual labels:  swoole
framework
🦋The PHP Framework For Code Poem As Free As Wind.
Stars: ✭ 65 (+195.45%)
Mutual labels:  swoole
http-emitter
📡 Emitting psr-7 responses.
Stars: ✭ 31 (+40.91%)
Mutual labels:  swoole
ultraman
奥特曼
Stars: ✭ 45 (+104.55%)
Mutual labels:  swoole
homebrew-extensions
🍻 Homebrew tap for PHP extensions
Stars: ✭ 264 (+1100%)
Mutual labels:  swoole
gotask
⚡️ A lightning speed replacement for Swoole TaskWorker in Go
Stars: ✭ 164 (+645.45%)
Mutual labels:  swoole
swoole-laravel-chat
Simple stateless chat board [Swoole + SocketIO + Laravel]
Stars: ✭ 16 (-27.27%)
Mutual labels:  swoole
static-php-cli
Build single static PHP binary in linux, build with PHP project together, with Swoole and other popular extensions included.
Stars: ✭ 129 (+486.36%)
Mutual labels:  swoole
kafka-swoole
☝️Implement all kafka protocols, providing 'HighLevel' and 'LowLevel' client apis respectively, and utilize swoole to realize collaboration and flexibly extend consumers' client, which is the first php-kafka client to support multiple compression forms
Stars: ✭ 33 (+50%)
Mutual labels:  swoole
docs
Lawoole documentation
Stars: ✭ 12 (-45.45%)
Mutual labels:  swoole
ext-postgresql
🐘 Coroutine-based client for PostgreSQL
Stars: ✭ 62 (+181.82%)
Mutual labels:  swoole
http
Support Swoole Implement PSR7
Stars: ✭ 28 (+27.27%)
Mutual labels:  swoole
MineAdmin
🎉🌈🔥MineAdmin是基于Hyperf框架 和 Vue3+Vite3 开发的前后端分离权限管理系统,自适应多终端。特色:crud生成 + json配置化开发一把梭
Stars: ✭ 142 (+545.45%)
Mutual labels:  swoole
RidPT
A Private Tracker System
Stars: ✭ 102 (+363.64%)
Mutual labels:  swoole
zhamao-framework
协程、高性能、灵活的聊天机器人 & Web 开发框架(炸毛框架)
Stars: ✭ 99 (+350%)
Mutual labels:  swoole
vertica-swoole-adapter
Provides a DB layer for Swoole-based applications to communicate to HP Vertica databases.
Stars: ✭ 14 (-36.36%)
Mutual labels:  swoole

Request Callback

CI codecov psalm

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();
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].