tsufeki/hmcontainer
Composer 安装命令:
composer require tsufeki/hmcontainer
包简介
dependency injection container
README 文档
README
HMContainer is a dependency injection container for PHP.
Installation
With Composer:
$ composer require tsufeki/hmcontainer
Usage
Create a container:
use Tsufeki\HmContainer\Container; $c = new Container();
Add a value (constant parameter):
$c->setValue("key", 42);
Retrieve it (HMContainer implements PSR-11):
$c->has("key"); // true $c->get("key"); // 42 $c->get("non-existent-key"); // throws NotFoundException $c->getOrDefault("non-existent-key", 5); // 5
Container is frozen during first get() or freeze() call and no new items can
be added afterwards.
Definition objects
There are to ways to add items to the container: convenience methods Container::setValue(),
::setClass() etc. or through creating Definition objects yourself and calling
Container::set(). I.e. those two lines are equivalent:
use Tsufeki\HmContainer\Definition\Value; $c->setValue("key", 42); $c->set("key", new Value(42));
Multi-valued keys
Add multiple items to be retrieved as an array:
$c->setValue("primes", 2, true); $c->setValue("primes", 3, true); $c->setValue("primes", 5, true); $c->isMulti("primes"); // true $c->get("primes"); // [2, 3, 5]
Class instantiation and autowiring
Add a class which will be instantiated once, during first get():
$c->setClass("aobject", AClass::class, false, ["dep1", "dep2"]); $c->get("aobject"); // returns new AClass($c->get("dep1"), $c->get("dep2")) $c->get("aobject"); // returns the same instance as above
Dependencies can be automatically deduced (autowired) if you use class names as DI keys:
class BClass { } class CClass { public function __construct(BClass $b) { } } $c->setClass(BClass::class); $c->setClass(CClass::class); $c->get(CClass::class); // correctly contructed CClass object
Autowiring key is guessed from parameter type hint, @param tag type or special @Inject tag:
class DClass { /** * @param CClass $c * @param $d @Inject("dkey") */ public function __construct(BClass $b, $c, $d) { } }
Multi items are supported as well:
class Aggregator { /** * @param SomeInterface[] $impls */ public function __construct(array $impls) { } } $c->setClass(SomeInterface::class, ConcreteImplementation1::class, true); $c->setClass(SomeInterface::class, ConcreteImplementation2::class, true); $c->setClass(Aggregator::class); $c->get(Aggregator::class);
Mark parameter with @Optional to have null injected when dependency can't
be found:
class Maybe { /** * @param $dep @Optional */ public function __construct(Dep $dep = null) { } }
Mix manual dependencies and autowiring by putting some nulls in dependency
array:
$c->setClass(DClass::class, null, false, [null, "dep2"]);
Using Definitions as dependencies is also supported:
use Tsufeki\HmContainer\Definition\Reference; $c->setClass(DClass::class, null, false, [null, new Reference("dep2")]);
Aliases
Add an alias to other key:
$c->setAlias("alias", "target"); $c->get("alias"); // same as $c->get("target")
Lazy items
Add a lazy item, it will return parameterless callable:
$c->setLazy('lazy', new Value(42)); $c->get('lazy'); // a callable $f such that $f() === 42
Custom definitions
You can add your custom instantiators by implementing
Definition interface and using
set() method:
$myFactory = new MyFactory(); $c->set("mykey", $myFactory);
Serialization
Container can be serialized and unserialized for caching with standard PHP
serialize() and unserialize(), but only if you use serializable factories.
License
MIT - see LICENCE.
tsufeki/hmcontainer 适用场景与选型建议
tsufeki/hmcontainer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.16k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tsufeki/hmcontainer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tsufeki/hmcontainer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.16k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-04-21