class Container

Inversion of control container.

Properties

protected array $hints Registered type hints.
protected array $aliases Aliases.
protected array $instances Singleton instances.
protected array $contextualDependencies Contextual dependencies.
protected array $replacers Instance replacers.

Methods

string
parseHint(array|string $hint)

Parse the hint parameter.

register(array|string $hint, Closure|string $class, bool $singleton = false)

Register a type hint.

registerSingleton(array|string $hint, Closure|string $class)

Register a type hint and return the same instance every time.

registerInstance(array|string $hint, object $instance)

Register a singleton instance.

registerContextualDependency(array|string $dependent, string $interface, string $implementation)

Registers a contextual dependency.

string
resolveAlias(string $alias)

Return the name based on its alias. If no alias exists then we'll just return the value we received.

replaceInstances(string $hint)

Replaces previously resolved instances.

onReplace(string $hint, callable $replacer, string|null $eventName = null)

Registers replacers.

replace(string $hint, Closure|string $class, bool $singleton = false)

Replaces a registered type hint.

replaceSingleton(string $hint, Closure|string $class)

Replaces a registered singleton type hint.

replaceInstance(string $hint, object $instance)

Replaces a singleton instance.

Closure|string
resolveHint(string $hint)

Resolves a type hint.

string
resolveContextualDependency(string $dependent, string $interface)

Resolves a contextual dependency.

array
mergeParameters(array $reflectionParameters, array $providedParameters)

Merges the provided parameters with the reflection parameters.

string
getDeclaringFunction(ReflectionParameter $parameter)

Returns the name of the declaring function.

mixed
resolveParameter(ReflectionParameter $parameter, ReflectionClass|null $class = null, string|null $method = null)

Resolve a parameter.

array
resolveParameters(array $reflectionParameters, array $providedParameters, ReflectionClass|null $class = null, string|null $method = null)

Resolve parameters.

bool
isContainerAware(object $class)

Checks if a class is container aware.

object
closureFactory(Closure $factory, array $parameters)

Creates a class instance using a factory closure.

object
reflectionFactory(string $class, array $parameters)

Creates a class instance using reflection.

object
factory(Closure|string $class, array $parameters = [])

Creates a class instance.

bool
has(string $class)

Returns TRUE if the class is registered in the container and FALSE if not.

bool
hasInstanceOf(string $class)

Returns TRUE if there's an instance of the class in the container and FALSE if not.

bool
isSingleton(string $class)

Returns TRUE if a class has been registered as a singleton and FALSE if not.

object
get(string $class, array $parameters = [], bool $reuseInstance = true)

Returns a class instance.

object
getFresh(string $class, array $parameters = [])

Returns a fresh class instance even if the class is registered as a singleton.

mixed
call(callable $callable, array $parameters = [])

Execute a callable and inject its dependencies.

Details

at line 75
protected string parseHint(array|string $hint)

Parse the hint parameter.

Parameters

array|string $hint Type hint or array contaning both type hint and alias

Return Value

string

at line 94
register(array|string $hint, Closure|string $class, bool $singleton = false)

Register a type hint.

Parameters

array|string $hint Type hint or array contaning both type hint and alias
Closure|string $class Class name or closure
bool $singleton Should we return the same instance every time?

at line 105
registerSingleton(array|string $hint, Closure|string $class)

Register a type hint and return the same instance every time.

Parameters

array|string $hint Type hint or array contaning both type hint and alias
Closure|string $class Class name or closure

at line 116
registerInstance(array|string $hint, object $instance)

Register a singleton instance.

Parameters

array|string $hint Type hint or array contaning both type hint and alias
object $instance Class instance

at line 128
registerContextualDependency(array|string $dependent, string $interface, string $implementation)

Registers a contextual dependency.

Parameters

array|string $dependent Class name or an array containing a class and method name
string $interface Interface name
string $implementation Implementation name

at line 144
protected string resolveAlias(string $alias)

Return the name based on its alias. If no alias exists then we'll just return the value we received.

Parameters

string $alias Alias

Return Value

string

at line 154
protected replaceInstances(string $hint)

