jbuncle/simple-dic
Composer 安装命令:
composer require jbuncle/simple-dic
包简介
Simple Dependency Injection container focussing on auto injection.
README 文档
README
A simple dependency injection container with focus on automatic wiring (auto-wiring) dependencies.
Introduction
Simple DIC is implemented with auto-wiring at the forefront.
This basically means that if a class can be created by automatically by recursively resolving dependencies then you will get a class back, even if you haven't manually put anything into the container!
For this to happen fully 100% automatically, all class constructor argumentss will need to have explicit type hints for instantiable types (i.e. they need to be non-abstract, non-interface, non-native).
If the types required are using interfaces and/or abstract classes then you can define a type mapping with the
addTypeMapping method, to tell the container what concrete classes to resolve the types to.
If the types required have dependencies on native types (e.g. the constructor requires a string), then you can define factory methods to create the instance manually. You're factory method can define arguments which will also be autowired.
I have tried to optimise this implementation as much as possible.
Example
In the example below we are able to get an instance of the Foo class, simply be asking the container for one,
no setup of the container is required other than instantiating it.
class Baz { public __construct(); } class Bar { public __construct(Baz baz); } class Foo { public __construct(Bar bar); } $container = new \JBuncle\SimpleDic\Container(); $fooInstance = $container->getInstance(Foo::class);
This will automatically instantiate instances of Foo, Bar and Baz, and store those instances in the container
automatically. Therefore subsequently calling $container->getInstance(Bar::class) will give you the same class
instance used to create Foo.
Adding Factory Functions
Factory functions allow you define a callback to use to manually create instances (lazily) when needed.
Useful for instances that cannot be created using full automatic autowiring.
Type Mapping
Type mapping allows you to tell the container what type to use when a specific type is requested.
This is useful for when you want to define what concrete or extending class to use when a supertype or interface is required.
PSR Compatibility
A simple DIC can be easily converted into a PHP FIG PSR-11 (psr/container v2) compatible implementation using the
PsrAdapter wrapper class:
$containerFactory = new \JBuncle\SimpleDic\ContainerFactory(); $container = $containerFactory->create(); /* @var $psrContainer \Psr\Container\ContainerInterface */ $psrContainer = new \JBuncle\SimpleDic\PsrAdapter($container);
jbuncle/simple-dic 适用场景与选型建议
jbuncle/simple-dic 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 51 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 07 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jbuncle/simple-dic 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jbuncle/simple-dic 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 51
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2021-07-28