Dependency Injection Container / Service Locator / Object Builder
This package is Recursive Dependency Injection Container (DiC) / Service Locator / Object Builder, (*1)
It has been designed to take it's dependency configuration map as part of it's construction,
so rather than setting up all the dependencies at runtime they can be loaded from a configuration file or files., (*2)
So as long as you can create an array, the configuration is injected at construction., (*3)
Main Features
- Build a Service/Object/Class based on a class name or alias
- Build a Class/Object that requires other dependencies
- Cache built Service/Object/Class for reuse in other classes (e.g. Database Connections)
- Build a Object/Class with a combination of passed and required dependencies
- Recursive Object Creation
Limitations
- Does not inject dependencies for methods other than __constructor
- Does not inject dependencies for setters
Requirements
Configuration Examples, (*6)
Usage
Create Container
``` PHP
$di = new KodCube\DependencyInjection\Container();, (*7)
or, (*8)
$di = new KodCube\DependencyInjection\Container($config);, (*9)
or, (*10)
$di = new KodCube\DependencyInjection\Container([
'MyAlias' => 'Vendor\Package\Class',
'Vendor\Package\Interface' => 'Vendor\Package\Class'
]), (*11)
### Get Object from Container using an alias
Using Alias
``` PHP
$object = $di->get('MyAlias');
or
$object = $di('MyAlias');
or
$object = $di->MyAlias();
Using Class Name
``` PHP
$object = $di->get('Vendor\Package\Class');, (*12)
or, (*13)
$object = $di('Vendor\Package\Class');, (*14)
Using Interface Name (requires dependency map)
``` PHP
$object = $di->get('Vendor\Package\Interface');
or
$object = $di('Vendor\Package\Interface');
Check if Alias\Class Exists in Container
``` PHP
Alias
$object = $di->has('MyAlias');, (*15)
Class
$object = $di->has('Vendor\Package\Class');, (*16)
### Make Object
Make a object using the DiC using passed arguments.
This will also take advantage of the autowiring properties of the container.
Note: Objects created with additional arguments are not cached by the container.
``` PHP
$object = $di->make('Vendor\Package\Class','argument1','argument2');