Giter Club home page Giter Club logo

di's Introduction

Di

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);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.