vpa/di

The simple DI implementation for PHP 8.x with using Attributes #[Injectable]

v0.4 2023-01-11 23:11 UTC

This package is auto-updated.

Last update: 2024-11-12 03:23:47 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require Scrutinizer Code Quality Code Coverage Build Status Code Intelligence Status

Dependency Injection pattern implementation PSR-11 (Psr\Container\ContainerInterface) for PHP 8.x

To specify the classes for which this pattern can be applied, attributes are used, support for which was added to PHP 8. This implementation allows you to fully control for which classes you can apply DI, just like Angular or NestJS does.

Install

composer require vpa/di

Example:

require_once(__DIR__ . '/../vendor/autoload.php');

use VPA\DI\Container;
use VPA\DI\Injectable;

#[Injectable]
class A {

    function __construct() {}
    function echo () {
        print("\nThis is Sparta!\n");
    }
}

#[Injectable]
class B {

    function __construct(protected A $a) {}
    function echo () {
        $this->a->echo();
    }
}

class C {

    function __construct(protected A $a) {}
    function echo () {
        $this->a->echo();
    }
}

$di = new Container();
$di->registerContainers();
$b = $di->get(B::class); // returns instance of class B
$b->echo();
$c = $di->get(C::class); // returns exception (class C not tagged as Injectable)
$c->echo();

You can add aliased classes manually, but the declaration of these classes must still have the #[Injecatble] tag.

$di = new Container();
$di->registerContainers(['E'=>A::class]);
$e = $di->get('E');
echo $e instanceof A; // returns true

If your class has a constructor with parameters (and the types of those parameters are not an object) you can pass those parameters as the second parameter of the get method as an array:

#[Injectable]
class A {
    function __construct() {}
}
#[Injectable]
class B {

    function __construct(protected A $a, private int $x, private int $y) {}
}

$di = new Container();
$di->registerContainers();
$b = $di->get(B::class,['x'=>10,'y'=>20]);

Bubble Propagation for attribute Injectable

In version 0.2.0 added method setBubblePropagation(bool $bubblePropogation) (default is true) which specifies whether parent classes should be checked for the presence of an attribute Injectable. This allows you to not set the attribute Injectable to all children that should be DI.

Example:

// In versions less 0.2.0 code below returns Exception, in version 0.2.0 and great - will return the desired class 
#[Injectable]
class A {}
class B extends A {} // this class is not marked with the attribute Injectable
$di = new Container();
$di->registerContainers();
$b = $di->get(B::class);

Similar to the previous point, DI supports bubble propagation for interfaces as well:

// In versions less 0.2.0 code below returns Exception, in version 0.2.0 and great - will return the desired class 
#[Injectable]
interface A {}
class B implements A {} // this class is not marked with the attribute Injectable
$di = new Container();
$di->registerContainers();
$b = $di->get(B::class);