class Container implements ArrayAccess, Container (View source)

Properties

static protected Container $instance

The current globally available container (if any).

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 object[] $instances

The container's shared instances.

protected array $scopedInstances

The container's scoped instances.

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.

protected array[] $buildStack

The stack of concretions currently being built.

protected array[] $with

The parameter override stack.

array[] $contextual

The contextual binding map.

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.

Methods

when(string|array $concrete)

Define a contextual binding.

bool
bound(string $abstract)

Determine if the given abstract type has been bound.

bool
has(string $id)

{@inheritdoc}

bool
resolved(string $abstract)

Determine if the given abstract type has been resolved.

bool
isShared(string $abstract)

Determine if a given type is shared.

bool
isAlias(string $name)

Determine if a given string is an alias.

void
bind(string $abstract, Closure|string|null $concrete = null, bool $shared = false)

Register a binding with the container.

getClosure(string $abstract, string $concrete)

Get the Closure to be used when building a type.

bool
hasMethodBinding(string $method)

Determine if the container has a method binding.

void
bindMethod(array|string $method, Closure $callback)

Bind a callback to resolve with Container::call.

string
parseBindMethod(array|string $method)

Get the method to be bound in class@method format.

mixed
callMethodBinding(string $method, mixed $instance)

Get the method binding for the given method.

void
addContextualBinding(string $concrete, string $abstract, Closure|string $implementation)

Add a contextual binding to the container.

void
bindIf(string $abstract, Closure|string|null $concrete = null, bool $shared = false)

Register a binding if it hasn't already been registered.

void
singleton(string $abstract, Closure|string|null $concrete = null)

Register a shared binding in the container.

void
singletonIf(string $abstract, Closure|string|null $concrete = null)

Register a shared binding if it hasn't already been registered.

void
scoped(string $abstract, Closure|string|null $concrete = null)

Register a scoped binding in the container.

void
scopedIf(string $abstract, Closure|string|null $concrete = null)

Register a scoped binding if it hasn't already been registered.

void
extend(string $abstract, Closure $closure)

"Extend" an abstract type in the container.

mixed
instance(string $abstract, mixed $instance)

Register an existing instance as shared in the container.

void
removeAbstractAlias(string $searched)

Remove an alias from the contextual binding alias cache.

void
tag(array|string $abstracts, array|mixed $tags)

Assign a set of tags to a given binding.

iterable
tagged(string $tag)

Resolve all of the bindings for a given tag.

void
alias(string $abstract, string $alias)

Alias a type to a different name.

mixed
rebinding(string $abstract, Closure $callback)

Bind a new callback to an abstract's rebind event.

mixed
refresh(string $abstract, mixed $target, string $method)

Refresh an instance on the given target and method.

void
rebound(string $abstract)

Fire the "rebound" callbacks for the given abstract type.

array
getReboundCallbacks(string $abstract)

Get the rebound callbacks for a given type.

wrap(Closure $callback, array $parameters = [])

Wrap the given closure such that its dependencies will be injected when executed.

mixed
call(callable|string $callback, array $parameters = [], string|null $defaultMethod = null)

Call the given Closure / class@method and inject its dependencies.

string|false
getClassForCallable(callable|string $callback)

Get the class name for the given callback, if one can be determined.

factory(string $abstract)

Get a closure to resolve the given type from the container.

mixed
makeWith(string|callable $abstract, array $parameters = [])

An alias function name for make().

mixed
make(string $abstract, array $parameters = [])

Resolve the given type from the container.

mixed
get(string $id)

{@inheritdoc}

mixed
resolve(string|callable $abstract, array $parameters = [], bool $raiseEvents = true)

Resolve the given type from the container.

mixed
getConcrete(string|callable $abstract)

Get the concrete type for a given abstract.

Closure|string|array|null
getContextualConcrete(string|callable $abstract)

Get the contextual concrete binding for the given abstract.

Closure|string|null
findInContextualBindings(string|callable $abstract)

Find the concrete binding for the given abstract in the contextual binding array.

bool
isBuildable(mixed $concrete, string $abstract)

Determine if the given concrete is buildable.

mixed
build(Closure|string $concrete)

Instantiate a concrete instance of the given type.

array
resolveDependencies(array $dependencies)

Resolve all of the dependencies from the ReflectionParameters.

bool
hasParameterOverride(ReflectionParameter $dependency)

Determine if the given dependency has a parameter override.

mixed
getParameterOverride(ReflectionParameter $dependency)

Get a parameter override for a dependency.

array
getLastParameterOverride()

Get the last parameter override.

mixed
resolvePrimitive(ReflectionParameter $parameter)

Resolve a non-class hinted primitive dependency.

mixed
resolveClass(ReflectionParameter $parameter)

Resolve a class based dependency from the container.

