Code Monkey home page Code Monkey logo

di's Introduction

DI

C/C++ CI

DI is a native dependency injection framework for PHP written as a C extension.

⚠️ IMPORTANT: This is an EXPERIMENTAL extension, do not use in production! ⚠️

Install Guide

Make sure to have the PHP development package installed.

On Ubuntu:

sudo apt install php-dev

Or on Fedora:

sudo dnf install php-devel

Please note this extension was tested for PHP 7.3 and above.

git clone https://github.com/jayay/di
cd di
phpize
./configure
make
sudo make install

You may also want to edit your php.ini and add the following line at the end of the file:

extension=di.so

Usage

In order to build a class and its dependencies, use the get method:

<?php
$di = new DIContainerNative;
$app = $di->get(App::class);

To create a new dependency injection container and tell it to use the DatabasePG implementation for the Database interface, write

<?php
$di = (new DIContainerNative)
  ->withClassMap([
    Database::class => DatabasePG::class,
  ]);

As soon as a new DIContainerNative gets instantiated, one default entry in the class mapping gets created: Interface DIContainer points to the implementation DIContainerNative. The interface is defined as follows:

<?php
interface DIContainer {
    public function get(string $className);
    public function withInstances(array $instances) : DIContainer;
    public function withClassMap(array $mapping) : DIContainer;
}

If a DIContainer is requested, $this will be returned by the container by default. The implementation of DIContainerNative itself can be overwritten as well by pointing the interface to a new class.

What can be injected?

Only dependencies that resolve to classes will be injected. The dependencies, as well, can only have dependencies that are resolvable classes as dependencies.

Only the constructor gets taken into account for analysis. Scalar types cannot be resolved and would lead to a DIException.

License

DI was published under The PHP License, version 3.01. See LICENSE document.

di's People

Contributors

jayay avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

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.