Download the PHP package aolbrich/php-di-container without Composer
On this page you can find all versions of the php package aolbrich/php-di-container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aolbrich/php-di-container
More information about aolbrich/php-di-container
Files in aolbrich/php-di-container
Package php-di-container
Short Description PHP Dependency Injection cotainer
License MIT
Informations about the package php-di-container
Constructor Dependency Injection container
This class implements a simple constructor dependency injection container
Installation:
Basic Usage:
If the class constructor implements a callable class then it will be auto wired, no mapping required. For example:
Implementation:
Returns new class and autowire all methods where the annotation says @atowire
(Note: The new class will not return with the same class name, or not even inherited from the original class)
Using array to set the resolver definitions
It is also possible to pass the definitions to the class via it's constructor, or calling the setDefinitions method instad of using the set
method.
It makes possible to set multiple definitions at one:
Example:
OR
The class:
Usage with interface resolution:
If the class implements a constuctor injection with interface type hint, then the container cannot resolve the depenceny automatically, therefore mapping should be provided by the set method. Mapping can be done by interface and class names, or interface name and closure as well.
The following two example illustrate those solutions:
Implementation:
Same implementation using closure:
Setter Autowire
It is also possible to auto wire setters, The requirement for auto wiring setters are:
- The method have to be public
- The method name have to start with "set" like "setLogger()"
- To auto wire, you need to add the @autowire annotations.
There can be any amount of setters
Example class wiring two dependencies at one setter.
Add primitive parameters
You can add extra parameters to your class resolve to bind:
Examlpe:
Class implementation:
Singleton creation support
Class can be created as signletor by using the sigleton() function or auto wire with closure. Examples:
Run the unit test
Tools
Run code quality check:
Note
This is not a full implementation of a dependency injection container. It resolves only constructor dependencies.
Features missing / will be added
Constructor InjectionSetter InjectionMethod InjectionSingleton supportProperty Injection(it will not be done, as it does not considered as a goop practice any more)Injecting primitive paramater values- Caching
- Aliases
- Annotations (partly implemented with @autowire keywords in setters)
- Immutable-setter Injection
- Only basic circular reference check added, improve to check for example, if ClassA depends on ClassB, and ClassB depends on ClassC, which in turn depends on ClassA, the code would not detect this circular reference. This can be addressed by maintaining a stack of dependencies and checking for cycles in the stack.
Licence
MIT licence