mixed
resolveVariadicClass(ReflectionParameter $parameter)

Resolve a class based variadic dependency from the container.

void
notInstantiable(string $concrete)

Throw an exception that the concrete is not instantiable.

void
unresolvablePrimitive(ReflectionParameter $parameter)

Throw an exception for an unresolvable primitive.

void
beforeResolving(Closure|string $abstract, Closure $callback = null)

Register a new before resolving callback for all types.

void
resolving(Closure|string $abstract, Closure $callback = null)

Register a new resolving callback.

void
afterResolving(Closure|string $abstract, Closure $callback = null)

Register a new after resolving callback for all types.

void
fireBeforeResolvingCallbacks(string $abstract, array $parameters = [])

Fire all of the before resolving callbacks.

void
fireBeforeCallbackArray(string $abstract, array $parameters, array $callbacks)

Fire an array of callbacks with an object.

void
fireResolvingCallbacks(string $abstract, mixed $object)

Fire all of the resolving callbacks.

void
fireAfterResolvingCallbacks(string $abstract, mixed $object)

Fire all of the after resolving callbacks.

array
getCallbacksForType(string $abstract, object $object, array $callbacksPerType)

Get all callbacks for a given type.

void
fireCallbackArray(mixed $object, array $callbacks)

Fire an array of callbacks with an object.

array
getBindings()

Get the container's bindings.

string
getAlias(string $abstract)

Get the alias for an abstract if available.

array
getExtenders(string $abstract)

Get the extender callbacks for a given type.

void
forgetExtenders(string $abstract)

Remove all of the extender callbacks for a given type.

void
dropStaleInstances(string $abstract)

Drop all of the stale instances and aliases.

void
forgetInstance(string $abstract)

Remove a resolved instance from the instance cache.

void
forgetInstances()

Clear all of the instances from the container.

void
forgetScopedInstances()

Clear all of the scoped instances from the container.

void
flush()

Flush the container of all bindings and resolved instances.

static Container
getInstance()

Get the globally available instance of the container.

setInstance(Container $container = null)

Set the shared instance of the container.

bool
offsetExists(string $key)

Determine if a given offset exists.

mixed
offsetGet(string $key)

Get the value at a given offset.

void
offsetSet(string $key, mixed $value)

Set the value at a given offset.

void
offsetUnset(string $key)

Unset the value at a given offset.

mixed
__get(string $key)

Dynamically access container services.

void
__set(string $key, mixed $value)

Dynamically set container services.

Details

ContextualBindingBuilder when(string|array $concrete)

Define a contextual binding.

Parameters

string|array $concrete

Return Value

ContextualBindingBuilder

bool bound(string $abstract)

Determine if the given abstract type has been bound.

Parameters

string $abstract

Return Value

bool

bool has(string $id)

{@inheritdoc}

Parameters

string $id

Return Value

bool

bool resolved(string $abstract)

Determine if the given abstract type has been resolved.

Parameters

string $abstract

Return Value

bool

bool isShared(string $abstract)

Determine if a given type is shared.

Parameters

string $abstract

Return Value

bool

bool isAlias(string $name)

Determine if a given string is an alias.

Parameters

string $name

Return Value

bool

void bind(string $abstract, Closure|string|null $concrete = null, bool $shared = false)

Register a binding with the container.

Parameters

string $abstract
Closure|string|null $concrete
bool $shared

Return Value

void

Exceptions

TypeError

protected Closure getClosure(string $abstract, string $concrete)

Get the Closure to be used when building a type.

Parameters

string $abstract
string $concrete

Return Value

Closure

bool hasMethodBinding(string $method)

Determine if the container has a method binding.

Parameters

string $method

Return Value

bool

void bindMethod(array|string $method, Closure $callback)

Bind a callback to resolve with Container::call.

Parameters

array|string $method
Closure $callback

Return Value

void

protected string parseBindMethod(array|string $method)

Get the method to be bound in class@method format.

Parameters

array|string $method

Return Value

string

mixed callMethodBinding(string $method, mixed $instance)

Get the method binding for the given method.

Parameters

string $method
mixed $instance

Return Value

mixed

void addContextualBinding(string $concrete, string $abstract, Closure|string $implementation)

Add a contextual binding to the container.

Parameters

string $concrete
string $abstract
Closure|string $implementation

Return Value

void

void bindIf(string $abstract, Closure|string|null $concrete = null, bool $shared = false)

Register a binding if it hasn't already been registered.

Parameters

string $abstract
Closure|string|null $concrete
bool $shared

Return Value

void

void singleton(string $abstract, Closure|string|null $concrete = null)

Register a shared binding in the container.

Parameters

string $abstract
Closure|string|null $concrete

Return Value

void

void singletonIf(string $abstract, Closure|string|null $concrete = null)

