PHPackages                             leocavalcante/request-callback - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. leocavalcante/request-callback

ActiveLibrary[HTTP &amp; Networking](/categories/http)

leocavalcante/request-callback
==============================

Swoole request callback for PSR compliant handlers.

v0.1.1(5y ago)261.2k4[7 PRs](https://github.com/leocavalcante/request-callback/pulls)1MITPHPPHP &gt;=7.4

Since Jan 3Pushed 3y ago2 watchersCompare

[ Source](https://github.com/leocavalcante/request-callback)[ Packagist](https://packagist.org/packages/leocavalcante/request-callback)[ RSS](/packages/leocavalcante-request-callback/feed)WikiDiscussions main Synced 6d ago

READMEChangelog (2)Dependencies (8)Versions (11)Used By (1)

➰ Request Callback
==================

[](#-request-callback)

[![CI](https://github.com/leocavalcante/request-callback/workflows/CI/badge.svg)](https://github.com/leocavalcante/request-callback/workflows/CI/badge.svg)[![codecov](https://camo.githubusercontent.com/340e8e3d485dec1950c09ecaf4ace04bb462714f4ce4bbed089c60f1bb9cfc70/68747470733a2f2f636f6465636f762e696f2f67682f6c656f636176616c63616e74652f726571756573742d63616c6c6261636b2f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d5445315951594e4b484a)](https://codecov.io/gh/leocavalcante/request-callback)[![psalm](https://camo.githubusercontent.com/0a2680e0be45f82d24e3fb8d10bbc9f376baa80a1101c22b519858e4a878a895/68747470733a2f2f73686570686572642e6465762f6769746875622f6c656f636176616c63616e74652f726571756573742d63616c6c6261636b2f636f7665726167652e737667)](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
-------

[](#install)

```
composer require leocavalcante/request-callback
```

Usage
-----

[](#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).

```
use Swoole\Http\RequestCallback;

$callback = new RequestCallback(\Psr\Http\Server\RequestHandlerInterface);
```

### Example

[](#example)

#### Hello, World!

[](#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

[](#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();
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~18 days

Total

2

Last Release

1941d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/940717934113da7abe00589e87cfde6f34496f039bcd3fc6ce4a33f5f415d4ac?d=identicon)[leocavalcante](/maintainers/leocavalcante)

---

Top Contributors

[![kodiakhq[bot]](https://avatars.githubusercontent.com/in/29196?v=4)](https://github.com/kodiakhq[bot] "kodiakhq[bot] (119 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (102 commits)")[![leocavalcante](https://avatars.githubusercontent.com/u/183722?v=4)](https://github.com/leocavalcante "leocavalcante (44 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (42 commits)")

---

Tags

middlewarepsr-15psr-7request-handlerswoolehttprequestpsrserverhandlerswoolecallback

###  Code Quality

TestsPest

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/leocavalcante-request-callback/health.svg)

```
[![Health](https://phpackages.com/badges/leocavalcante-request-callback/health.svg)](https://phpackages.com/packages/leocavalcante-request-callback)
```

###  Alternatives

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

8.0k1.0B3.2k](/packages/guzzlehttp-psr7)[psr/http-server-handler

Common interface for HTTP server-side request handler

175101.3M921](/packages/psr-http-server-handler)[psr/http-server-middleware

Common interface for HTTP server-side middleware

18091.2M1.5k](/packages/psr-http-server-middleware)[middlewares/request-handler

Middleware to execute request handlers

451.6M26](/packages/middlewares-request-handler)[laminas/laminas-psr7bridge

Bidirectional conversions between PSR-7 and laminas-http messages

117.9M18](/packages/laminas-laminas-psr7bridge)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28137.8k](/packages/phpro-http-tools)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
