Injector
A dependency injection class based on Pimple 3, (*1)
, (*2)
Author: Emmanuel Anticobr/
Last Modification: 2014/10/14br/
Version: 4.0, (*3)
br/, (*4)
Installation
br/
Installation is made via composer. Add the following lines to the composer.json file in your project., (*5)
br/
Composer, (*6)
{
"require": {
"injector/injector": "4.0.*"
}
}
br/, (*7)
Dependencies
br/
Injector requires the following packages to be installed:, (*8)
br/, (*9)
How to use
br/
Injector is a dependency injection library that uses the Pimple\Container class to initialize a set of properties within an instance. This is done by adding the appropiate annotations to the class declaration., (*10)
br/, (*11)
Step 1: Create a Provider, (*12)
A provider is a class that implements the Pimple\ServiceProviderInterface interface. This class sets a number of services (and parameters) inside a container., (*13)
<?php
namespace Acme\Providers;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Acme\Services\Logger;
use Acme\Services\MySQLConnection;
class MainProvider implements ServiceProviderInterface {
public function register(Container $container) {
//add some services
$container['logger'] = function ($c) {
return new Logger();
};
$container['conn'] = function ($c) {
return new MySQLConnection('usr', 'psw');
};
$container['environment'] = 'development';
}
}
br/, (*14)
Step 2: Configure your class, (*15)
In order to indicate a class provider we add the @Provider annotation followed by the provider class name. Dependencies can now be injected through the @Inject annotation. Notice that the syntax used for injecting contructor arguments differs a bit from the others., (*16)
<?php
namespace Acme\Components;
/**
* @Provider Acme\Providers\MainProvider
*/
class MyComponent {
private $name;
private $env;
/**
* @Inject logger
*/
private $logger;
/**
* @Inject conn
*/
private $connection;
/**
* @Inject($env) environment
*/
public function __construct($name, $env) {
$this->name = $name;
$this->env = $env;
}
public function getEnvironment() {
return $this->env;
}
public function getName() {
return $this->name;
}
public function getLogger() {
return $this->logger;
}
public function getConnection() {
return $this->connection;
}
}
br/, (*17)
Step 3: Create an instance, (*18)
Instances are created through the create static method in the Injector\Injector class. Additional arguments could be added as an array., (*19)
<?php
use Injector\Injector;
use Acme\Services\SQLiteConnection;
$component = Injector::create('Acme\Components\MyComponent', ['My Component']);
$component->getEnvironment(); //returns 'development'
$component->getName(); //returns 'My Component'
$component->getLogger()->debug('Component initialized');
//overriding a constructor parameter
$component = Injector::create('Acme\Components\MyComponent', ['My Component', 'production']);
$component->getEnvironment(); //returns 'production'
//filtering dependencies
$component = Injector::create('Acme\Components\MyComponent', ['My Component'], ['logger']);
$component->getLogger(); //Logger
$component->getConnection(); // NULL
//overriding dependencies
$component = Injector::create('Acme\Components\MyComponent', ['My Component'], null, ['conn' => new SQLiteConnection('file.db')]);
$component->getConnection(); // SQLiteConnection
br/, (*20)
Step 3 (alt): Inject dependencies, (*21)
You could also inject dependencies directly through the inject method using a custom made container., (*22)
<?php
use Injector\Injector;
//create container
$container = new Pimple\Container;
$provider = new Acme\Providers\MainProvider();
$provider->register($container);
//inject dependencies
$component = new CustomComponent();
Injector::inject($component, $container);
//...
$component->getLogger()->debug('Component initialized');
br/, (*23)
Subclasses, (*24)
Subclasses can extend the same set of providers from its parent class by adding the @ExtendInject annotation., (*25)
<?php
//A.php
/*
* Set class providers:
* @Provider MainProvider
* @Provider CustomProvider
*/
class A {
}
//B.php
/**
* Obtain providers from parent class:
* @ExtendInject
*/
class B extends A {
}
br/, (*26)
License
br/
This code is licensed under the BSD 2-Clause license., (*27)