2017 © Pedro Peláez
 

library phpdicontainer

Simple DI Container

image

minusfour/phpdicontainer

Simple DI Container

  • Thursday, May 21, 2015
  • by MinusFour
  • Repository
  • 1 Watchers
  • 0 Stars
  • 7 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

phpDIContainer

Very simple Dependency Injector Container., (*1)

Installing

You can install it through composer. Through the CLI:, (*2)

composer require minusfour/phpdicontainer

Don't forget to add the autoloader to your bootstrap file:, (*3)

require 'vendor/autoload.php';

How to use

Start by specifying the class namespace:, (*4)

use MinusFour\DIContainer\Container;
use MinusFour\DIContainer\Injector;

Instantiate the container., (*5)

$container = new Container();

Register an injector. Assuming class A depends on class B., (*6)

$container->registerInjector(new Injector('A', function($deps){
    return new A($deps['B']);
}, ['B']));

And register the injector dependencies. In this case B., (*7)

$container->registerInjector(new  Injector('B', function($deps){
    return new A($deps['B']);
}));

To resolve a class, all you need to do is:, (*8)

$A = $container->resolve('A');

It will instantiate A and inject its dependencies through their injectors., (*9)

Note

The injector constructor takes three parameters. The first parameter must be the name of the class (or dependency), the third parameter is an array containing a list of dependencies (names must match first parameter). The second parameter is an anonymous function that takes one parameter, the dependencies as an array of objects and is responsible for both instantiation and dependency injection., (*10)

The injector implementation borrows from pimple examples, mostly the anonymous function idea., (*11)

Exceptions note

$container->resolve() is responsible for throwing 2 kind of exceptions: MinusFour\DIContainer\InjectorNotFoundException and MinusFour\DIContainer\DependencyUnmetException. First only occurs if there's no injector for said class, second one is thrown when the container fails to find the injector of the dependency., (*12)

The Versions

21/05 2015

dev-develop

dev-develop

Simple DI Container

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3

 

by Alejandro Quiroga

18/05 2015

dev-master

9999999-dev

Simple DI Container

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3

 

by Alejandro Quiroga

17/05 2015

v1.0.0

1.0.0.0

Simple DI Container

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3

 

by Alejandro Quiroga