2017 © Pedro Peláez
 

library injector

A dependency injection class based on Pimple

image

injector/injector

A dependency injection class based on Pimple

  • Tuesday, October 14, 2014
  • by emaphp
  • Repository
  • 1 Watchers
  • 3 Stars
  • 42 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 2 % Grown

The README.md

Injector

A dependency injection class based on Pimple 3, (*1)

Build Status, (*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)

The Versions

14/10 2014

dev-master

9999999-dev

A dependency injection class based on Pimple

  Sources   Download

BSD 2-Clause

The Requires

 

by Emmanuel Antico

dependency injection pimple injector

14/10 2014

4.0

4.0.0.0

A dependency injection class based on Pimple

  Sources   Download

BSD 2-Clause

The Requires

 

by Emmanuel Antico

dependency injection pimple injector

02/07 2014

3.1

3.1.0.0

A dependency injection class based on Pimple

  Sources   Download

BSD 2-Clause

The Requires

 

by Emmanuel Antico

dependency injection pimple injector

28/06 2014

3.0

3.0.0.0

A dependency injection class based on Pimple

  Sources   Download

BSD 2-Clause

The Requires

 

by Emmanuel Antico

dependency injection pimple injector

25/08 2013

v2.0.0

2.0.0.0

A dependency injection class based on Pimple

  Sources   Download

BSD 2-Clause

The Requires

 

by Emmanuel Antico

dependency injection pimple injector

24/07 2013

v1.1.1

1.1.1.0

A dependency injection class based on Pimple

  Sources   Download

BSD 2-Clause

The Requires

 

by Emmanuel Antico

dependency injection pimple injector

24/07 2013

v1.1.0

1.1.0.0

A dependency injection class based on Pimple

  Sources   Download

BSD 2-Clause

The Requires

 

by Emmanuel Antico

dependency injection pimple injector

02/07 2013

v1.0.0

1.0.0.0

A dependency injection class based on Pimple

  Sources   Download

BSD 2-Clause

The Requires

 

by Emmanuel Antico

dependency injection pimple injector