Skip to content

WorkerLoop

Everything a worker runtime needs from the framework, so a runtime only has to know how to get a request in and a response out.

The compensations for leaving the SAPI behind all live here rather than being re-implemented per runtime, gated on WorkerRuntimeCapabilities: superglobal hydration, stray-output capture, native session-cookie synthesis. A runtime that reports SAPI-shaped capabilities pays for none of it.

final class WorkerLoop

SourceRuntime/Worker/WorkerLoop.php

public function __construct(Context $context, WorkerRequestFactory $requestFactory, SuperglobalBridge $superglobals, OutputCapture $output, ErrorResponseFactory $errors, WorkerRuntimeCapabilities $capabilities, int $maxRequests = 0): mixed

ParameterTypeDescription
$contextContext
$requestFactoryWorkerRequestFactory
$superglobalsSuperglobalBridge
$outputOutputCapture
$errorsErrorResponseFactory
$capabilitiesWorkerRuntimeCapabilities
$maxRequestsint

Returns mixed

MethodDescription
afterRequest(): voidRequest boundary: clear anything that must not leak into the next request.
bootWorker(): voidPost-fork / first-request-in-this-process hook.
capabilities(): WorkerRuntimeCapabilitiesReturns the hosting runtime’s capabilities, as handed to the constructor.
handle(ServerRequestInterface $request): ResponseInterfaceRuns one request through the pipeline.
renderError(Throwable $e, ?ServerRequestInterface $request = null): ResponseInterfaceFor a runtime that catches a protocol-level throwable of its own.
requestFromGlobals(): ServerRequestInterfaceFor SAPI-shaped runtimes, which have no request object of their own.
requestsHandled(): intReturns how many requests this loop has taken in.
shouldContinue(): boolFalse once the max-requests budget is spent; always true when unlimited.
shutdown(): voidGraceful stop.

public function afterRequest(): void

Request boundary: clear anything that must not leak into the next request.

public function bootWorker(): void

Post-fork / first-request-in-this-process hook.

Idempotent, so a runtime that doesn’t fork can call it unconditionally before its loop.

A forking runtime (Swoole) starts its children after bootstrap has already built the Context and possibly opened database sockets, so every child would inherit the same connections and interleave on the wire.

public function capabilities(): WorkerRuntimeCapabilities

Returns the hosting runtime’s capabilities, as handed to the constructor.

Returns WorkerRuntimeCapabilities

public function handle(ServerRequestInterface $request): ResponseInterface

Runs one request through the pipeline.

Never throws: a throwable that escapes becomes an error response, so a persistent worker survives a broken request instead of dying with the pool.

ParameterTypeDescription
$requestServerRequestInterface

Returns ResponseInterface

public function renderError(Throwable $e, ?ServerRequestInterface $request = null): ResponseInterface

For a runtime that catches a protocol-level throwable of its own.

ParameterTypeDescription
$eThrowable
$request?ServerRequestInterface

Returns ResponseInterface

public function requestFromGlobals(): ServerRequestInterface

For SAPI-shaped runtimes, which have no request object of their own.

Returns ServerRequestInterface

public function requestsHandled(): int

Returns how many requests this loop has taken in.

Counted at the start of WorkerLoop::handle(), so a request still in flight is already included, and a failed one still counts against the max-requests budget.

Returns int

public function shouldContinue(): bool

False once the max-requests budget is spent; always true when unlimited.

Returns bool

public function shutdown(): void

Graceful stop.