Injector: Inject dependencies defined in annotation comments

Recommend this page to a friend!
  Info   View files Documentation   View files View files (34)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 98 All time: 9,397 This week: 904Up
Version License PHP version Categories
injector 3.1BSD License5.4PHP 5, Language, Design Patterns
Description Author

This package can inject dependencies defined in annotation comments.

It implements the service provider interface defined by the Pimple package to define how each needed service is instantiated.

The dependencies of each class are defined in annotations described as comments in the class code.

The injector class instantiates the classes and initializes the necessary dependencies.

Dependencies can be injected directly using custom made containers.

Name: Emmanuel Antico <contact>
Classes: 3 packages by
Country: Argentina Argentina
Age: ???
All time rank: 346350 in Argentina Argentina
Week rank: 730 Up10 in Argentina Argentina Up

Details
Injector
========

A dependency injection class based on Pimple 2

**Author**: Emmanuel Antico<br/>
**Last Modification**: 2014/07/02<br/>
**Version**: 3.1.0

<br/>
Installation
------------
<br/>
Installation is made via composer. Add the following lines to the composer.json file in your project.

<br/>
**Composer**

```json
{
    "require": {
		"injector/injector": "~3.0"
	}
}
```

<br/>
Dependecies
------------
<br/>
Injector requires the following packages to be installed:

* [Pimple 2.1](https://github.com/fabpot/Pimple "")
* [Minime\Annotations 1.13](https://github.com/marcioAlmada/annotations "")

<br/>
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.

<br/>
>Step 1: Create a Provider

A provider is a class that implements the *Pimple\ServiceProviderInterface* interface. This class sets a number of services (and parameters) inside a container.

```php
<?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/>
>Step 2: Configure your class

In order to indicate a class provider we add the @inject.provider annotation followed by the provider class name. Dependencies can now be injected through the @inject.service and @inject.param annotations.

```php
<?php
namespace Acme\Components;

/**
 * @inject.provider Acme\Providers\MainProvider
 */
class MyComponent {
    private $name;
    private $env;    
    
    /**
     * @inject.service logger
     */
    private $logger;
    
    /**
     * @inject.service conn
     */
    private $connection;
    
    /**
     * @inject.param $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/>
>Step 3: Create an instance

Instances are created through the *create* static method in the *Injector\Injector* class. Additional arguments could be added as an array.

```php
<?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/>
>Step 3 (alt): Inject dependencies

You could also inject dependencies directly through the *inject* method using a custom made container.

```php
<?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/>
License
-------
<br/>
This code is licensed under the BSD 2-Clause license.
  Files folder image Files  
File Role Description
Files folder imagelib (1 directory)
Files folder imagetests (1 file, 2 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. Auxiliary data
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Auxiliary data

  Files folder image Files  /  lib  
File Role Description
Files folder imageInjector (3 files)

  Files folder image Files  /  lib  /  Injector  
File Role Description
  Accessible without login Plain text file ClassProfile.php Class Class source
  Accessible without login Plain text file Injector.php Class Class source
  Accessible without login Plain text file Profiler.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageclasses (1 directory)
Files folder imageInjector (3 files)
  Accessible without login Plain text file bootstrap.php Aux. Unit test script

  Files folder image Files  /  tests  /  classes  
File Role Description
Files folder imageAcme (3 directories)

  Files folder image Files  /  tests  /  classes  /  Acme  
File Role Description
Files folder imageComponents (14 files)
Files folder imageProviders (4 files)
Files folder imageServices (5 files)

  Files folder image Files  /  tests  /  classes  /  Acme  /  Components  
File Role Description
  Accessible without login Plain text file ExampleComponent.php Test Unit test script
  Accessible without login Plain text file TestComponentA.php Test Unit test script
  Accessible without login Plain text file TestComponentB.php Test Unit test script
  Accessible without login Plain text file TestComponentC.php Test Unit test script
  Accessible without login Plain text file TestComponentD.php Test Unit test script
  Accessible without login Plain text file TestComponentE.php Test Unit test script
  Accessible without login Plain text file TestComponentF.php Test Unit test script
  Accessible without login Plain text file TestComponentG.php Test Unit test script
  Accessible without login Plain text file TestComponentH.php Test Unit test script
  Accessible without login Plain text file TestComponentI.php Test Unit test script
  Accessible without login Plain text file TestComponentJ.php Test Unit test script
  Accessible without login Plain text file TestComponentX.php Test Unit test script
  Accessible without login Plain text file TestComponentY.php Test Unit test script
  Accessible without login Plain text file TestComponentZ.php Test Unit test script

  Files folder image Files  /  tests  /  classes  /  Acme  /  Providers  
File Role Description
  Accessible without login Plain text file AllServiceProvider.php Test Unit test script
  Accessible without login Plain text file ExampleProvider.php Test Unit test script
  Accessible without login Plain text file HTTPServiceProvider.php Test Unit test script
  Accessible without login Plain text file MailServiceProvider.php Test Unit test script

  Files folder image Files  /  tests  /  classes  /  Acme  /  Services  
File Role Description
  Accessible without login Plain text file HTTPService.php Test Unit test script
  Accessible without login Plain text file Logger.php Test Unit test script
  Accessible without login Plain text file MailService.php Test Unit test script
  Accessible without login Plain text file MySQLConnection.php Test Unit test script
  Accessible without login Plain text file SQLiteConnection.php Test Unit test script

  Files folder image Files  /  tests  /  Injector  
File Role Description
  Accessible without login Plain text file ClassProfileTest.php Test Unit test script
  Accessible without login Plain text file ContainerTest.php Test Unit test script
  Accessible without login Plain text file ExampleTest.php Test Unit test script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:98
This week:0
All time:9,397
This week:904Up

For more information send a message to info at phpclasses dot org.