nerd-framework/nerd-container

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

轻量级IoC容器,支持依赖注入

v1.0.4 2016-11-25 14:00 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:09:27 UTC


README

Build Status Coverage Status StyleCI

Nerd Framework的依赖注入容器。

获取容器

$container = new \Nerd\Framework\Container\Container();

绑定类构造函数

$container->bind('foo', Foo::class);

绑定可调用工厂

$container->bind('factory', function () {
  return new Factory();
});

绑定单例

$container->singleton('single', SingletonService::class);

从容器中检索资源

$foo = $container->get('foo');

使用依赖注入调用函数、类方法或类构造函数

$result = $container->invoke(function (FooFactoryInterface $factory) {
  // $foo will be injected using parameter name
  // $other will be injected using Bar type hint
  return $factory->makeFoo();
});

将附加资源传递给invoke()方法

$result = $container->invoke(function ($foo, $a, $b) {
  //
}, ["a" => "Hello", "b" => "World"]);

资源解析器

尚未文档化。