dmt-software/di-plug 问题修复 & 功能扩展

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

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

dmt-software/di-plug

最新稳定版本:0.5.2

Composer 安装命令:

composer require dmt-software/di-plug

包简介

An PHP dependency container abstraction

README 文档

README

Introduction

psr-11 introduced a standardized way for packages, libraries and frameworks to retrieve objects from containers. This package also allows adding dependencies to a container by a uniform interface without the use of a specific container implementation. This will help developers to access the dependency injection container to make installation of their packages easier.

Installation

composer require dmt-software/di-plug

Usage

Manual create the container

use DMT\DependencyInjection\Adapters\PimpleAdapter;
use DMT\DependencyInjection\Container;
use Pimple\Container as PimpleContainer;
 
$container = new Container(new PimpleAdapter(new PimpleContainer()));
$container->set(SomeClass::class, fn() => new SomeClass($container->get(SomeDependency::class)));

Autodetect

Use the container builder to wrap your container instance within the DMT\DependencyInjection\Container.

use DMT\DependencyInjection\ContainerFactory;
 
/** @var object $supportedContainerInstance */
$factory = new ContainerFactory();
$container = $factory->createContainer($supportedContainerInstance);
$container->set(SomeClass::class, fn() => new SomeClass($container->get(SomeDependency::class)));

Autodiscovery

NOTE: When another dependency uses a supported container, autodiscovery might end up using a different container than expected.

The code can discover the dependency container installed.

use DMT\DependencyInjection\ContainerFactory;
 
$factory = new ContainerFactory();
$container = $factory->createContainer();
$container->set(SomeClass::class, fn() => new SomeClass($container->get(SomeDependency::class)));

Concepts

Instances without registration

If the Container::get() is called with a className that is not present in the container will create a new instance for that class. This also counts for calls with made with constructor arguments.

call set returns
Container::get(Some::class) false new instance
Container::get(Some::class) true from dependency
Container::get(Some::class, 12) true new instance

NOTE: when the container creates a new instance it will NOT be set in the container. Not even when it is called with the same constructor arguments.

Configuration Discovery

When ConfigurationInterface is implemented and available in the container, the constructor arguments can be injected from the configuration using the ConfigValue attribute.

use DMT\DependencyInjection\Attributes\ConfigValue;

class Foo
{
    public function __construct(
        #[ConfigValue('configKeyForBar')]
        public string $bar,
    )
}

The value for the bar in the constructor is fetched from configuration when it is omitted from the container::get call.

Auto Inject Container

Instead of injection all the dependencies, one can choose to inject just the container and resolve the dependencies when needed. This can easily be achieved by using the HasContainer trait.

use DMT\DependencyInjection\Container;
use DMT\DependencyInjection\Traits\HasContainer;

class SomeClass
{
    use HasContainer;
    
    public function doSomething(): void
    {
        $dependency = $this->getContainer()->get(SomeDependency::class);
        $dependency->doSomething();
    }
}

This even works when the HasContainer is used within another trait, but just one level deep. Using the trait below f.i. enables getTwigEngine() call to retrieve the template engine in the parent class.

use DMT\DependencyInjection\Traits\HasContainer;
use Twig\Environment;

trait HasTwigEngine
{
    use HasContainer;
    
    public function getTwigEngine(): Environment
    {
        return $this->getContainer()->get(Environment::class);
    }
}

ServiceProvider

A service provider can be used to register or change dependencies in the container.

use DMT\DependencyInjection\Container;
use DMT\DependencyInjection\ServiceProviderInterface;
use Twig\Environment;
use Twig\Extension\StringLoaderExtension;

class MyServiceProvider implements ServiceProviderInterface
{
    /**
     * Register dependencies and ensure twig is enabled with string loader extension.   
     */
    public function register(Container $container): void
    {
        if (!$container->has(Environment::class)) {
            $container->set(Environment::class, fn() => new Environment());
        }
        
        $env = $container->get(Environment::class);
        if (!$env->hasExtention(StringLoaderExtension::class)) {
            $env->addExtension(new StringLoaderExtension());
        }

        $container->set(MyClassInterface::class, fn() => new MyClass($container->get(Environment::class)));
    }
}

Supports

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固