Register a shared binding if it hasn't already been registered.

Parameters

string $abstract
Closure|string|null $concrete

Return Value

void

void scoped(string $abstract, Closure|string|null $concrete = null)

Register a scoped binding in the container.

Parameters

string $abstract
Closure|string|null $concrete

Return Value

void

void scopedIf(string $abstract, Closure|string|null $concrete = null)

Register a scoped binding if it hasn't already been registered.

Parameters

string $abstract
Closure|string|null $concrete

Return Value

void

void extend(string $abstract, Closure $closure)

"Extend" an abstract type in the container.

Parameters

string $abstract
Closure $closure

Return Value

void

Exceptions

InvalidArgumentException

mixed instance(string $abstract, mixed $instance)

Register an existing instance as shared in the container.

Parameters

string $abstract
mixed $instance

Return Value

mixed

protected void removeAbstractAlias(string $searched)

Remove an alias from the contextual binding alias cache.

Parameters

string $searched

Return Value

void

void tag(array|string $abstracts, array|mixed $tags)

Assign a set of tags to a given binding.

Parameters

array|string $abstracts
array|mixed $tags

Return Value

void

iterable tagged(string $tag)

Resolve all of the bindings for a given tag.

Parameters

string $tag

Return Value

iterable

void alias(string $abstract, string $alias)

Alias a type to a different name.

Parameters

string $abstract
string $alias

Return Value

void

Exceptions

LogicException

mixed rebinding(string $abstract, Closure $callback)

Bind a new callback to an abstract's rebind event.

Parameters

string $abstract
Closure $callback

Return Value

mixed

mixed refresh(string $abstract, mixed $target, string $method)

Refresh an instance on the given target and method.

Parameters

string $abstract
mixed $target
string $method

Return Value

mixed

protected void rebound(string $abstract)

Fire the "rebound" callbacks for the given abstract type.

Parameters

string $abstract

Return Value

void

protected array getReboundCallbacks(string $abstract)

Get the rebound callbacks for a given type.

Parameters

string $abstract

Return Value

array

Closure wrap(Closure $callback, array $parameters = [])

Wrap the given closure such that its dependencies will be injected when executed.

Parameters

Closure $callback
array $parameters

Return Value

Closure

mixed call(callable|string $callback, array $parameters = [], string|null $defaultMethod = null)

Call the given Closure / class@method and inject its dependencies.

Parameters

callable|string $callback
array $parameters
string|null $defaultMethod

Return Value

mixed

Exceptions

InvalidArgumentException

protected string|false getClassForCallable(callable|string $callback)

Get the class name for the given callback, if one can be determined.

Parameters

callable|string $callback

Return Value

string|false

Closure factory(string $abstract)

Get a closure to resolve the given type from the container.

Parameters

string $abstract

Return Value

Closure

mixed makeWith(string|callable $abstract, array $parameters = [])

An alias function name for make().

Parameters

string|callable $abstract
array $parameters

Return Value

mixed

Exceptions

BindingResolutionException

mixed make(string $abstract, array $parameters = [])

Resolve the given type from the container.

Parameters

string $abstract
array $parameters

Return Value

mixed

Exceptions

BindingResolutionException

mixed get(string $id)

{@inheritdoc}

Parameters

string $id

Return Value

mixed

protected mixed resolve(string|callable $abstract, array $parameters = [], bool $raiseEvents = true)

Resolve the given type from the container.

Parameters

string|callable $abstract
array $parameters
bool $raiseEvents

Return Value

mixed

Exceptions

BindingResolutionException
CircularDependencyException

protected mixed getConcrete(string|callable $abstract)

Get the concrete type for a given abstract.

Parameters

string|callable $abstract

Return Value

mixed

protected Closure|string|array|null getContextualConcrete(string|callable $abstract)

Get the contextual concrete binding for the given abstract.

Parameters

string|callable $abstract

Return Value

Closure|string|array|null

protected Closure|string|null findInContextualBindings(string|callable $abstract)

Find the concrete binding for the given abstract in the contextual binding array.

Parameters

string|callable $abstract

Return Value

Closure|string|null

protected bool isBuildable(mixed $concrete, string $abstract)

Determine if the given concrete is buildable.

Parameters

mixed $concrete
string $abstract

Return Value

bool

mixed build(Closure|string $concrete)

Instantiate a concrete instance of the given type.

Parameters

Closure|string $concrete

Return Value

mixed

Exceptions

BindingResolutionException
CircularDependencyException

protected array resolveDependencies(array $dependencies)

Resolve all of the dependencies from the ReflectionParameters.

Parameters

array $dependencies

Return Value

array

Exceptions

BindingResolutionException

protected bool hasParameterOverride(ReflectionParameter $dependency)

