syberisle/zit
Composer 安装命令:
composer require syberisle/zit
包简介
Zit is a simple dependency injector based heavily on Pimple. It aims to provide the same simplicity as Pimple while offering a slightly more robust object interface.
README 文档
README
Zit is a simple dependency injector based heavily on Pimple. It aims to provide the same simplicity as Pimple while offering a slightly more robust object interface.
Installation
Zit is available via Composer:
composer require syberisle/zit
Usage
Zit is simple to use. Like Pimple, objects are defined through anonymous functions that return an instance of the object:
$container = new \Zit\Container(); $container->set('auth', function() { return new Auth(); });
All instantiation functions are passed the container as the first argument, making dependency injection possible:
$container->set('auth', function($container) { return new Auth($container->get('db')); });
Zit also provides convenient magic methods for setting instantiation functions:
$container->setAuth(function() { /* ... */ }); // Or: $container->set_auth(function() { /* ... */ });
Getting Objects
Getting objects are as simple as:
$container->get('auth');
Or, if you prefer the shorthand:
$container->getAuth(); // Or: $container->get_auth();
Getting Fresh Objects
By default, all objects are shared in Zit. That is, once an object is created, that same exact object is
returned for each additional get(). If you need a fresh object, you can do so with:
$container->fresh('auth'); // Or: $container->freshAuth(); // Or: $container->fresh_auth(); // Or: $container->newAuth(); // Or: $container->new_auth();
Note that because the 'new' keyword is reserved, you can only use it if you're using the magic methods.
Constructor Parameters
Sometimes you need to pass parameters to the constructor of an object, while still also injecting dependencies. Zit automatically passes all parameters on to your instantiation function:
$container->setUser(function($c, $id)) { $user = new User($id); $user->setDb($c->getDb()); return $user; }); $user = $container->newUser(1); // Parameters are taken into account when caching results: $user2 = $container->getUser(1); // $user2 === $user;
Storing Static Content
You can also use Zit to store strings/objects/etc. Just pass it instead of a generator:
$container->set('api_key', 'abcd1234567890'); $key = $container->get('api_key');
Please note: You must wrap callables with an instantiation function if you want the callable returned rather than the return value of the callable.
Custom Container
Most projects will benefit from a custom container that sets up its own injection rules. This is as simple as extending Zit:
namespace MyApp\Di; class Container extends \Zit\Container { public function __construct() { $this->setAuth(function() { /* ... */ }); $this->setUser(function() { /* ... */ }); } }
Change Log
Version 3.0.0
- Implemented Container Interoperability. Zit was already
container-interopcompatible, but it now implements the interface and throws an exception that implementsInterop\Container\Exception\NotFoundExceptionwhen a item is not found. This exception extends\InvalidArgumentException, so 3.0.0 should be nearly 100% backwards-compatible, but I'm bumping the major version just in case. - Dropped support for PHP 5.3
- Removed deprecated function
setParam setFactory()now accepts anycallableinstead of specifically aClosure- Fixed a typo in the exception message thrown from
__callif a method does not exist set()is now fluent (returns the container for chaining)- Switched to md4 hashing for speed improvements (we don't need the security of md5)
- Added DocBlocks throughout the code
Version 2.0
- Removed the
setParammethod in favor of checking whether the parameter passed tosetis callable. - Added the "Factory" variant. This could cause backwards compatibility issues if you have set up objects that end with the word "factory".
- Updated the
deletemethod so that it clears out objects, callbacks, and factories, which could have some abnormal BC issues as well.
syberisle/zit 适用场景与选型建议
syberisle/zit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 06 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dependency injection」 「container」 「dependency」 「di」 「pimple」 「zit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 syberisle/zit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 syberisle/zit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 syberisle/zit 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A fast and intuitive dependency injection container.
Dependency injection container for the Monolith framework.
The PSR-11 container bridges
Http Microframework for Hack
PHP Dependency Injection Container
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-06-21