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(string|array $hint)

Parse the hint parameter.

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

Register a type hint.

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

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

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

Register a singleton instance.

registerContextualDependency(string $class, 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, string|Closure $class, bool $singleton = false)

Replaces a registered type hint.

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

Replaces a registered singleton type hint.

replaceInstance(string $hint, object $instance)

Replaces a singleton instance.

string|Closure
resolveHint(string $hint)

Resolves a type hint.

string
resolveContextualDependency(string $class, 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)

Resolve a parameter.

array
resolveParameters(array $reflectionParameters, array $providedParameters, ReflectionClass|null $class = 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(string|Closure $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(string|array $hint)

Parse the hint parameter.

Parameters

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

Return Value

string

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

Register a type hint.

Parameters

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

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

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

Parameters

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

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

Register a singleton instance.

Parameters

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

at line 128
registerContextualDependency(string $class, string $interface, string $implementation)

Registers a contextual dependency.

Parameters

string $class Class
string $interface Interface
string $implementation Implementation

at line 139
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 149
protected replaceInstances(string $hint)

Replaces previously resolved instances.

Parameters

string $hint Type hint

at line 169
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 183
replace(string $hint, string|Closure $class, bool $singleton = false)

Replaces a registered type hint.

Parameters

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

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

Replaces a registered singleton type hint.

Parameters

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

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

Replaces a singleton instance.

Parameters

string $hint Type hint
object $instance Class instance

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

Resolves a type hint.

Parameters

string $hint Type hint

Return Value

string|Closure

at line 251
protected string resolveContextualDependency(string $class, string $interface)

Resolves a contextual dependency.

Parameters

string $class Class
string $interface Interface

Return Value

string

at line 263
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 294
protected string getDeclaringFunction(ReflectionParameter $parameter)

Returns the name of the declaring function.

Parameters

ReflectionParameter $parameter ReflectionParameter instance

Return Value

string

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

Resolve a parameter.

Parameters

ReflectionParameter $parameter ReflectionParameter instance
ReflectionClass|null $class ReflectionClass instance

Return Value

mixed

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

Resolve parameters.

Parameters

array $reflectionParameters Reflection parameters
array $providedParameters Provided Parameters
ReflectionClass|null $class ReflectionClass instance

Return Value

array

at line 405
protected bool isContainerAware(object $class)

Checks if a class is container aware.

Parameters

object $class Class instance

Return Value

bool

at line 419
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 433
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 467
object factory(string|Closure $class, array $parameters = [])

Creates a class instance.

Parameters

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

Return Value

object

at line 498
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 511
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 522
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 537
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 571
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 583
mixed call(callable $callable, array $parameters = [])

Execute a callable and inject its dependencies.

Parameters

callable $callable Callable
array $parameters Parameters

Return Value

mixed