A most simplistic Dependency Injection Container

v1.0.2 2018-01-23 02:26 UTC

This package is auto-updated.

Last update: 2024-10-29 04:53:19 UTC


README

Build Status Coverage Status Scrutinizer Code Quality

A minimalistic Dependency Injection Container

Services are lazy loaded through the use of anonymous functions.

Installation

Install using composer:

composer require stratadox/di

Basic usage

// Create container
$container = new DependencyContainer();

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

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

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

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

Alternatively, you can use the array syntax:

// Create container
$container = new ArrayAdapter(new DependencyContainer());

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

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

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

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

By decorating the container with an AutoWiring object, a large portion of the configuration effort can be automated:

// Create container
$container = AutoWiring::the(new DependencyContainer);

$foo = $container->get(Foo::class);

Dependent services

You can construct services that use other services by passing the DI container in your anonymous function.

$container = new DependencyContainer();

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

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

$service = $container->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.

Parameters

To pass other parameters to your services, pass them to your anonymous function as well.

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

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

Cache

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

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