2017 © Pedro Peláez
 

library di5

A most simplistic Dependency Injection Container

image

stratadox/di5

A most simplistic Dependency Injection Container

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Di

Build Status Coverage Status, (*1)

A minimalistic Dependency Injection Container, (*2)

Services are lazy loaded through the use of anonymous functions., (*3)

Php 5.x

This version is deprecated. If you're running Php 7 or higher, please consider using Stratadox/Di instead., (*4)

Installation

Install using composer:, (*5)

composer require stratadox/di5

Basic usage

// Create container
$di = new Container();

// Set service
$di->set('some_service', function () {
    return new SomeService();
});

// Get service
$service = $di->get('some_service');

// Check if service exists
$hasService = $di->has('some_service');

// Remove service
$di->forget('some_service');

Alternatively, you can use the array syntax:, (*6)

// Create container
$di = new ArrayAdapter(new Container());

// Set service
$di['some_service'] = function () {
    return new SomeService();
};

// Get service
$service = $di['some_service'];

// Check if service exists
$hasService = isset($di['some_service']);

// Remove service
unset($di['some_service']);

Dependent services

You can construct services that use other services by passing the DI container in your anonymous function., (*7)

$di = new Container();

$di->set('collaborator', function () {
    return new Collaborator();
});

$di->set('main_service', function () use ($di) {
    return new MainService($di->get('collaborator'));
});

$service = $di->get('main_service');

Because services are lazy it does not matter in which order you define them, as long they are all defined when you request one. So in the example above we could define main_service before collaborator, so long as we don't request main_service before collaborator is defined., (*8)

Parameters

To pass other parameters to your services, pass them to your anonymous function as well., (*9)

$dsn = 'mysql:host=localhost;dbname=testdb';
$username = 'admin';
$password = 'secret';

$di = new Container();
$di->set('database', function () use ($dsn, $username, $password) {
    return new DatabaseConnection($dsn, $username, $password);
});

Typehinting

You can assert the service to be of a certain class or implement an interface when requesting the service., (*10)

$foo = $di->get('foo', Foo::class);

If the type assertion fails, an InvalidServiceType exception is thrown., (*11)

Cache

By default, services are cached. You can trigger the factory on each request by setting cache to false., (*12)

// Set service
$di->set('some_service', function () {
    return new SomeService();
}, false);

The Versions

11/08 2017

dev-master

9999999-dev

A most simplistic Dependency Injection Container

  Sources   Download

MIT

The Requires

 

The Development Requires

container lightweight simple dic dependency di injection php5 inversion small stratadox

11/08 2017

v1.0.1

1.0.1.0

A most simplistic Dependency Injection Container

  Sources   Download

MIT

The Requires

 

The Development Requires

container lightweight simple dic dependency di injection php5 inversion small stratadox

11/08 2017

dev-php5

dev-php5

A most simplistic Dependency Injection Container

  Sources   Download

MIT

The Requires

 

The Development Requires

container lightweight simple dic dependency di injection inversion small stratadox

01/07 2017

v1.0

1.0.0.0

A most simplistic Dependency Injection Container

  Sources   Download

MIT

The Requires

 

The Development Requires

container lightweight simple dic dependency di injection inversion small stratadox