Skip to content

ContextRegistry

Owns the live Context instances — one per named profile.

A context profile is a configuration identity (web, console, a named profile), and something has to guarantee there is exactly one context per identity for the life of the process. That guarantee was a static map on Context itself, which made Context responsible both for being a context and for knowing about all the others. This is the second half, on its own.

Constructor-inject this to reach a context by name. Context::getInstance() answers from ContextRegistry::shared(), so both reach the same instances.

final class ContextRegistry

Since4.0.0
SourceContextRegistry.php
MethodDescription
clear(): voidForget every live context without resetting them.
get(?string $profile = null, ?class-string<Context> $fallbackClass = null): ContextRetrieve the context for a profile, initializing it on first request.
has(?string $profile = null): boolWhether a profile has a live context.
names(): array<int, string>The profile names of every live context.
resetAll(?string $preferred = null): voidReset every live context at a worker request boundary.
shared(): ContextRegistryThe process-wide registry.

public function clear(): void

Forget every live context without resetting them.

For tests that need a clean process-level slate. Not a request-boundary operation — ContextRegistry::resetAll() is, and dropping contexts there would rebuild the whole configuration on every request.

public function get(?string $profile = null, ?class-string<Context> $fallbackClass = null): Context

Retrieve the context for a profile, initializing it on first request.

The implementation to build when core.context_implementation is unset. Null means Context itself. This is how Context::getInstance() keeps its late-static-binding behaviour: SubContext::getInstance() builds a SubContext without needing the setting.

ParameterTypeDescription
$profile?``stringA profile name, or null for core.default_context.
$fallbackClass?``class-string``<Context>The implementation to build when core.context_implementation is unset. Null means Context itself. This is how Context::getInstance() keeps its late-static-binding behaviour: SubContext::getInstance() builds a SubContext without needing the setting.

Returns Context

ThrowsWhen
ExceptionWhatever construction or initialize() raised. Bootstrap runs before any PSR-15 pipeline exists, so there is no ErrorHandlingMiddleware to hand a failure to; it is logged here and propagated rather than rendered.

public function has(?string $profile = null): bool

Whether a profile has a live context.

Does not create one — this is for callers that need to act on what exists rather than bring it into being.

ParameterTypeDescription
$profile?``string

Returns bool

public function names(): array<int, string>

The profile names of every live context.

Returns array``<``int``, ``string``>

public function resetAll(?string $preferred = null): void

Reset every live context at a worker request boundary.

The profile that served the request, reset first.

ParameterTypeDescription
$preferred?``stringThe profile that served the request, reset first.

public static function shared(): ContextRegistry

The process-wide registry.

Built on first use.

Returns ContextRegistry