Skip to content

Contracts

pushinbr/pam-contracts holds the interfaces that let packages extend PAM without depending on a specific framework. A middleware package written against these contracts works with PAM HTTP, Nimbus and any other application that implements them. The package contains no router, server or application code.

Most applications receive it as a dependency of another package. Install it explicitly when you publish a reusable PAM extension.

Terminal window
pam composer require pushinbr/pam-contracts

The package requires PHP 8.4 and has no other dependencies.

A middleware receives the PAM request and response and the next handler, and must return a response:

src/RequestIdMiddleware.php
namespace Acme\RequestId;
use Pam\Contracts\Http\MiddlewareInterface;
use Pam\Contracts\Http\RequestHandlerInterface;
use Pam\Http\Request;
use Pam\Http\Response;
final readonly class RequestIdMiddleware implements MiddlewareInterface
{
public function process(Request $request, Response $response, RequestHandlerInterface $next): Response
{
$id = $request->getHeader('x-request-id') ?? bin2hex(random_bytes(8));
return $next->handle($request, $response)->header('x-request-id', $id);
}
}

RequestHandlerInterface has a single method, handle(Request $request, Response $response): Response.

ApplicationInterface is the minimum an application exposes to packages:

interface ApplicationInterface
{
public function route(string $method, string $path, callable $handler): self;
public function middleware(object|callable $middleware): self;
public function onError(callable $handler): self;
public function handle(Request $request, Response $response): Response;
}

Depend on it when your package registers routes or middleware without requiring a particular framework. pushinbr/pam-http-testing uses handle() to test any application in memory.

Packages integrate through providers:

src/RequestIdServiceProvider.php
namespace Acme\RequestId;
use Pam\Contracts\Http\ApplicationInterface;
use Pam\Contracts\Package\ServiceProviderInterface;
final readonly class RequestIdServiceProvider implements ServiceProviderInterface
{
public function register(ApplicationInterface $application): void
{
$application->middleware(new RequestIdMiddleware());
}
public function boot(ApplicationInterface $application): void
{
}
}

register() runs while the application is still mutable; use it to add routes, middleware and bindings. boot() runs once after every provider is registered. Neither method should do per-request work.

Applications built with PAM HTTP discover providers that a package lists in its composer.json:

composer.json
{
"name": "acme/pam-request-id",
"require": { "pushinbr/pam-contracts": "^1.1" },
"autoload": { "psr-4": { "Acme\\RequestId\\": "src/" } },
"extra": {
"pam": { "providers": ["Acme\\RequestId\\RequestIdServiceProvider"] }
}
}

See Package discovery.

A package that needs a native runtime feature should check for it during boot, not after it starts receiving traffic:

use Pam\Contracts\Runtime\RuntimeCompatibility;
use Pam\Native\Capability;
RuntimeCompatibility::discover()->assert([
Capability::WebSocket,
]);

discover() reads the runtime’s ABI version and capability list; it throws when PHP is not running inside PAM. assert() throws when the ABI version differs from the one the runtime classes expect, or when a requested capability is missing. pushinbr/pam-socket uses exactly this check.

The runtime exposes these capabilities through the integer-backed Pam\Native\Capability enum:

Value Case Value Case
1 Timer 7 HttpStreaming
2 StreamReadiness 8 WebSocket
3 Dns 9 Http3
4 Filesystem 10 SharedTables
5 Process 11 TaskWorkers
6 Signal

Pam\Contracts\Runtime\Stability labels contract maturity: Experimental = 1, Stable = 2, Deprecated = 3. Compare enum cases, never raw numbers.

  • Depend on the smallest contract you need, and declare the minimum PAM version and capabilities you require.
  • Check capabilities at boot.
  • Keep provider registration deterministic and free of I/O where possible.
  • Never keep request objects in providers, singletons or static properties.
  • Identifiers you publish in protocols and enums should be integers, appended in order and never reused.
Namespace Type Members
Pam\Contracts\Http ApplicationInterface route(), middleware(), onError(), handle()
Pam\Contracts\Http MiddlewareInterface process(Request $request, Response $response, RequestHandlerInterface $next): Response
Pam\Contracts\Http RequestHandlerInterface handle(Request $request, Response $response): Response
Pam\Contracts\Package ServiceProviderInterface register(ApplicationInterface), boot(ApplicationInterface)
Pam\Contracts\Runtime RuntimeCompatibility Readonly abiVersion, capabilities; static discover(), assert(array $required = [])
Pam\Contracts\Runtime Stability Experimental 1, Stable 2, Deprecated 3

pushinbr/pam-contracts is licensed under the Apache License 2.0.