2017 © Pedro Peláez
 

library di

A dynamic dependency injection manager

image

pierredup/di

A dynamic dependency injection manager

  • Wednesday, January 8, 2014
  • by pierredup
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 4 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Dynamic Dependency Injection

Build Status Scrutinizer Quality Score Code Coverage, (*1)

This library provides a class to handle dependency injection dynamically., (*2)

Dynamic dependency injection means you don't have to define services or define which dependencies needs to be injected. The dependencies is dynamically created based on the constructor parameters., (*3)

This means you only need to add type-hints for the classes you want to inject into your class constructor., (*4)

Installation

Composer

To install this library using composer, add the following to your composer.json:, (*5)

{
    "require": {
        "pierredup/di": "*"
    }
}

Make sure you are using composer's autoloader to include the file:, (*6)

require 'vendor/autoload.php';

Download the file

Download Di.phar from the repo and save the file into your project path somewhere., (*7)

require 'path/to/Di.phar';

Usage

Getting a class with dependencies

namespace Foo {

    class Bar
    {
        public function __construct(Baz $baz)
        {
            // ...
        }
    }

    class Baz {

    }
}

$object = Di::get('Foo\Bar');

var_dump($object);

This will give you an instance of Foo\Bar with the Baz class dynamically created and injection into the constructor., (*8)

Getting a new instance of a class

By default, the same instance for each class will be returned every time you call Di::get If you want to return a new instance of a class, you need to pass a second parameter to the get method, (*9)

    $object = Di::get('Foo\Bar', Di::NEW_INSTANCE);

This will return a new instance of the Foo\Bar class, but the dependencies will always return the same instance., (*10)

If you want to ensure that every object returns a new instance, you need to pass the Di::DEEP flag:, (*11)

    $object = Di::get('Foo\Bar', Di::DEEP);

This will return a new instance of Foo\Bar, as well as new instances for each dependency., (*12)

Parameters

Setting parameters

If you have values in your constructor that can not be type-hinted by an object ( E.G database settings), you use can use the map method to set the values for those parameters:, (*13)

class Db
{
    public function __construct($host, $username, $password)
    {
        // ...
    }
}

Di::map(array(
    'host'      => 'localhost',
    'username'  => 'user',
    'host'      => 'password',
));

$object = Di::get('Db');

Note: If you don't specify a value for a parameter that doesn't have a type-hint, NULL will be passed as the value. If you set a default value for the parameter, then the default value will be used instead., (*14)

Lazy loading parameters

If you want parameters to be lazy-loaded (only load when needed, E.G get value from a database or a session ), you can use a closure or valid callback as the parameter:, (*15)

Di::map(array(
    'parameter' => function() {
        return Di::get('Foo\Database')->getValueFromDb()
    }
));

// OR

Di::map(array(
    'parameter' => array($db, 'getValueFromDb')
));

The function or callback will only be executed when the parameter is needed, and the value will then be cached for any further calls., (*16)

Getting parameters

To get a parameter value, just pass the Di::PARAM flag to the get method:, (*17)

Di::map(array(
    'host'      => 'localhost',
    'username'  => 'user',
    'host'      => 'password',
));

$host = Di::get('host', Di::PARAM); // return 'localhost'

Over writing classes

You can also use the map method to over write a class name. This is useful if you want to over write a core class of a library with your own implementation. Note that your class needs to extend from the class you are over writing, otherwise you might get an error or unexpected results., (*18)

namespace Foo {
    class Bar
    {

    }
}

namespace Baz {
    use Foo\Bar;

    class FooBar extends Bar
    {

    }
}

Di::map(array(
    'Foo\Bar' => 'Baz\FooBar'
));

$object = Di::get('Foo\Bar'); // will return an instance of `Baz\FooBar`

TODO

  • Setter Injection #1
  • Cache class instances #2
  • Define services #3
  • Map interface to concrete class #4

The Versions

08/01 2014

dev-master

9999999-dev

A dynamic dependency injection manager

  Sources   Download

MIT

by Pierre du Plessis

dependency injection dependency di injection

08/01 2014

v0.1.0

0.1.0.0

A dynamic dependency injection manager

  Sources   Download

MIT

by Pierre du Plessis

dependency injection dependency di injection