aatis/依赖注入

此包的最新版本(1.5.0)没有可用的许可信息。

Aatis的依赖注入

1.5.0 2024-02-14 12:32 UTC

This package is auto-updated.

Last update: 2024-09-14 13:40:33 UTC


README

安装

composer require aatis/dependency-injection

使用

需求

将环境变量 APP_ENV 设置为您要使用的环境名称。

使用您的应用程序上下文($_SERVER)创建容器构建器。

(new ContainerBuilder($ctx))->build();

排除文件

精确不作为服务的文件。

# In config/services.yaml file :

exclude_paths:
  - '/Folder'
  - '/OtherFolder/file.txt'
  - <...>

服务配置

您可以管理服务必须在哪个环境中加载以及传递给构造函数的参数。

当它是一个接口时,您还可以精确使用依赖的类。

最后,您可以为任何服务添加额外的标签。

# In config/services.yaml file :

services:
    Namespace\To\The\Service:
        environment:
            - 'env_name1'
            - 'env_name2'
            - <...>
        arguments:
            variable_name_into_the_constructor: 'it_value'
        tags:
            - 'tag_name1'
            - 'tag_name2'
            - <...>

环境和标签是可选的

参数键的名称必须与构造函数中的名称相同

接口到构造函数中

当请求服务构造函数中的接口时,DI将尝试在您的应用程序中查找实现此接口的服务。

如果有多个服务实现了接口,DI将选择找到的第一个或已实例化的实现接口的服务。

如果您想使用特定的服务,不要忘记在服务声明中声明它。

# In config/services.yaml file :

services:
    Namespace\To\The\Service:
        arguments:
            variable_name_into_the_constructor: 'service_implementing_the_interface'

如果想要使用供应商的特定服务,执行前面的步骤并在配置的includes_services部分中指定它。

# In config/services.yaml file :

include_services:
    - 'Namespace\To\The\Vendor\Service\Implementing\The\Interface'

构造函数中的环境变量

您可以直接在服务的构造函数中请求环境变量。

public function __construct(string $_my_env_var)
{
    // ...
}

变量的名称必须以 $_ 开头,后跟小写的环境变量名称

容器使用

获取和设置

使用容器,您可以通过容器的get()set()方法获取和设置您想要的任何服务/环境变量。

但是,要设置服务,您必须提供Service类的实例。您可以使用ServiceFactory服务创建它。

// Env Variable
$container->get('APP_ENV_VAR_NAME');
$container->set('APP_ENV_VAR_NAME', 'value');

// Service
$container->get('Namespace\To\The\Service');

$service = $container->get(ServiceFactory::class)->create('Namespace\To\The\Service');
$container->set('Namespace\To\The\Service', $service);

APP_ENV_VAR_NAME必须以 "APP_" 开头

按标签获取

您可以使用ContainergetByTag()getByTags方法通过标签获取服务。

$container->getByTag('tag_name');
$container->getByTags(['tag_name1', 'tag_name2']);

您也可以请求获取Service实例,而不是服务的实例,通过在第二个参数中精确指定true

$container->getByTag('tag_name', true);
$container->getByTags(['tag_name1', 'tag_name2'], true);

按接口获取

您可以使用ContainergetByInterface()getByInterfaces方法通过接口获取服务。

$container->getByInterface('Namespace\To\The\Interface');
$container->getByInterfaces(['Namespace\To\The\Interface1', 'Namespace\To\The\Interface2']);

像标签一样,您也可以请求获取Service实例,而不是服务的实例,通过在第二个参数中精确指定true

$container->getByInterface('Namespace\To\The\Interface', true);
$container->getByInterfaces(['Namespace\To\The\Interface1', 'Namespace\To\The\Interface2'], true);

ServiceInstanciator

您可以使用ServiceInstanciator服务和Service类的setInstance()方法将服务实例化到容器中。

您可以选择两种方法来实例化服务。对于第一种,您必须在配置中提供传递给构造函数的参数。对于第二种,您必须自己创建实例。

$service = $container->get(ServiceFactory::class)->create('Namespace\To\The\Service');

// Method 1
$instance = $container->get(ServiceInstanciator::class)->instanciate($service)

// Method 2
$instance = new Namespace\To\The\Service($arg1, $arg2, ...);

$service->setInstance($instance);
$container->set('Namespace\To\The\Service', $service);