seeren/container

自动装配和配置依赖项

3.0.1 2022-06-23 14:18 UTC

This package is auto-updated.

Last update: 2024-09-23 19:23:20 UTC


README

Build Require Coverage Download Codacy Badge Version

自动装配和配置依赖项

安装

Seeren\Container 是一个 PSR-11 容器接口 实现

composer require seeren/container

Seeren\Container\Container

容器创建、构建、存储和共享实例

use Seeren\Container\Container;

$container = new Container();

$foo = $container->get('Dummy\Foo');

自动装配

使用类型声明解决依赖项

namespace Dummy;

class Foo
{
    public function __construct(Bar $bar){}
}

class Bar 
{
    public function __construct(Baz $baz){}
}

class Baz {}

接口

namespace Dummy;

class Foo {
    public function __construct(BarInterface $bar){}
}

默认情况下,使用配置文件(/config/services.json)通过接口解决

{
  "parameters": {},
  "services": {
    "Dummy\\Foo": {
      "Dummy\\BarInterface": "Dummy\\Bar"
    }
  }
}

可以在构造时指定包含路径

project/
└─ config/
   └─ services.json

参数

参数和原始数据类型使用配置文件解决

namespace Dummy;

class Foo
{
    public function __construct(string $bar){}
}
{
  "parameters": {
    "message": "Hello"
  },
  "services": {
    "Dummy\\Foo": {
      "bar": ":message"
    }
  }
}

方法

方法可以使用自动装配

namespace Dummy;

class Foo
{

    public function __construct(BarInterface $bar){}

    public function action(int $id, Baz $baz)
    {
        return 'Hello';
    }

}
use Seeren\Container\Container;

$container = new Container();

$message = $container->call('Dummy\Foo', 'action', [7]);

echo $message; // Hello

许可协议

本项目采用 MIT 许可协议