class CoroutineContext

Properties

static protected TValue> $nonCoroutineContext

Methods

static mixed
set(UnitEnum|string $id, mixed $value, int|null $coroutineId = null)

Store a value in the current context.

static mixed
get(UnitEnum|string $id, mixed $default = null, int|null $coroutineId = null)

Retrieve a value from the current context.

static bool
has(UnitEnum|string $id, int|null $coroutineId = null)

Determine if a value exists in the current context.

static void
forget(UnitEnum|string $id, int|null $coroutineId = null)

Remove a value from the current context.

static void
copyFrom(int $fromCoroutineId, array $keys = [])

Copy context from another coroutine into the current coroutine.

static array
captureFrom(array $keys = [], int|null $fromCoroutineId = null)

Capture context values as an array.

static mixed
override(UnitEnum|string $id, Closure $closure, int|null $coroutineId = null)

Retrieve a value and replace it with the result of a closure.

static mixed
getOrSet(UnitEnum|string $id, mixed $value, int|null $coroutineId = null)

Retrieve the value and store it if not exists.

static void
setMany(array $values, int|null $coroutineId = null)

Set multiple key-value pairs in the context.

static void
copyFromNonCoroutine(array $keys = [], int|null $coroutineId = null)

Copy context data from non-coroutine context to the specified coroutine context.

static void
copyToNonCoroutine(array $keys = [], int|null $coroutineId = null)

Copy context data from the specified coroutine context to non-coroutine context.

static mixed
getFromNonCoroutine(UnitEnum|string $id, mixed $default = null)

Get a value from non-coroutine context only.

static void
clearFromNonCoroutine(array $keys)

Clear specific keys from non-coroutine context only.

static void
flush(int|null $coroutineId = null)

Flush all context data for the specified coroutine.

static array|ArrayObject|null
getContainer(int|null $coroutineId = null)

Get the raw context storage for the current or specified coroutine.

Details

at line 33
static mixed set(UnitEnum|string $id, mixed $value, int|null $coroutineId = null)

Store a value in the current context.

Parameters

UnitEnum|string $id
mixed $value
int|null $coroutineId

Return Value

mixed

Exceptions

CoroutineDestroyedException

at line 55
static mixed get(UnitEnum|string $id, mixed $default = null, int|null $coroutineId = null)

Retrieve a value from the current context.

Parameters

UnitEnum|string $id
mixed $default
int|null $coroutineId

Return Value

mixed

at line 74
static bool has(UnitEnum|string $id, int|null $coroutineId = null)

Determine if a value exists in the current context.

Parameters

UnitEnum|string $id
int|null $coroutineId

Return Value

bool

at line 91
static void forget(UnitEnum|string $id, int|null $coroutineId = null)

Remove a value from the current context.

Parameters

UnitEnum|string $id
int|null $coroutineId

Return Value

void

at line 109
static void copyFrom(int $fromCoroutineId, array $keys = [])

Copy context from another coroutine into the current coroutine.

Merges into the current coroutine's context — existing values that are not in the source are preserved. Matching keys are overwritten.

Parameters

int $fromCoroutineId
array $keys

Return Value

void

at line 124
static array captureFrom(array $keys = [], int|null $fromCoroutineId = null)

Capture context values as an array.

Replicable values are copied in the calling coroutine at capture time.

Parameters

array $keys
int|null $fromCoroutineId

Return Value

array

at line 151
static mixed override(UnitEnum|string $id, Closure $closure, int|null $coroutineId = null)

Retrieve a value and replace it with the result of a closure.

Parameters

UnitEnum|string $id
Closure $closure
int|null $coroutineId

Return Value

mixed

at line 173
static mixed getOrSet(UnitEnum|string $id, mixed $value, int|null $coroutineId = null)

Retrieve the value and store it if not exists.

Parameters

UnitEnum|string $id
mixed $value
int|null $coroutineId

Return Value

mixed

at line 185
static void setMany(array $values, int|null $coroutineId = null)

Set multiple key-value pairs in the context.

Parameters

array $values
int|null $coroutineId

Return Value

void

at line 216
static void copyFromNonCoroutine(array $keys = [], int|null $coroutineId = null)

Copy context data from non-coroutine context to the specified coroutine context.

Merges into the target coroutine's context — existing values that are not in the source are preserved. Matching keys are overwritten.

Parameters

array $keys
int|null $coroutineId

Return Value

void

at line 242
static void copyToNonCoroutine(array $keys = [], int|null $coroutineId = null)

Copy context data from the specified coroutine context to non-coroutine context.

Tests only. This copies coroutine-local values into worker-global storage, so request-time use can leak or race across concurrent requests.

Parameters

array $keys
int|null $coroutineId

Return Value

void

at line 271
static mixed getFromNonCoroutine(UnitEnum|string $id, mixed $default = null)

Get a value from non-coroutine context only.

Unlike get() which reads from coroutine context when inside a coroutine, this always reads from non-coroutine storage regardless of current context.

Parameters

UnitEnum|string $id
mixed $default

Return Value

mixed

at line 284
static void clearFromNonCoroutine(array $keys)

Clear specific keys from non-coroutine context only.

Tests only. This mutates worker-global storage, so request-time use can race with concurrent requests using the same keys.

Parameters

array $keys

Return Value

void

at line 294
static void flush(int|null $coroutineId = null)

Flush all context data for the specified coroutine.

Parameters

int|null $coroutineId

Return Value

void

at line 310
static array|ArrayObject|null getContainer(int|null $coroutineId = null)

Get the raw context storage for the current or specified coroutine.

Parameters

int|null $coroutineId

Return Value

array|ArrayObject|null