mmdm/sim-container

简单而优雅的依赖注入容器

v1.2.3 2020-10-17 18:49 UTC

This package is auto-updated.

Last update: 2024-09-18 04:15:10 UTC


README

一个用于依赖注入管理的库。

功能

  • 自动绑定
  • 单例构造
  • 工厂构造
  • 方法注入
  • 方法和数组访问

安装

composer

composer require mmdm/sim-container

或者您可以直接从GitHub下载zip文件,然后解压到您的项目库中,就像使用其他库一样。

只需添加以下行来自动加载文件

require_once 'path_to_library/autoloader.php';

然后您就可以开始了。

如何使用

// to instance a container object
$container = new Container();
// here you can define your object and use them

可用函数

容器

set($abstract, $concrete = null, ?string $method_name = null, array $method_parameters = []): Container

此方法将一个实体 $concrete 以别名 $abstract 存储起来,例如类名或其他名称。

版本 >= 1.2.0:您还可以传递要注入的方法 $method_name 以及方法的参数 $method_parameters。参数可以是实际参数或通过容器获取的抽象。

注意:您可以将 $concrete 作为 $abstract 传递

注意:参数是一个键值对,如下所示

parameter_name => your assignment,
index_of_parameter like 0|1|2|... => your assignment,
// example
'class_type' => CustomClass::class,
1 => 'A parameter',
// store a class
$container->set('log_cls', Logger::class);
// or simply store with Logger::class string
// alias will be the string from Logger::class
$container->set(Logger::class);
// or if you have some works to do and return a 
// new instance then use this
$container->set('alias of class', function (Container $c) {
    // do anything you need
    // ...
    
    // return a new instance of a class
    return new SomeClass();
});

// method injection
$container->set('log_cls', Logger::class, 'info');

// or with parameters
$container->set('log_cls', Logger::class, 'info', [
    'handler' => Handler::class,
    1 => '{level} - {other_parameter} - {message}',
]);

get($abstract, ?string $method_name = null, array $method_parameters = [])

此方法返回存储在别名 $abstract 下的实体 $concrete。如果它不存在,它将首先存储它,然后解析。

版本 >= 1.2.0:您还可以传递要注入的方法 $method_name 以及方法的参数 $method_parameters。参数可以是实际参数或通过容器获取的抽象。

注意:如果存储的 $concrete 已被解析,它将不再解析,而是返回之前解析的 $concrete。

注意:参数是一个键值对,如下所示

parameter_name => your assignment,
index_of_parameter like 0|1|2|... => your assignment,
// example
'class_type' => CustomClass::class,
1 => 'A parameter',
// retrieve a class
$container->get('log_cls');
// or
$container->get(NotStoredClass::class);

make($abstract, ?string $method_name = null, array $method_parameters = [])

此方法返回存储在别名 $abstract 下的实体 $concrete。如果它不存在,它将首先存储它,然后解析。

参数与 get 方法相同

// retrieve new instance of a class
$container->make('log_cls');
// or
$container->make(NotStoredClass::class);

has($abstract, string $method_name = null): bool

此方法检查特定的别名 $abstract 是否已存储。您还可以传递 $method_name 来检查特定的 $abstract 的方法是否已注册。

// check existence of a $abstract
$container->has('log_cls');
// or
$container->has(NotStoredClass::class);

unset($abstract, string $method_name = null): Container

此方法移除存储的 $abstract。您还可以传递 $method_name 来移除特定的 $abstract 的方法。

// remove an $abstract
$container->unset('log_cls');
// or
$container->unset(NotStoredClass::class);

单例访问

如果您需要以单例方式访问容器,请使用 getInstance 静态方法。

$container = Container::getInstance();

// then use all methods
$container->get($abstract);

//...

容器继承

如果您想继承 Container(可能进行自定义),您可以通过正常方式继承,但如果您有多个类要继承,则请在您的类中使用 ContainerTrait 并同时继承其他类。

注意:在 traits 使用后,您应在闭包中将 Container 类型提示为 YourOtherClass

class YourOtherClass extends AnotherClass {
    use ContainerTrait;
    
    // other codes
}

$instance = new YourOtherClass();

// the difference is in [YourOtherClass] instead of [Container]
$instance->set($abstract, function (YourOtherClass $c) {
    // some code
});

数组访问

您可以使用数组访问而不是方法访问

  • $container[$abstract] = $concrete 而不是 $container->set($abstract, $concrete, ?string $method_name = null, array $method_parameters = [])

接受以下偏移量

  [
    'concrete' => $concrete,
    'method' => [
      'name' => method's name,
      'parameters' => [
        method_parameter1 => CustomClass::class  or  Specified name in container,
        method_parameter2 => AnotherCustomClass::class  or  Specified name in container
      ].
    ],
  ]

OR

  object(stdClass) {
    'concrete' => $concrete,
    'method' => [
      'name' => method's name,
      'parameters' => [
        method_parameter1 => CustomClass::class  or  Specified name in container,
        method_parameter2 => AnotherCustomClass::class  or  Specified name in container
      ].
    ],
  }

OR

  An encoded json that has above structure

OR

  An optional $abstract variable and $concrete/$abstract value
  • $concrete = $container[$abstract] 而不是 $concrete = $container->get($abstract, ?string $method_name = null, array $method_parameters = [])

接受以下偏移量

  [
    'abstract' => $abstract,
    'method' => [
      'name' => method's name,
      'parameters' => [
        method_parameter1 => CustomClass::class  or  Specified name in container,
        method_parameter2 => AnotherCustomClass::class  or  Specified name in container
      ].
    ],
  ]

OR

  object(stdClass) {
    'abstract' => $abstract,
    'method' => [
      'name' => method's name,
      'parameters' => [
        method_parameter1 => CustomClass::class  or  Specified name in container,
        method_parameter2 => AnotherCustomClass::class  or  Specified name in container
      ].
    ],
  }

OR

  An encoded json that has above structure

OR

  An $abstract variable
  • isset($container[$abstract]) 而不是 $container->has($abstract, string $method_name = null)

接受以下偏移量

  [
    'abstract' => $abstract,
    'method' => [
      'name' => method's name,
    ],
  ]

OR

  object(stdClass) {
    'abstract' => $abstract,
    'method' => [
      'name' => method's name,
    ],
  }

OR

  An encoded json that has above structure

OR

  An $abstract variable
  • unset($container[$abstract]) 而不是 $container->unset($abstract, string $method_name = null)

接受以下偏移量

  [
    'abstract' => $abstract,
    'method' => [
      'name' => method's name,
    ],
  ]

OR

  object(stdClass) {
    'abstract' => $abstract,
    'method' => [
      'name' => method's name,
    ],
  }

OR

  An encoded json that has above structure

OR

  An $abstract variable

许可证

在MIT许可证下。