Replaces previously resolved instances.

Parameters

string $hint Type hint

at line 174
onReplace(string $hint, callable $replacer, string|null $eventName = null)

Registers replacers.

Parameters

string $hint Type hint
callable $replacer Instance replacer
string|null $eventName Event name

at line 188
replace(string $hint, Closure|string $class, bool $singleton = false)

Replaces a registered type hint.

Parameters

string $hint Type hint
Closure|string $class Class name or closure
bool $singleton Are we replacing a singleton?

at line 213
replaceSingleton(string $hint, Closure|string $class)

Replaces a registered singleton type hint.

Parameters

string $hint Type hint
Closure|string $class Class name or closure

at line 224
replaceInstance(string $hint, object $instance)

Replaces a singleton instance.

Parameters

string $hint Type hint
object $instance Class instance

at line 244
protected Closure|string resolveHint(string $hint)

Resolves a type hint.

Parameters

string $hint Type hint

Return Value

Closure|string

at line 256
protected string resolveContextualDependency(string $dependent, string $interface)

Resolves a contextual dependency.

Parameters

string $dependent Class name or class name and method name separated by a double colon
string $interface Interface

Return Value

string

at line 268
protected array mergeParameters(array $reflectionParameters, array $providedParameters)

Merges the provided parameters with the reflection parameters.

Parameters

array $reflectionParameters Reflection parameters
array $providedParameters Provided parameters

Return Value

array

at line 299
protected string getDeclaringFunction(ReflectionParameter $parameter)

Returns the name of the declaring function.

Parameters

ReflectionParameter $parameter ReflectionParameter instance

Return Value

string

at line 324
protected mixed resolveParameter(ReflectionParameter $parameter, ReflectionClass|null $class = null, string|null $method = null)

Resolve a parameter.

Parameters

ReflectionParameter $parameter ReflectionParameter instance
ReflectionClass|null $class ReflectionClass instance
string|null $method Metod name

Return Value

mixed

at line 382
protected array resolveParameters(array $reflectionParameters, array $providedParameters, ReflectionClass|null $class = null, string|null $method = null)

Resolve parameters.

Parameters

array $reflectionParameters Reflection parameters
array $providedParameters Provided Parameters
ReflectionClass|null $class ReflectionClass instance
string|null $method Method name

Return Value

array

at line 414
protected bool isContainerAware(object $class)

Checks if a class is container aware.

Parameters

object $class Class instance

Return Value

bool

at line 428
protected object closureFactory(Closure $factory, array $parameters)

Creates a class instance using a factory closure.

Parameters

Closure $factory Class name or closure
array $parameters Constructor parameters

Return Value

object

at line 442
protected object reflectionFactory(string $class, array $parameters)

Creates a class instance using reflection.

Parameters

string $class Class name
array $parameters Constructor parameters

Return Value

object

at line 476
object factory(Closure|string $class, array $parameters = [])

Creates a class instance.

Parameters

Closure|string $class Class name or closure
array $parameters Constructor parameters

Return Value

object

at line 507
bool has(string $class)

Returns TRUE if the class is registered in the container and FALSE if not.

Parameters

string $class Class name

Return Value

bool

at line 520
bool hasInstanceOf(string $class)

Returns TRUE if there's an instance of the class in the container and FALSE if not.

Parameters

string $class Class name

Return Value

bool

at line 531
bool isSingleton(string $class)

Returns TRUE if a class has been registered as a singleton and FALSE if not.

Parameters

string $class Class name

Return Value

bool

at line 546
object get(string $class, array $parameters = [], bool $reuseInstance = true)

Returns a class instance.

Parameters

string $class Class name
array $parameters Constructor parameters
bool $reuseInstance Reuse existing instance?

Return Value

object

at line 580
object getFresh(string $class, array $parameters = [])

Returns a fresh class instance even if the class is registered as a singleton.

Parameters

string $class Class name
array $parameters Constructor parameters

Return Value

object

at line 592
mixed call(callable $callable, array $parameters = [])

Execute a callable and inject its dependencies.

Parameters

callable $callable Callable
array $parameters Parameters

Return Value

mixed