com\setasign\SetaFramework\Di

GlobalDi Class GlobalDi - Singleton

File: /SetaFramework/src/Di/GlobalDi.php

This singleton will be used by the Bootstrap.

Class hierarchy

Summary

Static Properties

$instance

static private ?GlobalDi GlobalDi::$instance

The singleton instance


Properties

$aliases

protected array Di::$aliases = array()

An array of aliases configurations

$configures

protected array Di::$configures = array()

An array of classes/interfaces configurations

$instances

protected array<string, null|object> Di::$instances = array()

An internal cache of singleton instances


Static Methods

getInstance()

public static GlobalDi::getInstance (
void
): GlobalDi

Get the singleton instance

isInitialized()

public static GlobalDi::isInitialized (
void
): bool

Returns whether the global di is even be used.

resetInstance()

public static GlobalDi::resetInstance (
void
): void

Resets the global di instance.


Methods

__construct()

private GlobalDi::__construct (
void
)

__sleep()

public Di::__sleep (
void
): array

__wakeup()

public GlobalDi::__wakeup (
void
): void

addAlias()

public Di::addAlias (
string $name,
string|callable $classNameOrCallback,
array $params = array ( ),
bool $singleton = true
): self

Add an alias configuration

Parameters
$name : string
 
$classNameOrCallback : string|callable
 
$params : array
 
$singleton : bool
 
Exceptions

Throws AlreadyDefinedException

addAliasWithInstancedObject()

public Di::addAliasWithInstancedObject (
string $name,
?object $instance
): self
Parameters
$name : string
 
$instance : ?object
 

call()

public Di::call (
callable $callable,
array $parameters = array ( )
): ?mixed

Call the callable and autowire the params.

Parameters
$callable : callable
 
$parameters : array
 
Return Values

Result of the callable

Exceptions

Throws \ReflectionException

callNewInstance()

public Di::callNewInstance (
class-string<T> $className,
array $parameters = array ( )
): T

Create a new instance of $className and autowire the params.

Parameters
$className : class-string<T>
 
$parameters : array
 
Exceptions

Throws \ReflectionException

clearInstance()

public Di::clearInstance (
string $classNameOrAlias
): self
Parameters
$classNameOrAlias : string
 

configure()

public Di::configure (
string $className,
array $params = array ( ),
bool $singleton = true
): self

Add a class/interface configuration

Parameters
$className : string
 
$params : array
 
$singleton : bool
 
Exceptions

Throws AlreadyDefinedException

get()

public Di::get (
string|class-string<T> $classNameOrAlias,
array $params = array ( ),
?bool $singleton = null
): object|T

Get an instance by class name or alias.

If singleton is false, Di will create everytime a new object.

Di will remember up to one instance of every class/alias (except $singleton is false).

If you use an alias or a class that has a configuration, $params will be merged with the params of the configuration or alias.

If the di has already an instance, the $params will be ignored because no new object is created.

Parameters
$classNameOrAlias : string|class-string<T>
 
$params : array
 
$singleton : ?bool

Use null to use the configured value, or true/false to affect this behaviour

Exceptions

Throws UnknownClassOrAliasException if alias wasn't found.

Throws DiClassConstructionException if an exception occurs on constructing a new instance.

Throws \com\setasign\SetaFramework\Bootstrap\MaintenanceException

getArguments()

private Di::getArguments (
array<string, ReflectionParameter> $reflectionParameters,
array $parameters
): array
Parameters
$reflectionParameters : array<string, ReflectionParameter>
 
$parameters : array
 

getByAlias()

private Di::getByAlias (
string $name,
array $params,
?bool $singleton
): ?object

Get an instance by an alias

If the alias is definied to be a singleton the $params array is ignored if the instance was already created.

Parameters
$name : string
 
$params : array
 
$singleton : ?bool

Use null to use the configured value, or true/false to affect this behaviour

Exceptions

Throws UnknownClassOrAliasException if alias wasn't found.

Throws DiClassConstructionException if an exception occurs on constructing a new instance.

Throws \com\setasign\SetaFramework\Bootstrap\MaintenanceException

getByClassName()

private Di::getByClassName (
class-string<T> $className,
array $params,
?bool $singleton
): T

Get an instance by a class name

If the class is definied to be a singleton the $params array is ignored if the instance was already created.

Parameters
$className : class-string<T>
 
$params : array
 
$singleton : ?bool

Use null to use the configured value, or true/false to affect this behaviour

Exceptions

Throws UnknownClassOrAliasException if alias wasn't found.

Throws DiClassConstructionException if an exception occurs on constructing a new instance.

Throws \com\setasign\SetaFramework\Bootstrap\MaintenanceException

getReflectionParameters()

private Di::getReflectionParameters (
array{0: class-string|object, 1: string}|callable $callable
): array
Parameters
$callable : array{0: class-string|object, 1: string}|callable
 
Exceptions

Throws \ReflectionException

hasAlias()

public Di::hasAlias (
string $name
): bool

Checks whether the di contains a alias with this name

Parameters
$name : string
 

hasConfiguration()

public Di::hasConfiguration (
string $className
): bool

Checks whether the di has a configuration for this class

Parameters
$className : string
 

hasInstance()

public Di::hasInstance (
string $aliasOrClassname
): bool

Checks whether the di has an instance for this alias or className

Parameters
$aliasOrClassname : string
 

parentInjections()

public Di::parentInjections (
string $parentClassName,
array $parameters = array ( )
): array
Parameters
$parentClassName : string
 
$parameters : array
 
Exceptions

Throws \ReflectionException

prepareParams()

private Di::prepareParams (
array $params,
array $defaultParams = array ( )
): array

Prepares inject parameters

Internally aliases will be automatically resolved.

Parameters
$params : array
 
$defaultParams : array
 
Exceptions

Throws UnknownClassOrAliasException

purgeName()

protected Di::purgeName (
string $name
): string
Parameters
$name : string
 

remove()

public Di::remove (
string $classNameOrAlias
): self

Removes all added configurations, aliases and instances with this name.

Parameters
$classNameOrAlias : string