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

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

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

mdarc/di

Composer 安装命令:

composer require mdarc/di

包简介

A simple yet powerful PSR-11 autowiring dependency injection container

README 文档

README

A simple yet powerful PSR-11 autowiring dependency injection container.

mdarc/DI was conceived to be simple to configure and use. It was built for performance. (check benchmarks)

mdarc/DI has an very small but robust code base. It is production ready and can be used for small micro-services or large monolithic projects.

Features:
  • Autowiring: Automatically instantiate and inject dependencies
  • Manual configuration: When classes cannot be autowired, you can create them by yourself
  • Circular reference detection: It throws a CircularReferenceException with enough details to fix the problem
What mdarc/DI is not good for:
  • Autowiring via setter methods is not supported (and it will never be)
  • Autowiring using phpDoc annotations is not supported (and it will never be)
  • Automatically injecting dependencies on constructor parameters without type hints is not supported. You must manually configure those cases

Installation

Composer

Before anything else, use this to add it to your composer.json

$ composer require mdarc/di "^1.0.0"

Usage

Creating a container ready to use with autowiring enabled is a matter of creating a Container instance:

use Mdarc\DI\Container;

$container = new Container();

If your classes contain constructor parameters that are other objects, then simply:

$myClass = $container->get(\Path\To\MyClass::class);

As any other DI container, $myClass will always get the same instance on the requested class.

If you want to create a new object every time (instead of getting the same object instance) then use the factory helper:

use Mdarc\DI\Container;
use Mdarc\DI\DI;

$container = new Container([
    \Path\To\MyClass::class => DI::factory(function () {
        return new \Path\To\MyClass(); 
    }),
]);

$myClass = $container->get(\Path\To\MyClass::class);

For those classes that cannot be created using autowiring, then you can add their definitions:

use Mdarc\DI\Container;

$container = new Container([
    \Monolog\Logger::class => function (Container $c) {
        $config = $c->get(\Path\To\Config::class);
        $logger = new \Monolog\Logger($config->get('name'));
        $logger->pushHandler(new \Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::DEBUG));

        return $logger; 
    }),
]);

$logger = $container->get(\Monolog\Logger::class);

Defining aliases for binding interfaces to implementations is simple:

use Mdarc\DI\Container;

$container = new Container([
    // Binding Interface to implementation
    \Psr\Log\LoggerInterface::class => \Monolog\Logger::class,
    // Concrete implementation
    \Monolog\Logger::class => function (Container $c) {
        // build Monolog here
    }),
]);

$logger = $container->get(\Psr\Log\LoggerInterface::class);

Specifying arguments for classes with constructor parameters that are scalar, array or undefined type:

use Mdarc\DI\Container;

class MyClass {
    public function __construct(array $config, \Psr\LoggerInterface $logger) { /*...*/ }
}

$definitions = [
    // Binding Interface to implementation
    \Psr\Log\LoggerInterface::class => \Monolog\Logger::class,
    // Concrete implementation
    \Monolog\Logger::class => function (Container $c) {
        // build Monolog here
    },
];

$constructorParameters = [
    MyClass::class => [
        'config' => ['an array', 'of relevant', 'things']
    ],
];
$container = new Container($definitions, $constructorParameters);

$myClass = $container->get(MyClass::class);

License

mdarc/DI is licensed under the MIT License.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-09-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固