Skip to content

ContextRequestHandler

Turns a PSR-7 request into a response for one context.

This is the per-request work that used to sit on Context: owning the middleware pipeline, resolving the request’s correlation id, opening the ambient logging scope, arming the request-state flush, and emitting the last event that sees request and response together. None of it is about being a context — it is about serving a request against one — and a context that also handles requests is a context that cannot be asked “which profile am I” without also carrying a middleware pipeline.

Implements RequestHandlerInterface rather than merely resembling it, which Context::handle() always did without declaring it.

The pipeline is per handler, and therefore per context: a named context profile has its own middleware stack. It survives across requests within that context’s lifetime, which is safe because the pipeline itself holds no request state.

final class ContextRequestHandler implements RequestHandlerInterface

ImplementsRequestHandlerInterface
Since4.0.0
SourceRuntime/ContextRequestHandler.php

public function __construct(Context $context): mixed

ParameterTypeDescription
$contextContext

Returns mixed

MethodDescription
correlationId(): ?stringThis request’s correlation id, or null outside a handled request.
forgetPipeline(): voidDiscard the built pipeline so the next request builds a fresh one.
handle(ServerRequestInterface $request): ResponseInterfaceServes one request against this handler’s context.
hasPipeline(): boolWhether the pipeline has been built yet.
pipeline(): MiddlewarePipelineThe middleware pipeline for this context, built on first use.

public function correlationId(): ?string

This request’s correlation id, or null outside a handled request.

Returns ?``string

public function forgetPipeline(): void

Discard the built pipeline so the next request builds a fresh one.

The pipeline is composed from MiddlewareCatalog the first time it is needed and then reused for the context’s lifetime, which is the right trade for a worker but means a later change to the catalog would otherwise never be seen. Anything that reconfigures the catalog after a request has been served — a test replacing the core stack, a host reconfiguring middleware between runs — has to call this.

public function handle(ServerRequestInterface $request): ResponseInterface

Serves one request against this handler’s context.

Opens the request scope (correlation id, ambient logging scope, request-state flush), builds the WebRequest eagerly, exposes the correlation id on the request as the quiote.rid attribute and on the response as the configured header, and runs the middleware pipeline. ResponseSendingEvent is emitted last, with both request and response in hand.

ParameterTypeDescription
$requestServerRequestInterface

Returns ResponseInterface

public function hasPipeline(): bool

Whether the pipeline has been built yet.

Answers without building one, unlike ContextRequestHandler::pipeline().

Returns bool

public function pipeline(): MiddlewarePipeline

The middleware pipeline for this context, built on first use.

Returns MiddlewarePipeline