Download the PHP package dmt-software/di-plug without Composer
On this page you can find all versions of the php package dmt-software/di-plug. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dmt-software/di-plug
More information about dmt-software/di-plug
Files in dmt-software/di-plug
Package di-plug
Short Description An PHP dependency container abstraction
License MIT
Informations about the package di-plug
DI-Plug
Introduction
psr-11 introduced a standardized way for packages, libraries and frameworks to retrieve objects from containers. This package also allows adding dependencies to a container by a uniform interface without the use of a specific container implementation. This will help developers to access the dependency injection container to make installation of their packages easier.
Installation
Usage
Manual create the container
`
Autodetect
Use the container builder to wrap your container instance within the DMT\DependencyInjection\Container.
`
Autodiscovery
NOTE: When another dependency uses a supported container, autodiscovery might end up using a different container than expected.
The code can discover the dependency container installed.
`
Concepts
Instances without registration
If the Container::get() is called with a className that is not present in the container will create a new instance for
that class. This also counts for calls with made with constructor arguments.
| call | set | returns |
|---|---|---|
| Container::get(Some::class) | false | new instance |
| Container::get(Some::class) | true | from dependency |
| Container::get(Some::class, 12) | true | new instance |
NOTE: when the container creates a new instance it will NOT be set in the container. Not even when it is called with the same constructor arguments.
Configuration Discovery
When ConfigurationInterface is implemented and available in the container, the constructor arguments can be injected
from the configuration using the ConfigValue attribute.
The value for the bar in the constructor is fetched from configuration when it is omitted from the container::get
call.
Auto Inject Container
Instead of injection all the dependencies, one can choose to inject just the container and resolve the dependencies when
needed. This can easily be achieved by using the HasContainer trait.
This even works when the HasContainer is used within another trait, but just one level deep.
Using the trait below f.i. enables getTwigEngine() call to retrieve the template engine in the parent class.
ServiceProvider
A service provider can be used to register or change dependencies in the container.