Determine if the given dependency has a parameter override.

Parameters

ReflectionParameter $dependency

Return Value

bool

protected mixed getParameterOverride(ReflectionParameter $dependency)

Get a parameter override for a dependency.

Parameters

ReflectionParameter $dependency

Return Value

mixed

protected array getLastParameterOverride()

Get the last parameter override.

Return Value

array

protected mixed resolvePrimitive(ReflectionParameter $parameter)

Resolve a non-class hinted primitive dependency.

Parameters

ReflectionParameter $parameter

Return Value

mixed

Exceptions

BindingResolutionException

protected mixed resolveClass(ReflectionParameter $parameter)

Resolve a class based dependency from the container.

Parameters

ReflectionParameter $parameter

Return Value

mixed

Exceptions

BindingResolutionException

protected mixed resolveVariadicClass(ReflectionParameter $parameter)

Resolve a class based variadic dependency from the container.

Parameters

ReflectionParameter $parameter

Return Value

mixed

protected void notInstantiable(string $concrete)

Throw an exception that the concrete is not instantiable.

Parameters

string $concrete

Return Value

void

Exceptions

BindingResolutionException

protected void unresolvablePrimitive(ReflectionParameter $parameter)

Throw an exception for an unresolvable primitive.

Parameters

ReflectionParameter $parameter

Return Value

void

Exceptions

BindingResolutionException

void beforeResolving(Closure|string $abstract, Closure $callback = null)

Register a new before resolving callback for all types.

Parameters

Closure|string $abstract
Closure $callback

Return Value

void

void resolving(Closure|string $abstract, Closure $callback = null)

Register a new resolving callback.

Parameters

Closure|string $abstract
Closure $callback

Return Value

void

void afterResolving(Closure|string $abstract, Closure $callback = null)

Register a new after resolving callback for all types.

Parameters

Closure|string $abstract
Closure $callback

Return Value

void

protected void fireBeforeResolvingCallbacks(string $abstract, array $parameters = [])

Fire all of the before resolving callbacks.

Parameters

string $abstract
array $parameters

Return Value

void

protected void fireBeforeCallbackArray(string $abstract, array $parameters, array $callbacks)

Fire an array of callbacks with an object.

Parameters

string $abstract
array $parameters
array $callbacks

Return Value

void

protected void fireResolvingCallbacks(string $abstract, mixed $object)

Fire all of the resolving callbacks.

Parameters

string $abstract
mixed $object

Return Value

void

protected void fireAfterResolvingCallbacks(string $abstract, mixed $object)

Fire all of the after resolving callbacks.

Parameters

string $abstract
mixed $object

Return Value

void

protected array getCallbacksForType(string $abstract, object $object, array $callbacksPerType)

Get all callbacks for a given type.

Parameters

string $abstract
object $object
array $callbacksPerType

Return Value

array

protected void fireCallbackArray(mixed $object, array $callbacks)

Fire an array of callbacks with an object.

Parameters

mixed $object
array $callbacks

Return Value

void

array getBindings()

Get the container's bindings.

Return Value

array

string getAlias(string $abstract)

Get the alias for an abstract if available.

Parameters

string $abstract

Return Value

string

protected array getExtenders(string $abstract)

Get the extender callbacks for a given type.

Parameters

string $abstract

Return Value

array

void forgetExtenders(string $abstract)

Remove all of the extender callbacks for a given type.

Parameters

string $abstract

Return Value

void

protected void dropStaleInstances(string $abstract)

Drop all of the stale instances and aliases.

Parameters

string $abstract

Return Value

void

void forgetInstance(string $abstract)

Remove a resolved instance from the instance cache.

Parameters

string $abstract

Return Value

void

void forgetInstances()

Clear all of the instances from the container.

Return Value

void

void forgetScopedInstances()

Clear all of the scoped instances from the container.

Return Value

void

void flush()

Flush the container of all bindings and resolved instances.

Return Value

void

static Container getInstance()

Get the globally available instance of the container.

Return Value

Container

static Container|Container setInstance(Container $container = null)

Set the shared instance of the container.

Parameters

Container $container

Return Value

Container|Container

bool offsetExists(string $key)

Determine if a given offset exists.

Parameters

string $key

Return Value

bool

mixed offsetGet(string $key)

Get the value at a given offset.

Parameters

string $key

Return Value

mixed

void offsetSet(string $key, mixed $value)

Set the value at a given offset.

Parameters

string $key
mixed $value

Return Value

void

void offsetUnset(string $key)

Unset the value at a given offset.

Parameters

string $key

Return Value

void

mixed __get(string $key)

Dynamically access container services.

Parameters

string $key

Return Value

mixed

void __set(string $key, mixed $value)

Dynamically set container services.

Parameters

string $key
mixed $value

Return Value

void