response-interop / impl
Reference implementations of the Response-Interop interfaces.
Requires
- php: >=8.4
- response-interop/interface: ^1.1
Requires (Dev)
- pds/composer-script-names: ^1.0
- pds/skeleton: ^1.0
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^11.0
- pmjones/php-styler: 0.x@dev
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-28 20:18:15 UTC
README
Reference implementations of the Response-Interop interfaces for PHP 8.4+.
Installation
Install this package via Composer:
composer require response-interop/impl
Usage
Build a ResponseStruct, populate it, and hand it to a ResponseSenderService:
use ResponseInterop\Impl\Response; use ResponseInterop\Impl\ResponseSender; $response = new Response(); $response->headers->setHeader('content-type', 'text/plain'); $response->body = 'Hello, world!'; (new ResponseSender())->sendResponse($response);
Every Response property is also a promoted constructor argument; its defaults
are:
use ResponseInterop\Impl\Response; use ResponseInterop\Impl\ResponseHeaders; $response = new Response( httpVersion: '1.1', statusCode: 200, headers: new ResponseHeaders(), body: '', );
Code typed against the interfaces can obtain a new response from a
ResponseStructFactory instead. ResponseFactory returns a new Response,
with its own ResponseHeaders, on every call:
use ResponseInterop\Impl\ResponseFactory; $response = (new ResponseFactory())->newResponse();
ResponseSender writes to php://output by default. Pass any writable
resource to send elsewhere, such as a buffer to capture output in tests:
use ResponseInterop\Impl\Response; use ResponseInterop\Impl\ResponseSender; $buffer = fopen('php://memory', 'wb+'); /** @var Response $response */ (new ResponseSender($buffer))->sendResponse($response); rewind($buffer); $sent = stream_get_contents($buffer);
Set a cookie:
use ResponseInterop\Impl\Response; /** @var Response $response */ $response->headers->setCookie('session', 'abc123', [ 'path' => '/', 'httponly' => true, 'samesite' => 'Lax', ]);
setCookie() lower-cases attribute names, as parsing a set-cookie string
does, so 'SameSite' would be kept and sent as samesite.
The body accepts a string, any Stringable, or a
ResponseBodyHandler that prepares its own headers and streams its own
content. This package ships two handlers; implement the interface for your own.
Send a JSON body:
use ResponseInterop\Impl\Response; use ResponseInterop\Impl\JsonResponseBody; /** @var Response $response */ $response->body = new JsonResponseBody(['hello' => 'world']);
The optional arguments customize the output:
use ResponseInterop\Impl\Response; use ResponseInterop\Impl\JsonResponseBody; /** @var Response $response */ $response->body = new JsonResponseBody( data: ['hello' => 'world'], type: 'application/hal+json', flags: JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR, depth: 512, );
$type sets the content-type (default application/json); $flags and
$depth pass through to json_encode(), defaulting to an HTML-safe flag set
with JSON_THROW_ON_ERROR and a depth of 512. Passing $flags replaces the
default set entirely; re-include JSON_THROW_ON_ERROR and the HTML-safe flags
to keep them. The data is encoded when the response is prepared, before any
header goes out; with JSON_THROW_ON_ERROR, an encoding failure throws a
ResponseException whose previous exception is the JsonException.
Send a file:
use ResponseInterop\Impl\Response; use ResponseInterop\Impl\FileResponseBody; /** @var Response $response */ $response->body = new FileResponseBody(new SplFileObject('/path/to/file.pdf'));
The optional arguments set the response headers, defaulting from the file itself:
use ResponseInterop\Impl\Response; use ResponseInterop\Impl\FileResponseBody; /** @var Response $response */ $response->body = new FileResponseBody( file: new SplFileObject('/path/to/report.pdf'), type: 'application/pdf', encoding: 'binary', disposition: 'attachment', filename: 'report.pdf', );
$type sets the content-type (default application/octet-stream),
$encoding the content-transfer-encoding (default binary), and
$disposition/$filename the content-disposition (default attachment
using the file's own name). The name goes into filename as plain ASCII, with
_ in place of any other character and of %, ", and \; when that changes
the name, a UTF-8 name also goes into an RFC 6266 filename*. A
content-length header is added from the file size, and no more than that is
sent; a file that has shrunk since then causes a ResponseException.
Classes
| Interface | Implementation |
|---|---|
| ResponseStruct | Response |
| ResponseStructFactory | ResponseFactory |
| ResponseHeadersCollection | ResponseHeaders |
| ResponseBodyHandler | FileResponseBody, JsonResponseBody |
| ResponseCookieHelperService | ResponseCookieHelper |
| ResponseSenderService | ResponseSender |
| ResponseBodySenderService | ResponseSender |
| ResponseThrowable | ResponseException |
All classes are in the ResponseInterop\Impl namespace.
Errors
Every exception thrown by this package implements the ResponseThrowable marker
interface. Catch that instead of the concrete ResponseException for
portability across implementations:
use ResponseInterop\Impl\Response; use ResponseInterop\Interface\ResponseThrowable; try { /** @var Response $response */ $response->headers->setHeader('content-type', ''); } catch (ResponseThrowable $e) { // handle the blank header value }
ResponseHeaders throws on an invalid or empty header field name, or on
setting a status field; a blank header value, or one containing CR, LF, or
NUL; a cookie name that setcookie() would reject, whether passed to
setCookie() or in a set-cookie header; an invalid cookie attribute name or
value; or an unparseable set-cookie string. ResponseCookieHelper throws
when composing a cookie whose name setcookie() would reject, whose attribute
name or value would inject further attributes, or whose attribute value is
neither a string nor true. ResponseSender throws on a content or output
resource that is closed or is not a stream; on a write failure, including a
write that stops partway; and, when sending a body from a stream resource, on a
negative length or offset, a read failure, or an offset it cannot reach (such
as one behind the pointer of a resource that cannot seek). JsonResponseBody
throws on data it cannot encode (with JSON_THROW_ON_ERROR), and
FileResponseBody on a file whose size it cannot get or that it cannot open.
Write, read, and open failures throw a ResponseException even under an error
handler that turns PHP warnings into exceptions.
ResponseSender sends the status line after the headers, so the response's
own statusCode is what goes out. PHP's header() would otherwise change it
when it sees location or www-authenticate; a response that redirects needs
to set its own redirect status. For the same reason, ResponseHeaders does not
accept a status field, which under CGI and FPM takes the place of the status
line.
ResponseSender sends the body it prepared. If a handler's prepareResponse()
puts a different body on the response, that replacement is not sent; a
handler that needs to send something else, such as nothing after setting a
304, can change its own output instead.
See the Response-Interop interface package for the full specification.