class Repository implements ReplicableContext

Traits

Constants

CONTEXT_KEY

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected array<string, mixed> $data

The contextual data that flows to logs and jobs.

protected array<string, mixed> $hidden

The hidden contextual data that flows to jobs but not logs.

static protected null|Throwable, string, string, bool): mixed $handleUnserializeExceptionsUsing

The callback that should handle unserialize exceptions.

Methods

mixed
when(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) truthy.

mixed
unless(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) falsy.

static void
macro(string $name, callable|object $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Check if macro is registered.

static void
flushMacros()

Flush the existing macros.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
getSerializedPropertyValue(mixed $value, bool $withRelations = true)

Get the property value prepared for serialization.

mixed
getRestoredPropertyValue(mixed $value)

Get the restored property value after deserialization.

restoreCollection(ModelIdentifier $value)

Restore a queueable collection instance.

restoreModel(ModelIdentifier $value)

Restore the model from the model identifier instance.

getQueryForModelRestoration(Model $model, array|int|string $ids)

Get the query for model restoration.

__construct(Dispatcher $events)

Create a new context repository instance.

static Repository
getInstance()

Get the context repository instance for the current coroutine.

static bool
hasInstance()

Determine if a context repository instance exists for the current coroutine.

bool
has(string $key)

Determine if the given key exists.

bool
missing(string $key)

Determine if the given key is missing.

array
all()

Retrieve all the context data.

mixed
get(string $key, mixed $default = null)

Retrieve the given key's value.

mixed
pull(string $key, mixed $default = null)

Retrieve the given key's value and then forget it.

array
only(array $keys)

Retrieve only the values of the given keys.

array
except(array $keys)

Retrieve all values except those with the given keys.

add(string|array $key, mixed $value = null)

Add a context value.

addIf(string $key, mixed $value)

Add a context value if it does not exist yet.

mixed
remember(string $key, mixed $value)

Add a context value if it does not exist yet, and return the value.

forget(string|array $key)

Forget the given context key.

push(string $key, mixed ...$values)

Push values onto a context stack.

mixed
pop(string $key)

Pop the latest value from a context stack.

bool
stackContains(string $key, mixed $value, bool $strict = false)

Determine if the given value is in the given stack.

increment(string $key, int $amount = 1)

Increment a context counter.

decrement(string $key, int $amount = 1)

Decrement a context counter.

bool
hasHidden(string $key)

Determine if the given key exists within the hidden context data.

bool
missingHidden(string $key)

Determine if the given key is missing within the hidden context data.

array
allHidden()

Retrieve all the hidden context data.

mixed
getHidden(string $key, mixed $default = null)

Retrieve the given key's hidden value.

mixed
pullHidden(string $key, mixed $default = null)

Retrieve the given key's hidden value and then forget it.

array
onlyHidden(array $keys)

Retrieve only the hidden values of the given keys.

array
exceptHidden(array $keys)

Retrieve all hidden values except those with the given keys.

addHidden(string|array $key, mixed $value = null)

Add a hidden context value.

addHiddenIf(string $key, mixed $value)

Add a hidden context value if it does not exist yet.

mixed
rememberHidden(string $key, mixed $value)

Add a hidden context value if it does not exist yet, and return the value.

forgetHidden(string|array $key)

Forget the given hidden context key.

pushHidden(string $key, mixed ...$values)

Push values onto a hidden context stack.

mixed
popHidden(string $key)

Pop the latest value from a hidden context stack.

bool
hiddenStackContains(string $key, mixed $value, bool $strict = false)

Determine if the given value is in the given hidden stack.

mixed
scope(callable $callback, array $data = [], array $hidden = [])

Run a callback with temporary context, then restore.

bool
isEmpty()

Determine if the context is empty.

flush()

Flush all context data.

replicate()

Create an independent copy with the same data and hidden values.

dehydrating(callable $callback)

Register a callback to execute before context is dehydrated.

hydrated(callable $callback)

Register a callback to execute after context has been hydrated.

handleUnserializeExceptionsUsing(callable|null $callback)

Set the callback to handle unserialize exceptions.

static void
flushState()

Flush all static state.

array|null
dehydrate()

Dehydrate the context into a serializable payload.

hydrate(array|null $context)

Hydrate the context from a serialized payload.

bool
isStackable(string $key)

Determine if a given key can be used as a stack.

bool
isHiddenStackable(string $key)

Determine if a given key can be used as a hidden stack.

Details

in Conditionable at line 23
mixed when(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) truthy.

Parameters

mixed $value
callable|null $callback
callable|null $default

Return Value

mixed

in Conditionable at line 56
mixed unless(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) falsy.

Parameters

mixed $value
callable|null $callback
callable|null $default

Return Value

mixed

in Macroable at line 30
static void macro(string $name, callable|object $macro)

Register a custom macro.

Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.

Parameters

string $name
callable|object $macro

Return Value

void

in Macroable at line 44
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

in Macroable at line 61
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 72
static void flushMacros()

Flush the existing macros.

Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.

Return Value

void

in Macroable at line 83
static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

in Macroable at line 108
mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

protected mixed getSerializedPropertyValue(mixed $value, bool $withRelations = true)

Get the property value prepared for serialization.

Parameters

mixed $value
bool $withRelations

Return Value

mixed

protected mixed getRestoredPropertyValue(mixed $value)

Get the restored property value after deserialization.

Parameters

mixed $value

Return Value

mixed

protected Collection restoreCollection(ModelIdentifier $value)

Restore a queueable collection instance.

Parameters

ModelIdentifier $value

Return Value

Collection

Model restoreModel(ModelIdentifier $value)

Restore the model from the model identifier instance.

Parameters

ModelIdentifier $value

Return Value

Model

protected Builder getQueryForModelRestoration(Model $model, array|int|string $ids)

Get the query for model restoration.

Parameters

Model $model
array|int|string $ids

Return Value

Builder

at line 56
__construct(Dispatcher $events)

Create a new context repository instance.

Parameters

Dispatcher $events

at line 68
static Repository getInstance()

Get the context repository instance for the current coroutine.

Creates the instance on first access. For hot paths that should avoid unnecessary allocation (log processors, queue hooks), use hasInstance() to check first.

Return Value

Repository

at line 81
static bool hasInstance()

Determine if a context repository instance exists for the current coroutine.

Unlike getInstance(), this does NOT create one if it doesn't exist.

Return Value

bool

at line 91
bool has(string $key)

Determine if the given key exists.

Parameters

string $key

Return Value

bool

at line 99
bool missing(string $key)

Determine if the given key is missing.

Parameters

string $key

Return Value

bool

at line 109
array all()

Retrieve all the context data.

Return Value

array

at line 117
mixed get(string $key, mixed $default = null)

Retrieve the given key's value.

Parameters

string $key
mixed $default

Return Value

mixed

at line 125
mixed pull(string $key, mixed $default = null)

Retrieve the given key's value and then forget it.

Parameters

string $key
mixed $default

Return Value

mixed

at line 138
array only(array $keys)

Retrieve only the values of the given keys.

Parameters

array $keys

Return Value

array

at line 149
array except(array $keys)

Retrieve all values except those with the given keys.

Parameters

array $keys

Return Value

array

at line 160
Repository add(string|array $key, mixed $value = null)

Add a context value.

Parameters

string|array $key
mixed $value

Return Value

Repository

at line 176
Repository addIf(string $key, mixed $value)

Add a context value if it does not exist yet.

Parameters

string $key
mixed $value

Return Value

Repository

at line 188
mixed remember(string $key, mixed $value)

Add a context value if it does not exist yet, and return the value.

Parameters

string $key
mixed $value

Return Value

mixed

at line 205
Repository forget(string|array $key)

Forget the given context key.

Parameters

string|array $key

Return Value

Repository

at line 220
Repository push(string $key, mixed ...$values)

Push values onto a context stack.

Parameters

string $key
mixed ...$values

Return Value

Repository

Exceptions

RuntimeException

at line 239
mixed pop(string $key)

Pop the latest value from a context stack.

Parameters

string $key

Return Value

mixed

Exceptions

RuntimeException

at line 253
bool stackContains(string $key, mixed $value, bool $strict = false)

Determine if the given value is in the given stack.

Parameters

string $key
mixed $value
bool $strict

Return Value

bool

Exceptions

RuntimeException

at line 275
Repository increment(string $key, int $amount = 1)

Increment a context counter.

Parameters

string $key
int $amount

Return Value

Repository

at line 290
Repository decrement(string $key, int $amount = 1)

Decrement a context counter.

Parameters

string $key
int $amount

Return Value

Repository

at line 300
bool hasHidden(string $key)

Determine if the given key exists within the hidden context data.

Parameters

string $key

Return Value

bool

at line 308
bool missingHidden(string $key)

Determine if the given key is missing within the hidden context data.

Parameters

string $key

Return Value

bool

at line 318
array allHidden()

Retrieve all the hidden context data.

Return Value

array

at line 326
mixed getHidden(string $key, mixed $default = null)

Retrieve the given key's hidden value.

Parameters

string $key
mixed $default

Return Value

mixed

at line 334
mixed pullHidden(string $key, mixed $default = null)

Retrieve the given key's hidden value and then forget it.

Parameters

string $key
mixed $default

Return Value

mixed

at line 347
array onlyHidden(array $keys)

Retrieve only the hidden values of the given keys.

Parameters

array $keys

Return Value

array

at line 358
array exceptHidden(array $keys)

Retrieve all hidden values except those with the given keys.

Parameters

array $keys

Return Value

array

at line 369
Repository addHidden(string|array $key, mixed $value = null)

Add a hidden context value.

Parameters

string|array $key
mixed $value

Return Value

Repository

at line 385
Repository addHiddenIf(string $key, mixed $value)

Add a hidden context value if it does not exist yet.

Parameters

string $key
mixed $value

Return Value

Repository

at line 397
mixed rememberHidden(string $key, mixed $value)

Add a hidden context value if it does not exist yet, and return the value.

Parameters

string $key
mixed $value

Return Value

mixed

at line 414
Repository forgetHidden(string|array $key)

Forget the given hidden context key.

Parameters

string|array $key

Return Value

Repository

at line 429
Repository pushHidden(string $key, mixed ...$values)

Push values onto a hidden context stack.

Parameters

string $key
mixed ...$values

Return Value

Repository

Exceptions

RuntimeException

at line 448
mixed popHidden(string $key)

Pop the latest value from a hidden context stack.

Parameters

string $key

Return Value

mixed

Exceptions

RuntimeException

at line 462
bool hiddenStackContains(string $key, mixed $value, bool $strict = false)

Determine if the given value is in the given hidden stack.

Parameters

string $key
mixed $value
bool $strict

Return Value

bool

Exceptions

RuntimeException

at line 491
mixed scope(callable $callback, array $data = [], array $hidden = [])

Run a callback with temporary context, then restore.

Parameters

callable $callback
array $data
array $hidden

Return Value

mixed

Exceptions

Throwable

at line 517
bool isEmpty()

Determine if the context is empty.

Return Value

bool

at line 527
Repository flush()

Flush all context data.

Return Value

Repository

at line 542
ReplicableContext replicate()

Create an independent copy with the same data and hidden values.

Used by coroutine context copying via ReplicableContext to ensure forked coroutines get their own instance rather than sharing an object reference with the parent.

Return Value

ReplicableContext

at line 560
Repository dehydrating(callable $callback)

Register a callback to execute before context is dehydrated.

When the dispatcher is the application's worker-shared dispatcher, per-request registration persists and affects subsequent requests.

Parameters

callable $callback

Return Value

Repository

at line 576
Repository hydrated(callable $callback)

Register a callback to execute after context has been hydrated.

When the dispatcher is the application's worker-shared dispatcher, per-request registration persists and affects subsequent requests.

Parameters

callable $callback

Return Value

Repository

at line 591
Repository handleUnserializeExceptionsUsing(callable|null $callback)

Set the callback to handle unserialize exceptions.

The callback persists in a static property for the worker lifetime and handles every subsequent context hydration failure.

Parameters

callable|null $callback

Return Value

Repository

at line 604
static void flushState()

Flush all static state.

Return Value

void

at line 624
array|null dehydrate()

internal  
 

Dehydrate the context into a serializable payload.

Creates a clone of this instance, dispatches ContextDehydrating so listeners can modify the clone (not the original), then serializes all values. Uses SerializesAndRestoresModelIdentifiers to handle Eloquent models.

Returns null if both data and hidden are empty after dehydration hooks run.

Return Value

array|null

at line 656
Repository hydrate(array|null $context)

internal  
 

Hydrate the context from a serialized payload.

Flushes existing data, deserializes the payload, then dispatches ContextHydrated so listeners can react to the restored data. Uses SerializesAndRestoresModelIdentifiers to restore Eloquent models.

Parameters

array|null $context

Return Value

Repository

Exceptions

RuntimeException

at line 705
protected bool isStackable(string $key)

Determine if a given key can be used as a stack.

Parameters

string $key

Return Value

bool

at line 714
protected bool isHiddenStackable(string $key)

Determine if a given key can be used as a hidden stack.

Parameters

string $key

Return Value

bool