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

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

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

azexsoft/di

最新稳定版本:v1.1.2

Composer 安装命令:

composer require azexsoft/di

包简介

Azexsoft Dependency Injection

README 文档

README

License Latest Stable Version Total Downloads

Simple PSR-11 compatible dependency injection container and injector based on autowiring that is able to instantiate and configure classes resolving dependencies. Works only with classes and interfaces. Works with PHP 7.1+ and 8.0+.

Features

  • PSR-11 compatible.
  • Detects circular references.
  • Supports classname, object and Closure bindings.
  • Supports service providers and deferred service providers.
  • Injector resolves dependencies for definitions when building class and invoke dependencies.
  • Injector supports array definitions with method arguments and properties resolving dependencies.

Configure container

Just create container instance with an array of bindings and array of service providers.

// Bindings
$bindings = [
    FooInterface::class => Foo::class, // Classname binding
    Baz::class => new Baz(), // Object binding
    
    // Closure binding with DI resolving in Closure params
    Bar::class => fn(\Azexsoft\Di\Injector $injector) => $injector->build( 
        Bar::class,
        [
            'config' => $params['barConfig'],
            'someParameter' => 123
        ]
    ),
];

// Service providers
$providers = [
    ApplicationServiceProvider::class,
    new FooServiceProvider(),
];

$container = new \Azexsoft\Di\Container($bindings, $providers);

Also, you can add bindings and providers after container be configurated. After rebinding old instance will be removed from container.

$foo = $container->get(FooInterface::class); // will be returned Foo
$container->bind(FooInterface::class, OtherFoo::class);
$otherFoo = $container->get(FooInterface::class); // will be returned OtherFoo

Array definitions for injector builder. Works for bind() and provide() methods.

$container->bind(Abstract::class, [
    '__class' => Concrete::class, // Concrete classname
    '__construct()' => [
        'simpleParam' => 123,
        'otherSimpleParam' => $params['concreteSomeParam'],
        'someObject' => new Definition(fn(Dependency $d) => $d->getSomeObject()),
    ],
    'someMethod()' => [
        'methodParam' => $params['someMethodParam'],
    ],
    'someProperty' => 321
]);

Instead of:

$container->bind(Abstract::class, function(Injector $injector, Dependency $d) use ($params) {
    $concrete = $injector->build(Concrete::class,  [
        'simpleParam' => 123,
        'otherSimpleParam' => $params['concreteSomeParam'],
        'someObject' => $d->getSomeObject(),
    ]);
    $concrete->someMethod($params['someMethodParam']);
    $concrete->someProperty = 321;
    return $concrete;
});

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固