Skip to content

Context

An execution profile — web, console, a named one — and the container its services resolve from.

The context owns its own identity and lifecycle: it initializes the components the compiled factories configuration declares, binds them, arms and clears per-request state, and shuts them down in order. It does not hand out its collaborators; a class that needs the routing, the user or a service declares that in its constructor and the container supplies it. ContextInterface is what a collaborator should type-hint, and it is two methods wide for that reason.

A subclass named by core.context_implementation must keep the constructor signature: the registry builds it knowing only the profile name.

class Context implements ContextInterface, Stringable, ResetInterface

ImplementsContextInterface, Stringable, ResetInterface
Since1.0.0
SourceContext.php

protected function __construct(string $name): mixed

Constructor method, intentionally made protected so the context cannot be created directly.

The name of this context.

ParameterTypeDescription
$namestringThe name of this context.

Returns mixed

MethodDescription
__clone(): mixedClone method, overridden to prevent cloning, there can be only one.
__toString(): string__toString overload, returns the name of the Context.
beginRequest(): voidArm this context for a new request.
create(string $profile): staticBuild an uninitialized context for a profile.
flushRequestState(bool $persistUser = true): voidPersist request-scoped state that lives in the session.
getContainer(): ContainerThis profile’s container, built on first use.
getCorrelationId(): ?stringRetrieve current correlation ID (may be null outside a handled request).
getInstance(string $profile = null): ContextRetrieve the Context instance.
getLifecycle(): ContextLifecycleThis context’s per-request lifecycle — the flush claim and the end-of-request clears.
getModelLocator(): ModelLocatorRetrieve (lazily create) this context’s model locator.
getName(): stringRetrieve the name of this Context.
getRequestHandler(): RequestHandlerInterfaceThis context’s request handler, built on first use.
getShutdownSequence(): ShutdownSequenceThe components this context shuts down, in order.
initialize(): void(re)Initialize the Context instance.
reset(): voidEnds the request on this context and clears everything request-scoped.
resetWorkerState(?string $profile = null): voidReset every live context’s request-scoped state at a persistent worker’s request boundary, preserving each context’s configuration.
shutdown(): voidShut down this Context and all related factories.

public function __clone(): mixed

Clone method, overridden to prevent cloning, there can be only one.

Returns mixed

public function __toString(): string

__toString overload, returns the name of the Context.

Returns string — The context name.

public function beginRequest(): void

Arm this context for a new request.

Re-arms the per-request state flush so the next flushRequestState() actually runs. Called by the request handler on the way in; ContextLifecycle::endRequest() does it too, on the way out, and this covers a runtime that serves requests without a reset between them.

public static function create(string $profile): static

Build an uninitialized context for a profile.

The named constructor ContextRegistry uses. The registry is what guarantees one context per profile, so it needs a way in that the constructor’s protected visibility does not give it — but going through here rather than opening the constructor keeps new Context() from being written casually, since an unregistered context is not the one anything else in the process will find.

initialize() is deliberately not called: the registry has to record the instance before initialization runs, so a context reaching back for its own profile mid-initialize finds itself instead of recursing into a second one.

ParameterTypeDescription
$profilestring

Returns static

public function flushRequestState(bool $persistUser = true): void

Persist request-scoped state that lives in the session.

False for a sessionless request (auth.sessionless / jwt.skip_session): there is no session to persist into, and writing a token-derived identity into whatever unrelated session cookie the client still carries would be wrong. The flush is still claimed, so the post-emit reset() does not attempt a late write.

ParameterTypeDescription
$persistUserboolFalse for a sessionless request (auth.sessionless / jwt.skip_session): there is no session to persist into, and writing a token-derived identity into whatever unrelated session cookie the client still carries would be wrong. The flush is still claimed, so the post-emit reset() does not attempt a late write.

public function getContainer(): Container

This profile’s container, built on first use.

Every component the compiled factories configuration declares is bound here under both its role name and its concrete class name, so get(\Quiote\User\User::class) and get(RbacSecurityUser::class) answer the same instance.

Returns Container

public function getCorrelationId(): ?string

Retrieve current correlation ID (may be null outside a handled request).

Returns ?``string

public static function getInstance(string $profile = null): Context

Retrieve the Context instance.

A name corresponding to a section of the config

ParameterTypeDescription
$profilestringA name corresponding to a section of the config

Returns Context — An context instance initialized with the settings of the requested context name

public function getLifecycle(): ContextLifecycle

This context’s per-request lifecycle — the flush claim and the end-of-request clears.

Exposed so a host that drives the context itself can register a clear of its own without going through the plugin registry, and so what a context clears is assertable.

Returns ContextLifecycle

public function getModelLocator(): ModelLocator

Retrieve (lazily create) this context’s model locator.

The locator owns model resolution and model lifetimes; the context only owns the fact that there is one per context. Constructor-inject ModelLocator in new code.

Returns ModelLocator

public function getName(): string

Retrieve the name of this Context.

Returns string — A context name.

public function getRequestHandler(): RequestHandlerInterface

This context’s request handler, built on first use.

Declared as the PSR contract rather than as ContextRequestHandler: every caller outside the handler’s own tests wants nothing but handle(), and a runtime that serves a context through a handler of its own is then wiring, not a subclass.

Returns RequestHandlerInterface

public function getShutdownSequence(): ShutdownSequence

The components this context shuts down, in order.

The generated factory cache installs the sequence through here, and the lazy component recreation paths splice replacements back into it.

Returns ShutdownSequence

public function initialize(): void

(re)Initialize the Context instance.

public function reset(): void

Ends the request on this context and clears everything request-scoped.

Resets the model locator and the controller, flushes any user state that the middleware did not already persist, and recycles the database manager’s connections instead of shutting it down, so the manager survives into the next request. The registered lifecycle clears — session bag, user, request, logging scopes, request-scoped container entries and the registered resettable instances — run in a finally, so a throw anywhere above cannot leave request N’s authenticated user installed for request N+1.

public static function resetWorkerState(?string $profile = null): void

Reset every live context’s request-scoped state at a persistent worker’s request boundary, preserving each context’s configuration.

The profile that served the request; it is reset first, but every other live context is reset too.

ParameterTypeDescription
$profile?``stringThe profile that served the request; it is reset first, but every other live context is reset too.

public function shutdown(): void

Shut down this Context and all related factories.