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.
Synopsis
Section titled “Synopsis”final class WorkerLoop
| Source | Runtime/Worker/WorkerLoop.php |
Constructor
Section titled “Constructor”__construct()
Section titled “__construct()”public function __construct(Context $context, WorkerRequestFactory $requestFactory, SuperglobalBridge $superglobals, OutputCapture $output, ErrorResponseFactory $errors, WorkerRuntimeCapabilities $capabilities, int $maxRequests = 0): mixed
| Parameter | Type | Description |
|---|---|---|
$context | Context | |
$requestFactory | WorkerRequestFactory | |
$superglobals | SuperglobalBridge | |
$output | OutputCapture | |
$errors | ErrorResponseFactory | |
$capabilities | WorkerRuntimeCapabilities | |
$maxRequests | int |
Returns mixed
Methods
Section titled “Methods”| Method | Description |
|---|---|
afterRequest(): void | Request boundary: clear anything that must not leak into the next request. |
bootWorker(): void | Post-fork / first-request-in-this-process hook. |
capabilities(): WorkerRuntimeCapabilities | Returns the hosting runtime’s capabilities, as handed to the constructor. |
handle(ServerRequestInterface $request): ResponseInterface | Runs one request through the pipeline. |
renderError(Throwable $e, ?ServerRequestInterface $request = null): ResponseInterface | For a runtime that catches a protocol-level throwable of its own. |
requestFromGlobals(): ServerRequestInterface | For SAPI-shaped runtimes, which have no request object of their own. |
requestsHandled(): int | Returns how many requests this loop has taken in. |
shouldContinue(): bool | False once the max-requests budget is spent; always true when unlimited. |
shutdown(): void | Graceful stop. |
afterRequest()
Section titled “afterRequest()”public function afterRequest(): void
Request boundary: clear anything that must not leak into the next request.
bootWorker()
Section titled “bootWorker()”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.
capabilities()
Section titled “capabilities()”public function capabilities(): WorkerRuntimeCapabilities
Returns the hosting runtime’s capabilities, as handed to the constructor.
Returns WorkerRuntimeCapabilities
handle()
Section titled “handle()”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.
| Parameter | Type | Description |
|---|---|---|
$request | ServerRequestInterface |
Returns ResponseInterface
renderError()
Section titled “renderError()”public function renderError(Throwable $e, ?ServerRequestInterface $request = null): ResponseInterface
For a runtime that catches a protocol-level throwable of its own.
| Parameter | Type | Description |
|---|---|---|
$e | Throwable | |
$request | ?ServerRequestInterface |
Returns ResponseInterface
requestFromGlobals()
Section titled “requestFromGlobals()”public function requestFromGlobals(): ServerRequestInterface
For SAPI-shaped runtimes, which have no request object of their own.
Returns ServerRequestInterface
requestsHandled()
Section titled “requestsHandled()”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
shouldContinue()
Section titled “shouldContinue()”public function shouldContinue(): bool
False once the max-requests budget is spent; always true when unlimited.
Returns bool
shutdown()
Section titled “shutdown()”public function shutdown(): void
Graceful stop.