PSR Interoperability
pushinbr/pam-http-psr lets standard PHP-FIG code run on PAM. It provides PSR-7 message implementations, PSR-17
factories and a PSR-15 pipeline. When an application registers a PSR-15 request handler, PAM converts each native
request into a PSR-7 ServerRequestInterface, runs your PSR-15 middleware and handler, and converts the
ResponseInterface back.
Use it to reuse existing PSR-15 middleware and handlers, or to keep framework-neutral code portable between PAM and other runtimes.
Installation
Section titled “Installation”pam composer require pushinbr/pam-http-psrThe package requires PHP 8.4 and the official interfaces psr/http-message ^2.0, psr/http-factory ^1.1,
psr/http-server-handler ^1.0 and psr/http-server-middleware ^1.0, which Composer installs for you.
Serving a PSR-15 Handler
Section titled “Serving a PSR-15 Handler”Register the handler with handler() and PSR-15 middleware with middleware(). The example uses
Pam\App; Nimbus exposes the same two methods.
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use Pam\App;use Pam\Http\Psr7\Factory;use Psr\Http\Message\ResponseInterface;use Psr\Http\Message\ServerRequestInterface;use Psr\Http\Server\MiddlewareInterface;use Psr\Http\Server\RequestHandlerInterface;
final class HelloHandler implements RequestHandlerInterface{ public function __construct(private Factory $factory = new Factory()) {}
public function handle(ServerRequestInterface $request): ResponseInterface { $name = $request->getQueryParams()['name'] ?? 'world';
return $this->factory->createResponse(200) ->withHeader('content-type', 'text/plain') ->withBody($this->factory->createStream("Hello, {$name}!")); }}
final class RequestIdMiddleware implements MiddlewareInterface{ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $id = bin2hex(random_bytes(4));
return $handler ->handle($request->withAttribute('request-id', $id)) ->withHeader('x-request-id', $id); }}
$app = new App();$app->middleware(new RequestIdMiddleware());$app->handler(new HelloHandler());$app->listen((int) (getenv('PAM_PORT') ?: 3000));pam dev index.phpcurl -i 'http://127.0.0.1:3000/anything?name=Alice'# HTTP/1.1 200 OK# x-request-id: 8943e27a# content-type: text/plain## Hello, Alice!The handler can also be a callable that receives a ServerRequestInterface and returns a ResponseInterface.
Middleware runs in registration order around the handler.
What the Request Contains
Section titled “What the Request Contains”PAM builds the ServerRequest from the native request: the method, an absolute URI from the Host header and the
request target, all headers, the body as a stream, $_SERVER as server params, query params, cookies, the parsed
body for form submissions and uploaded files as UploadedFileInterface objects.
Mixing With PAM HTTP Routes
Section titled “Mixing With PAM HTTP Routes”A PSR-15 handler takes over the listener. When handler() is set, listen() serves every request through the
PSR-15 pipeline and the application’s own routes and PAM middleware are not used. PSR-15 middleware registered with
middleware() runs only in front of that handler.
To combine both styles, keep the PAM HTTP application and call PSR-15 components from a route handler with the
Pipeline class described below.
Building Messages
Section titled “Building Messages”The Pam\Http\Psr7 namespace contains the implementations. Type your code against the PSR interfaces and create
instances through the factory:
use Pam\Http\Psr7\Factory;
$factory = new Factory();
$request = $factory->createServerRequest('POST', 'https://api.example.com/orders') ->withHeader('content-type', 'application/json') ->withBody($factory->createStream('{"sku":"ABC-1"}'));
$response = $factory->createResponse(201)->withHeader('location', '/orders/42');Factory implements every PSR-17 interface: RequestFactoryInterface, ResponseFactoryInterface,
ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface and
UriFactoryInterface.
Messages are immutable: with*() methods return a modified clone.
Running PSR-15 Pipelines Yourself
Section titled “Running PSR-15 Pipelines Yourself”Pam\Http\Psr15\Pipeline is a RequestHandlerInterface that runs middleware before a destination handler. It
accepts MiddlewareInterface objects or callables with the signature (ServerRequestInterface, RequestHandlerInterface):
use Pam\Http\Psr15\Pipeline;
$pipeline = new Pipeline(new HelloHandler(), [ new RequestIdMiddleware(),]);
$response = $pipeline->handle($request);This is handy in unit tests: you exercise PSR-15 components without starting a server.
Classes
Section titled “Classes”| Class | Implements |
|---|---|
Pam\Http\Psr7\Stream |
StreamInterface; wraps a string or a stream resource. |
Pam\Http\Psr7\Uri |
UriInterface. |
Pam\Http\Psr7\Message |
Abstract base for headers, protocol version and body. |
Pam\Http\Psr7\ServerRequest |
ServerRequestInterface: server params, cookies, query, uploads, parsed body and attributes. |
Pam\Http\Psr7\Request |
A ServerRequest used for client-side requests. |
Pam\Http\Psr7\Response |
ResponseInterface; __construct(int $statusCode = 200, array $headers = [], StreamInterface|string $body = '', string $reasonPhrase = ''). |
Pam\Http\Psr7\UploadedFile |
UploadedFileInterface. |
Pam\Http\Psr7\Factory |
All PSR-17 factories. |
Pam\Http\Psr15\Pipeline |
RequestHandlerInterface; __construct(RequestHandlerInterface|Closure $handler, array $middleware = []). |
The classes are declared only when the matching PSR interfaces are installed, so the package is safe to load in any project.
Limits and Caveats
Section titled “Limits and Caveats”- The response body is read into memory when the handler returns. For streaming or server-sent events, use the native PAM response instead.
- PSR-15 middleware is synchronous. The bridge does not make blocking code cooperative.
- Middleware written for PHP-FPM may keep state in static properties or singletons. In a persistent worker that state survives between requests; review third-party middleware before using it.
- Close or detach streams you open, and do not rely on uploaded files after the request ends.
pushinbr/pam-http-psr is licensed under the Apache License 2.0.