Wallogit.com
2017 © Pedro Peláez
Ion is a minimalist inversion of control container using automatic dependency injection
A minimalist inversion of control container using automatic dependency injection, (*1)
To install ION Container, you need to add it to your composer.json file, (*2)
{
"require": "terrydjony/ion"
}
Or, by using terminal, (*3)
composer require terrydjony/ion
First, you need to instantiate the container, (*4)
use Ion\Container; $ion = new Container();
To instantiate any objects, you should use make($class, array $args = array()), instead of new keyword, (*5)
Notice that the parameter two $args is optional
For example, if you want to instantiate stdClass class., (*6)
$stdClass = $ion->make('stdClass');
// $stdClass is an instance of stdClass
You can register the way to instantiate some class via register($key, $closure)
Here is an example of registering PDO class with some defined arguments, (*7)
$ion->register('PDO', function() {
return new PDO('mysql:host=localhost;dbname=databasename','root','');
};
$pdo = $ion->make('PDO');
// $pdo is an instance of PDO class with the arguments as defined before
The above example will register PDO service. So, if you instantiate it via $ion->make('PDO'), you don't need to supply any arguments since all the arguments have been defined before., (*8)
However, it will be nice to have a database config., (*9)
$config = array('dsn'=>'mysql:host=localhost;dbname=databasename','username'=>'root','password'=>'');
$ion->register('PDO', function() use ($config) {
return new PDO($config['dsn'],$config['username'],$username['password']);
});
$pdo = $ion->make('PDO');
And, notice that the first parameter of register is a key. The key can be anything, but it's recommended to use the class name as the key.
$ion->register('DAL', function() {
return new PDO($config['dsn'],$config['username'],$config['password']);
};
$dal = $ion->make('DAL');
// $dal is a PDO object
Notice that if you register a service, it'll be a shared instance. If you want to register a factory, use registerFactory, (*10)
You can register a factory using registerFactory($key, $closure), (*11)
$ion->registerFactory('CarFactory', function() {
return new Car();
};
$car1 = $ion->make('CarFactory');
$car2 = $ion->make('CarFactory');
// $car1 and $car2 are different objects
If you want to use the supplied arguments, you can use getArgs() method.
$this->ion->registerFactory('User', function($ion) {
return new User($ion->getArgs());
});
$user1 = $this->ion->make('User',array('Foo'));
// $user1 is an instance with argument Foo
You can also register parameters using setParam($param, $value) and get the value via param($param_name), (*12)
$ion->setParam('db_dsn','mysql:host=localhost;dbname=databasename');
$ion->setParam('db_user','root');
$ion->setParam('db_password','');
$ion->register('PDO', function($ion) {
return new PDO($ion->param('db_dsn'), $ion->param('db_user'), $ion->param('db_pass');
}
You can force a new instance using makeNew($key, $args=array())., (*13)
// register a PDO service before
$pdo1 = $ion->makeNew('PDO');
$pdo2 = $ion->makeNew('PDO');
// $pdo1 and $pdo2 are different objects no matter how the PDO service is registered
You can bind an interface to a class. So, if a class depends on that interface, that class will represent that interface, (*14)
If the class must be instantiated with some arguments, you should register it first. Then, you give the key as the parameter two of bindInterface, (*15)
But, if the class can be instantiated with no arguments, you can just specify the class name., (*16)
class A
{
public function __construct(DatabaseAdapterInterface $dba, StandardClassInterface $stdClass, StandardInterface $obj)
{
}
}
$ion->register('PDO', function() {
return new PDO('mysql:host=localhost;dbname=databasename','root','');
}
// Binds DatabaseAdapterInterface to PDO class which has been registered
$ion->bindInterface('DatabaseAdapterInterface', 'PDO');
// Binds StandardClassInterface to stdClass which needs no arguments for construction
$ion->bindInterface('StandardClassInterface', 'stdClass');
$a = $ion->make('A');
Suppose you want to instantiate a class A that needs class B which needs class C, (*17)
I mean something like, (*18)
class A
{
public function __construct(B $b)
{
}
}
class B
{
public function __construct(C $c)
{
}
}
class C
{
public function __construct()
{
}
}
And, to make a class A instance, you just need to:, (*19)
$a = $ion->make('A');
And, voila! You get the class A instance!, (*20)
If you have a dependency that needs some parameters, you can register the dependency first before making object that needs that dependency, (*21)
And, if the class needs some arguments, you can supply an array of arguments as parameter two of make or makeNew method., (*22)
class A
{
public function __construct(B $b, $name)
{
echo "Hello ".$name
}
}
class B
{
public function __construct(DatabaseAdapterInterface $dba)
{
}
}
$ion->register('PDO', function() {
return new PDO('mysql:host=localhost;dbname=databasename','root','');
});
$ion->bindInterface('DatabaseAdapterInterface','PDO');
$a = $ion->make('A',array('World'));
The class A instance is made automatically., (*23)