nerd/glue
Composer 安装命令:
composer require nerd/glue
包简介
Glue is a simple library that helps various libraries work together, simplifying things like relying on various implementations of things like Caching, by creating a centralized factory for creating such objects. It's a form of Dependency Injection.
README 文档
README
Glue is a simple PHP library that helps various libraries work together with less effort. It primarily provides a centralized factory method for fetching functionality based on an interface definition. For example; if your library can utilize caching, you would could easily obtain the cache-interface by simply calling:
$cache = \Nerd\Glue::get(Psr\Cache\CachePoolInterface::class);
You are now guaranteed that $cache is a class that implements the Psr\Cache\CachePoolInterface and that you can use it as you wish.
Example 1: Database access
If your library needs access to the database, you could very conveniently do the following:
$db = \Nerd\Glue::get(\PDO::class);
This would provide you with a PDO object, as per the applications configuration. The framework would previosuly have to ave done the following:
\Nerd\Glue::singletonFactory(\PDO::class, function() {
return new PDO("your-connection-string", "user", "pass");
});
Example 2: Caching
$cache = \Nerd\Glue::get(\Psr\Cache\CachePoolInterface);
$item = $cache->getItem("some-cache-key");
if(!$item->exists()) {
// Let's generate the item
$value = "Some string".time();
$item->set($value);
$item->setExpiration(30);
$cache->save($item);
} else {
$value = $item->get();
}
Standardized interfaces
It's recommended that you request interfaces that have been standardized through the process at www.php-fig.org, or standard PHP interfaces. This means that the following is good candidates:
- Psr\Log\LoggerInterface
- Psr\Cache\CachePoolInterface
- Psr\Http\Message\ResponseInterface
- PDO
Dependency Injection Container
There is a very interesting proposal draft related to dependency injection at https://github.com/container-interop/fig-standards/blob/master/proposed/container.md. Dependency injection basically does what Glue does, in a more elaborate way. Interestingly, Glue still has a purpose in this case, as you need a way to retrieve the Dependency Injection container:
$container = \Nerd\Glue::get(\Psr\Container\ContainerInterface::class);
Also, if this proposal is approved - Glue will naturally integrate with the configured Dependency Injection Container.
nerd/glue 适用场景与选型建议
nerd/glue 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 126 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 11 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 nerd/glue 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nerd/glue 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 126
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 29
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-08