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 for a job.

hydrated(callable $callback)

Register a callback to execute after context has been hydrated from a job.

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 28
static void macro(string $name, callable|object $macro)

Register a custom macro.

Boot-only. 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 41
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Boot-only. 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 57
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 68
static void flushMacros()

Flush the existing macros.

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

Return Value

void

in Macroable at line 78
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 103
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 55
__construct(Dispatcher $events)

Create a new context repository instance.

Parameters

Dispatcher $events

at line 67
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 80
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 90
bool has(string $key)

Determine if the given key exists.

Parameters

string $key

Return Value

bool

at line 98
bool missing(string $key)

Determine if the given key is missing.

Parameters

string $key

Return Value

bool

at line 108
array all()

Retrieve all the context data.

Return Value

array

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

Retrieve the given key's value.

Parameters

string $key
mixed $default

Return Value

mixed

at line 124
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 137
array only(array $keys)

Retrieve only the values of the given keys.

Parameters

array $keys

Return Value

array

at line 148
array except(array $keys)

Retrieve all values except those with the given keys.

Parameters

array $keys

Return Value

array

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

Add a context value.

Parameters

string|array $key
mixed $value

Return Value

Repository

at line 174
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 186
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 203
Repository forget(string|array $key)

Forget the given context key.

Parameters

string|array $key

Return Value

Repository

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

Push values onto a context stack.

Parameters

string $key
mixed ...$values

Return Value

Repository

Exceptions

RuntimeException

at line 237
mixed pop(string $key)

Pop the latest value from a context stack.

Parameters

string $key

Return Value

mixed

Exceptions

RuntimeException

at line 251
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 273
Repository increment(string $key, int $amount = 1)

Increment a context counter.

Parameters

string $key
int $amount

Return Value

Repository

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

Decrement a context counter.

Parameters

string $key
int $amount

Return Value

Repository

at line 298
bool hasHidden(string $key)

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

Parameters

string $key

Return Value

bool

at line 306
bool missingHidden(string $key)

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

Parameters

string $key

Return Value

bool

at line 316
array allHidden()

Retrieve all the hidden context data.

Return Value

array

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

Retrieve the given key's hidden value.

Parameters

string $key
mixed $default

Return Value

mixed

at line 332
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 345
array onlyHidden(array $keys)

Retrieve only the hidden values of the given keys.

Parameters

array $keys

Return Value

array

at line 356
array exceptHidden(array $keys)

Retrieve all hidden values except those with the given keys.

Parameters

array $keys

Return Value

array

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

Add a hidden context value.

Parameters

string|array $key
mixed $value

Return Value

Repository

at line 382
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 394
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 411
Repository forgetHidden(string|array $key)

Forget the given hidden context key.

Parameters

string|array $key

Return Value

Repository

at line 426
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 445
mixed popHidden(string $key)

Pop the latest value from a hidden context stack.

Parameters

string $key

Return Value

mixed

Exceptions

RuntimeException

at line 459
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 488
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 514
bool isEmpty()

Determine if the context is empty.

Return Value

bool

at line 524
Repository flush()

Flush all context data.

Return Value

Repository

at line 539
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 557
Repository dehydrating(callable $callback)

Register a callback to execute before context is dehydrated for a job.

Boot-only. Registers a listener on the worker-global event dispatcher; per-request registration persists and affects subsequent requests.

Parameters

callable $callback

Return Value

Repository

at line 573
Repository hydrated(callable $callback)

Register a callback to execute after context has been hydrated from a job.

Boot-only. Registers a listener on the worker-global event dispatcher; per-request registration persists and affects subsequent requests.

Parameters

callable $callback

Return Value

Repository

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

Set the callback to handle unserialize exceptions.

Boot-only. 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 600
static void flushState()

Flush all static state.

Return Value

void

at line 620
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 652
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 701
protected bool isStackable(string $key)

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

Parameters

string $key

Return Value

bool

at line 710
protected bool isHiddenStackable(string $key)

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

Parameters

string $key

Return Value

bool