Context
class Context
This class is an implementation of a generic context.
Example:
// Create and set a new context, which inherits values from the current
// context and adds a new key/value pair of 'foo'/'bar'.
$prev = Context::current()->withValue('foo', 'bar')->attach();
try {
// Do something within the context
} finally {
// This makes sure that $prev will be current after this execution
Context::current()->detach($prev);
}
// Here, we are 100% sure $prev is the current context.
Methods
Creates a new Context.
Creates a new context with the given key/values.
Fetches the value for a given key in this context. Returns the provided default if not set.
Returns all the contained data.
Attaches this context, thus entering a new scope within which this context is current(). The previously current context is returned.
Returns the context associated with the current scope, will never return null.
Returns an empty context.
Details
at line 54
__construct(array $initialValues = [])
Creates a new Context.
at line 66
Context
withValue(string $key, mixed $value)
Creates a new context with the given key/value set.
at line 79
Context
withValues(array $data)
Creates a new context with the given key/values.
at line 96
mixed
value(string $key, mixed $default = null)
Fetches the value for a given key in this context. Returns the provided default if not set.
at line 108
array
values()
Returns all the contained data.
at line 119
Context
attach()
Attaches this context, thus entering a new scope within which this context is current(). The previously current context is returned.
at line 132
detach(Context $toAttach)
Reverses an attach(), restoring the previous context and exiting the current scope.
at line 147
static Context
current()
Returns the context associated with the current scope, will never return null.
at line 161
static Context
background()
Returns an empty context.