Moodle PHP Documentation 4.5
Moodle 4.5dev (Build: 20240606) (d3ae1391abe)
DI\Container Class Reference
Inheritance diagram for DI\Container:
Psr\Container\ContainerInterface DI\FactoryInterface Invoker\InvokerInterface DI\CompiledContainer

Public Member Functions

 __construct (array|MutableDefinitionSource $definitions=[], ProxyFactory $proxyFactory=null, ContainerInterface $wrapperContainer=null)
 Use $container = new Container() if you want a container with the default configuration.
 
 call ($callable, array $parameters=[])
 Call the given function using the given parameters.
 
 debugEntry (string $name)
 Get entry debug information.
 
 get (string $id)
 Returns an entry of the container by its name.
 
 getKnownEntryNames ()
 Get defined container entries.
 
 has (string $id)
 Returns true if the container can return an entry for the given identifier.
 
 injectOn (object $instance)
 Inject all dependencies on an existing instance.
 
 make (string $name, array $parameters=[])
 Build an entry of the container by its name.
 
 set (string $name, mixed $value)
 Define an object or a value in the container.
 

Static Public Member Functions

static create (array $definitions)
 

Protected Member Functions

 setDefinition (string $name, Definition $definition)
 

Protected Attributes

ContainerInterface $delegateContainer
 Container that wraps this container.
 
array $entriesBeingResolved = []
 Array of entries being resolved.
 
ProxyFactory $proxyFactory
 
array $resolvedEntries = []
 Map of entries that are already resolved.
 

Constructor & Destructor Documentation

◆ __construct()

DI\Container::__construct ( array|MutableDefinitionSource $definitions = [],
ProxyFactory $proxyFactory = null,
ContainerInterface $wrapperContainer = null )

Use $container = new Container() if you want a container with the default configuration.

If you want to customize the container's behavior, you are discouraged to create and pass the dependencies yourself, the ContainerBuilder class is here to help you instead.

See also
ContainerBuilder
Parameters
ContainerInterface$wrapperContainerIf the container is wrapped by another container.

Member Function Documentation

◆ call()

DI\Container::call ( $callable,
array $parameters = [] )

Call the given function using the given parameters.

Missing parameters will be resolved from the container.

Parameters
callable | array | string$callableFunction to call.
array$parametersParameters to use. Can be indexed by the parameter names or not indexed (same order as the parameters). The array can also contain DI definitions, e.g. DI\get().
Return values
mixedResult of the function.

Implements Invoker\InvokerInterface.

◆ debugEntry()

DI\Container::debugEntry ( string $name)

Get entry debug information.

Parameters
string$nameEntry name
Exceptions
InvalidDefinition
NotFoundException

◆ get()

DI\Container::get ( string $id)

Returns an entry of the container by its name.

@template T

Parameters
string|class-string<T>$id Entry name or a class name.
Return values
mixed|T
Exceptions
DependencyExceptionError while resolving the entry.
NotFoundExceptionNo entry found for the given name.

Implements Psr\Container\ContainerInterface.

Reimplemented in DI\CompiledContainer.

◆ getKnownEntryNames()

DI\Container::getKnownEntryNames ( )

Get defined container entries.

Return values
string[]

◆ has()

DI\Container::has ( string $id)

Returns true if the container can return an entry for the given identifier.

Returns false otherwise.

has($id) returning true does not mean that get($id) will not throw an exception. It does however mean that get($id) will not throw a NotFoundExceptionInterface.

Parameters
string$idIdentifier of the entry to look for.
Return values
bool

Implements Psr\Container\ContainerInterface.

Reimplemented in DI\CompiledContainer.

◆ injectOn()

DI\Container::injectOn ( object $instance)

Inject all dependencies on an existing instance.

@template T

Parameters
object | T$instanceObject to perform injection upon
Return values
object|T\$instanceReturns the same instance
Exceptions
InvalidArgumentException
DependencyExceptionError while injecting dependencies

◆ make()

DI\Container::make ( string $name,
array $parameters = [] )

Build an entry of the container by its name.

This method behave like get() except resolves the entry again every time. For example if the entry is a class then a new instance will be created each time.

This method makes the container behave like a factory.

@template T

Parameters
string|class-string<T>$name Entry name or a class name.
array$parametersOptional parameters to use to build the entry. Use this to force specific parameters to specific values. Parameters not defined in this array will be resolved using the container.
Return values
mixed|T
Exceptions
InvalidArgumentExceptionThe name parameter must be of type string.
DependencyExceptionError while resolving the entry.
NotFoundExceptionNo entry found for the given name.

Implements DI\FactoryInterface.

◆ set()

DI\Container::set ( string $name,
mixed $value )

Define an object or a value in the container.

Parameters
string$nameEntry name
mixed | DefinitionHelper$valueValue, use definition helpers to define objects

Member Data Documentation

◆ $delegateContainer

ContainerInterface DI\Container::$delegateContainer
protected

Container that wraps this container.

If none, points to $this.

◆ $entriesBeingResolved

array DI\Container::$entriesBeingResolved = []
protected

Array of entries being resolved.

Used to avoid circular dependencies and infinite loops.


The documentation for this class was generated from the following file: