Container
class Container implements Container
Traits
Constants
| protected MAX_RESOLUTION_DEPTH |
Maximum resolution depth before assuming a circular dependency. Safety net for indirect cycles (e.g., through interfaces) where the abstract names differ from the concretes pushed by build(), making the direct in_array check insufficient. |
Properties
| static protected Container|null | $instance | The current globally available container (if any). |
|
| static protected int | $contextKeyPrefixCount | The number of context key prefixes given to containers in this worker. |
|
| protected bool[] | $resolved | An array of the types that have been resolved. |
|
| protected array[] | $bindings | The container's bindings. |
|
| protected Closure[] | $methodBindings | The container's method bindings. |
|
| protected array<string, mixed> | $instances | The container's shared instances. |
|
| protected object[] | $autoSingletons | Auto-singletoned instances for unbound concrete classes. |
|
| protected SharedResolution> | $sharedResolutions | Cacheable first resolutions currently owned by a coroutine. |
|
| protected array<int, int> | $sharedResolutionWaits | Container waits keyed by the waiting coroutine ID. |
|
| protected array<string, true> | $scopedInstances | The abstract names registered as scoped bindings. |
|
| protected string|null | $contextKeyPrefix | The prefix of this container's coroutine context keys. |
|
| protected string[] | $aliases | The registered type aliases. |
|
| protected array[] | $abstractAliases | The registered aliases keyed by the abstract name. |
|
| protected array[] | $extenders | The extension closures for services. |
|
| protected array[] | $tags | All of the registered tags. |
|
| array[] | $contextual | The contextual binding map. |
|
| array[] | $contextualAttributes | The contextual attribute handlers. |
|
| protected array<class-string, true> | $checkedForAttributeBindings | Whether an abstract class has already had its attributes checked for bindings. |
|
| protected array<class-string, null|"scoped"|"singleton"> | $checkedForSingletonOrScopedAttributes | Whether a class has already been checked for Singleton or Scoped attributes. |
|
| protected array[] | $reboundCallbacks | All of the registered rebound callbacks. |
|
| protected Closure[] | $globalBeforeResolvingCallbacks | All of the global before resolving callbacks. |
|
| protected Closure[] | $globalResolvingCallbacks | All of the global resolving callbacks. |
|
| protected Closure[] | $globalAfterResolvingCallbacks | All of the global after resolving callbacks. |
|
| protected array[] | $beforeResolvingCallbacks | All of the before resolving callbacks by class type. |
|
| protected array[] | $resolvingCallbacks | All of the resolving callbacks by class type. |
|
| protected array[] | $afterResolvingCallbacks | All of the after resolving callbacks by class type. |
|
| protected Closure>> | $afterResolvingAttributeCallbacks | All of the after resolving attribute callbacks by class type. |
|
| protected null|callable(array<int, string>|string): bool|string | $environmentResolver | The callback used to determine the container's environment. |
|
| static protected BuildRecipe> | $buildRecipes | Cache of computed build recipes. |
Methods
Get the class name of the first parameter of the given Closure.
Get the class names of the first parameter of the given Closure, including union types.
Get the class names / types of the parameters of the given Closure.
Get the class names / types of the return type of the given Closure.
Define a contextual binding.
Define a contextual binding based on an attribute.
Determine if the given abstract type has been bound.
No description
Determine if the given abstract type has been resolved.
Determine if a given type is shared.
Determine if a given type has an explicit shared lifetime.
Determine if a given abstract is a scoped binding.
Determine if the object is a resolved scoped instance in this coroutine.
Determine if a scoped binding has already been resolved in this coroutine.
Determine if the container holds a worker-shared instance for the given type.
Determine if a ReflectionClass has scoping attributes applied.
Determine if a given string is an alias.
Register one binding and its lifecycle.
Get the Closure to be used when building a type.
Determine if the container has a method binding.
Bind a callback to resolve with Container::call.
Get the method to be bound in class@method format.
Get the method binding for the given method.
Add a contextual binding to the container.
Register a shared binding if it hasn't already been registered.
Register a binding with the container based on the given Closure's return types.
Register an existing instance as shared in the container.
Remove an alias from the contextual binding alias cache.
Assign a set of tags to a given binding.
Resolve all of the bindings for a given tag.
Alias a type to a different name.
Refresh an instance on the given target and method.
Fire the "rebound" callbacks for the given abstract type.
Get the rebound callbacks for a given type.
Call the given Closure / class@method and inject its dependencies.
Get the class name for the given callback, if one can be determined.
An alias function name for make().
Resolve the given type from the container.
Resolve the given type with a transient implicit lifetime.
No description
Resolve the given type from the container.
Determine whether this miss can publish a worker-shared instance.
Wait for a shared resolution without introducing a coordinator cycle.
Determine whether the resolution owner is an ancestor of a coroutine.
Determine whether adding a container wait edge would close a cycle.
Get the concrete type for a given abstract.
Get the concrete binding for an abstract from the BindWhen or Bind attributes.
Resolve the concrete from the Bind and BindWhen attributes in declaration order.
Get the contextual concrete binding for the given abstract.
Get the resolution state for the current coroutine.
Get or create the mutable resolution state for the current coroutine.
Get this container's coroutine context key for the given entry.
Get the current build stack without creating resolution state.
Find the concrete binding for the given abstract in the contextual binding array.
Determine if the given concrete is buildable.
Get the cached build recipe for the given concrete class.
Compute the build recipe for the given concrete class.
Resolve the contextual constructor parameters of a class within its build context.
Build a concrete as part of container resolution.
Instantiate a concrete instance of the given self building type.
Resolve all constructor parameters using cached recipe metadata.
Resolve a class based variadic dependency from the container.
Resolve a dependency based on an attribute.
Throw an exception that the concrete is not instantiable.
Register a new before resolving callback for all types.
Register a new after resolving callback for all types.
Register a new after resolving attribute callback for all types.
Fire all of the before resolving callbacks.
Fire an array of callbacks with an object.
Fire all of the resolving callbacks.
Fire all of the after resolving callbacks.
Fire all of the after resolving attribute callbacks.
Fire all callbacks that match the given abstract or object type.
Fire an array of callbacks with an object.
Get the class name or closure object ID the container is currently resolving.
Get the container's bindings.
Get the alias for an abstract if available.
Get the extender callbacks for a given type.
Remove all of the extender callbacks for a given type.
Drop all of the stale instances and aliases.
Forget every cached instance for an abstract without changing its lifecycle.
Remove a resolved instance from the instance cache.
Clear all of the instances from the container.
Clear all of the scoped instances from the container.
Set the callback which determines the current container environment.
Determine the environment for the container.
Flush the container of all bindings and resolved instances.
Give the cloned container its own coroutine context entries.
Get the globally available instance of the container.
Flush all static state.
Details
in
ReflectsClosures at line 26
protected string
firstClosureParameterType(Closure $closure)
Get the class name of the first parameter of the given Closure.
in
ReflectsClosures at line 49
protected array
firstClosureParameterTypes(Closure $closure)
Get the class names of the first parameter of the given Closure, including union types.
in
ReflectsClosures at line 75
protected array
closureParameterTypes(Closure $closure)
Get the class names / types of the parameters of the given Closure.
in
ReflectsClosures at line 97
protected array
closureReturnTypes(Closure $closure)
Get the class names / types of the return type of the given Closure.
at line 272
ContextualBindingBuilder
when(string|array $concrete)
Define a contextual binding.
at line 286
void
whenHasAttribute(string $attribute, Closure $handler)
Define a contextual binding based on an attribute.
at line 294
bool
bound(string $abstract)
Determine if the given abstract type has been bound.
at line 302
bool
has(string $id)
No description
at line 310
bool
resolved(string $abstract)
Determine if the given abstract type has been resolved.
at line 324
bool
isShared(string $abstract)
Determine if a given type is shared.
at line 346
protected bool
isExplicitlyShared(string $abstract)
Determine if a given type has an explicit shared lifetime.
at line 372
bool
isScoped(string $abstract)
Determine if a given abstract is a scoped binding.
at line 403
bool
isScopedInstance(mixed $instance)
Determine if the object is a resolved scoped instance in this coroutine.
at line 434
bool
resolvedScoped(string $abstract)
Determine if a scoped binding has already been resolved in this coroutine.
Scoped instances live in coroutine-local context, so this is false at the start of every request even for a binding resolved many times before. Callers that would only inspect an instance can use this to avoid paying for a full resolution — and the construction it implies — when nothing in the request asked for one.
Only the scoped resolution path writes this context key, so its presence already implies the binding is scoped; singletons, auto-singletons and transient bindings never produce one. An instance() registration that replaced a scoped binding also counts: it supersedes the scoped lifetime during resolution — tests swap scoped services this way — so an existing instance must be reported as resolved rather than skipped.
at line 446
bool
resolvedShared(string $abstract)
Determine if the container holds a worker-shared instance for the given type.
at line 461
protected string|null
getScopedType(ReflectionClass|string $reflection)
Determine if a ReflectionClass has scoping attributes applied.
at line 507
bool
isAlias(string $name)
Determine if a given string is an alias.
at line 521
void
bind(Closure|string $abstract, Closure|string|null $concrete = null, bool $shared = false)
Register a binding with the container.
Bindings persist on the worker-lifetime container; runtime use races across coroutines and changes resolution for every subsequent request.
at line 540
protected void
registerBinding(string $abstract, Closure|string|null $concrete, bool $shared, bool $scoped)
Register one binding and its lifecycle.
at line 577
protected Closure
getClosure(string $abstract, string $concrete)
Get the Closure to be used when building a type.
at line 598
bool
hasMethodBinding(string $method)
Determine if the container has a method binding.
at line 609
void
bindMethod(array|string $method, Closure $callback)
Bind a callback to resolve with Container::call.
Method bindings persist on the worker-lifetime container and affect every subsequent Container::call() for the bound method.
at line 618
protected string
parseBindMethod(array|string $method)
Get the method to be bound in class@method format.
at line 630
mixed
callMethodBinding(string $method, mixed $instance)
Get the method binding for the given method.
at line 642
void
addContextualBinding(string $concrete, string $abstract, mixed $implementation)
Add a contextual binding to the container.
Contextual bindings persist on the worker-lifetime container; runtime use races across coroutines and changes dependency resolution for every subsequent build of the concrete.
at line 654
void
bindIf(Closure|string $abstract, Closure|string|null $concrete = null, bool $shared = false)
Register a binding if it hasn't already been registered.
Conditional bindings mutate the worker-lifetime container for every subsequent request.
at line 680
void
singleton(Closure|string $abstract, Closure|string|null $concrete = null)
Register a shared binding in the container.
Singleton bindings persist on the worker-lifetime container; runtime use races across coroutines and changes shared resolution for every subsequent request.
at line 692
void
singletonIf(Closure|string $abstract, Closure|string|null $concrete = null)
Register a shared binding if it hasn't already been registered.
Conditional singleton bindings mutate the worker-lifetime container for every subsequent request.
at line 704
void
scoped(Closure|string $abstract, Closure|string|null $concrete = null)
Register a scoped binding in the container.
Scoped binding definitions persist on the worker-lifetime container; only their resolved instances are coroutine-scoped.
at line 722
void
scopedIf(Closure|string $abstract, Closure|string|null $concrete = null)
Register a scoped binding if it hasn't already been registered.
Conditional scoped bindings mutate the worker-lifetime container for every subsequent request.
at line 745
protected void
bindBasedOnClosureReturnTypes(Closure $abstract, Closure|string|null $concrete = null, bool $shared = false, bool $scoped = false, bool $onlyIfUnbound = false)
Register a binding with the container based on the given Closure's return types.
at line 773
void
extend(string $abstract, Closure $closure)
"Extend" an abstract type in the container.
The extender persists in the container's $extenders array for the worker lifetime and runs on every subsequent resolution of $abstract.
at line 819
mixed
instance(string $abstract, mixed $instance)
Register an existing instance as shared in the container.
Replaces a worker-lifetime shared instance; runtime use races across coroutines and changes the object returned to every subsequent resolver.
at line 845
protected void
removeAbstractAlias(string $searched)
Remove an alias from the contextual binding alias cache.
at line 866
void
tag(array|string $abstracts, array|string $tags)
Assign a set of tags to a given binding.
Tags persist on the worker-lifetime container and affect every subsequent tagged() resolution.
at line 885
iterable
tagged(string $tag)
Resolve all of the bindings for a given tag.
at line 906
void
alias(string $abstract, string $alias)
Alias a type to a different name.
Aliases persist on the worker-lifetime container; runtime use races across coroutines and changes resolution for every subsequent request.
at line 936
mixed
rebinding(string $abstract, Closure $callback)
Bind a new callback to an abstract's rebind event.
Rebinding callbacks persist on the worker-lifetime container and run whenever the abstract is rebound later in the worker.
at line 954
mixed
refresh(string $abstract, mixed $target, string $method)
Refresh an instance on the given target and method.
Registers a rebinding callback on the worker-lifetime container; runtime use leaks the callback across later rebindings.
at line 965
protected void
rebound(string $abstract)
Fire the "rebound" callbacks for the given abstract type.
at line 981
protected array
getReboundCallbacks(string $abstract)
Get the rebound callbacks for a given type.
at line 989
Closure
wrap(Closure $callback, array $parameters = [])
Wrap the given closure such that its dependencies will be injected when executed.
at line 1001
mixed
call(callable|string $callback, array $parameters = [], string|null $defaultMethod = null)
Call the given Closure / class@method and inject its dependencies.
at line 1070
protected string|false
getClassForCallable(callable|string $callback)
Get the class name for the given callback, if one can be determined.
Closures are handled directly in call() before this method is reached, so this only needs to handle non-closure callable types.
at line 1095
Closure
factory(string $abstract)
Get a closure to resolve the given type from the container.
at line 1110
mixed
makeWith(string $abstract, array $parameters = [])
An alias function name for make().
at line 1125
mixed
make(string $abstract, array $parameters = [])
Resolve the given type from the container.
at line 1142
mixed
makeTransient(string $abstract)
Resolve the given type with a transient implicit lifetime.
Explicit lifetimes remain authoritative.
at line 1156
mixed
get(string $id)
No description
at line 1184
protected mixed
resolve(string $abstract, array $parameters = [], bool $raiseEvents = true, bool $transient = false)
Resolve the given type from the container.
at line 1441
protected bool
shouldCoordinateSharedResolution(string $abstract, mixed $concrete, bool $needsContextualBuild, bool $raiseEvents)
Determine whether this miss can publish a worker-shared instance.
at line 1468
protected mixed
awaitSharedResolution(string $abstract, SharedResolution $resolution)
Wait for a shared resolution without introducing a coordinator cycle.
at line 1498
protected bool
sharedResolutionOwnerIsAncestor(int $ownerId, int $coroutineId)
Determine whether the resolution owner is an ancestor of a coroutine.
at line 1518
protected bool
sharedResolutionWaitWouldCycle(int $waiterId, int $ownerId)
Determine whether adding a container wait edge would close a cycle.
at line 1534
protected mixed
getConcrete(string $abstract)
Get the concrete type for a given abstract.
at line 1553
protected mixed
getConcreteBindingFromAttributes(string $abstract)
Get the concrete binding for an abstract from the BindWhen or Bind attributes.
at line 1589
protected string|null
resolveConcreteFromAttributes(ReflectionClass $reflected)
Resolve the concrete from the Bind and BindWhen attributes in declaration order.
at line 1627
protected mixed
getContextualConcrete(string $abstract)
Get the contextual concrete binding for the given abstract.
at line 1656
protected ContainerResolutionState|null
getResolutionState()
Get the resolution state for the current coroutine.
at line 1666
protected ContainerResolutionState
getOrCreateResolutionState()
Get or create the mutable resolution state for the current coroutine.
at line 1681
protected string
contextKey(string $name)
Get this container's coroutine context key for the given entry.
at line 1691
protected array
currentBuildStack()
Get the current build stack without creating resolution state.
at line 1701
protected mixed
findInContextualBindings(string $abstract)
Find the concrete binding for the given abstract in the contextual binding array.
at line 1715
protected bool
isBuildable(mixed $concrete, string $abstract)
Determine if the given concrete is buildable.
at line 1723
protected BuildRecipe
getBuildRecipe(string $concrete)
Get the cached build recipe for the given concrete class.
at line 1735
protected BuildRecipe
computeBuildRecipe(string $concrete)
Compute the build recipe for the given concrete class.
Analyzes the class via reflection and caches the result as a BuildRecipe. This happens once per class per worker lifetime, avoiding repeated reflection on every build() call.
at line 1815
mixed
buildWith(Closure|string $concrete, array $parameters = [])
Instantiate a concrete instance with the given parameter overrides.
Bypasses all binding lookups and the auto-singleton cache, so the returned instance is always freshly constructed. Class-typed dependencies are still resolved via the container.
at line 1841
array
resolveContextualParameters(string $concrete, array|null $names = null)
Resolve the contextual constructor parameters of a class within its build context.
Contextual bindings for the class apply to dependencies the attributes resolve, and each parameter's attribute callbacks fire here. Pass the values to buildWith() as overrides so they are not resolved again. Variadic parameters are skipped, because an override is passed as one argument; the container resolves them when the class is built.
at line 1887
mixed
build(Closure|string $concrete)
Instantiate a concrete instance of the given type.
at line 1962
protected mixed
buildForResolution(Closure|string $concrete, ContainerResolutionState $resolutionState)
Build a concrete as part of container resolution.
at line 1989
protected mixed
buildSelfBuildingInstance(string $concrete, BuildRecipe $recipe, ContainerResolutionState $resolutionState)
Instantiate a concrete instance of the given self building type.
at line 2021
protected array
resolveRecipeParameters(BuildRecipe $recipe, ContainerResolutionState $resolutionState)
Resolve all constructor parameters using cached recipe metadata.
at line 2076
protected mixed
resolvePrimitive(ParameterRecipe $param)
Resolve a non-class hinted primitive dependency.
at line 2102
protected mixed
resolveClass(ParameterRecipe $param)
Resolve a class based dependency from the container.
at line 2132
protected mixed
resolveVariadicClass(ParameterRecipe $param)
Resolve a class based variadic dependency from the container.
at line 2150
mixed
resolveFromAttribute(ReflectionAttribute $attribute, ReflectionParameter $parameter)
Resolve a dependency based on an attribute.
at line 2172
protected never
notInstantiable(string $concrete)
Throw an exception that the concrete is not instantiable.
at line 2192
protected never
unresolvablePrimitive(ParameterRecipe $param)
Throw an exception for an unresolvable primitive.
at line 2205
void
beforeResolving(Closure|string $abstract, Closure|null $callback = null)
Register a new before resolving callback for all types.
The callback persists on the worker-lifetime container and runs before every matching future resolution.
at line 2225
void
resolving(Closure|string $abstract, Closure|null $callback = null)
Register a new resolving callback.
The callback persists on the worker-lifetime container and runs during every matching future resolution.
at line 2245
void
afterResolving(Closure|string $abstract, Closure|null $callback = null)
Register a new after resolving callback for all types.
The callback persists on the worker-lifetime container and runs after every matching future resolution.
at line 2265
void
afterResolvingAttribute(string $attribute, Closure $callback)
Register a new after resolving attribute callback for all types.
The callback persists on the worker-lifetime container and runs after every future resolution with the matching attribute.
at line 2274
protected void
fireBeforeResolvingCallbacks(string $abstract, array $parameters = [])
Fire all of the before resolving callbacks.
at line 2292
protected void
fireBeforeCallbackArray(string $abstract, array $parameters, array $callbacks)
Fire an array of callbacks with an object.
at line 2302
protected void
fireResolvingCallbacks(string $abstract, mixed $object)
Fire all of the resolving callbacks.
at line 2321
protected void
fireAfterResolvingCallbacks(string $abstract, mixed $object)
Fire all of the after resolving callbacks.
at line 2333
void
fireAfterResolvingAttributeCallbacks(array $attributes, mixed $object)
Fire all of the after resolving attribute callbacks.
at line 2365
protected void
fireCallbacksForType(string $abstract, mixed $object, array $callbacksPerType)
Fire all callbacks that match the given abstract or object type.
at line 2379
protected void
fireCallbackArray(mixed $object, array $callbacks)
Fire an array of callbacks with an object.
at line 2389
int|string|null
currentlyResolving()
Get the class name or closure object ID the container is currently resolving.
at line 2397
array
getBindings()
Get the container's bindings.
at line 2405
string
getAlias(string $abstract)
Get the alias for an abstract if available.
at line 2416
protected array
getExtenders(string $abstract)
Get the extender callbacks for a given type.
at line 2427
void
forgetExtenders(string $abstract)
Remove all of the extender callbacks for a given type.
Clears worker-lifetime extender callbacks; concurrent resolutions may diverge depending on whether they resolved before or after the removal.
at line 2436
protected void
dropStaleInstances(string $abstract)
Drop all of the stale instances and aliases.
at line 2447
protected void
forgetCachedInstances(string $abstract)
Forget every cached instance for an abstract without changing its lifecycle.
at line 2461
void
forgetInstance(string $abstract)
Remove a resolved instance from the instance cache.
Clears a worker-lifetime shared instance and current coroutine scoped instance; concurrent coroutines may still hold references that next resolution will not share.
at line 2474
void
forgetInstances()
Clear all of the instances from the container.
Clears worker-lifetime shared instances and all scoped context entries known to this container; concurrent coroutines may still hold references that next resolution will not share.
at line 2488
void
forgetScopedInstances()
Clear all of the scoped instances from the container.
Clears this container's scoped instances in the current coroutine; runtime use can invalidate scoped services while the request is still using them.
at line 2504
void
resolveEnvironmentUsing(callable|null $callback)
Set the callback which determines the current container environment.
The resolver persists on the worker-lifetime container and is used by every currentEnvironmentIs() call.
at line 2515
bool
currentEnvironmentIs(array|string $environments)
Determine the environment for the container.
at line 2528
void
flush()
Flush the container of all bindings and resolved instances.
Clears the worker-lifetime container registry; runtime use breaks every concurrent resolution in the worker.
at line 2558
void
__clone()
Give the cloned container its own coroutine context entries.
Like its shared instances, the clone starts with the scoped instances resolved in the current coroutine, but forgetting or replacing them on one container leaves the other's untouched.
at line 2577
static Container
getInstance()
Get the globally available instance of the container.
at line 2588
static Container|null
setInstance(Container|null $container = null)
Set the shared instance of the container.
Replaces the process-wide container singleton; runtime use races across coroutines and breaks every facade/global container lookup.
at line 2597
static void
flushState()
Flush all static state.