vpa/di 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vpa/di

最新稳定版本:v0.4

Composer 安装命令:

composer require vpa/di

包简介

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

README 文档

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

统计信息

  • 总下载量: 205
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 2
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: AGPL-3.0-or-later
  • 更新时间: 2022-08-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固