celestriode/dynamic-registry
Composer 安装命令:
composer require celestriode/dynamic-registry
包简介
Simple, individual registries that are populated on-demand to save on memory.
README 文档
README
Create registries that are filled on-demand instead of wasting memory when they are never accessed.
Example
Extending
Extend AbstractRegistry for each type of registry you want to keep a set of values for.
class ItemsRegistry extends AbstractRegistry { public function getName() : string { return 'items'; } } class EntitiesRegistry extends AbstractRegistry { protected $defaultValues = [1, 2, 3]; // Optional. public function getName() : string { return 'entities'; } }
Or if you only want strings in the registry, you can extend AbstractStringRegistry. When a non-string value is added to such a registry, InvalidValue is thrown.
class ItemsRegistry extends AbstractStringRegistry { public function getName() : string { return 'items'; } }
Or if you want a simple container and have no use for multiple unique registries, create a new SimpleRegistry object.
$registry = new SimpleRegistry(-1, -2, -3); // Optionally add values upon instantiation.
Usage
You can either use new to create a new object for your registry or obtain a singleton via get().
$itemsRegistry = ItemsRegistry::get(); $entitiesRegistry = EntitiesRegistry::get(4, 5, 6); // Optionally add values upon FIRST get() call.
Apart from those initially-present values, you can create and register a dynamic populator. A dynamic populator adds values to the registry the first time that the registry has been queried via has() or if the registry has been forcibly populated with populate().
class ItemsPopulator implements DynamicPopulatorInterface { public function populate(AbstractRegistry $registry) : void { $registry->addValues('a', 'b', 'c'); } }
Of course, rather than dynamically populating with hard-coded values, you would instead populate the registry through some other means, such as from a database or via an external API. After registering the populator, once has() is called, the registry will be automatically populated and will return true if the value exists.
$itemsRegistry->has('a'); // false $itemsRegistry->register(new ItemsPopulator()); $itemsRegistry->has('a'); // true
Be warned that attempting to add multiple of the same value will result in InvalidValue being thrown. To avoid this consequence and instead cause it to fail silently, override the failSilently method.
class ItemsRegistry extends AbstractStringRegistry { public function getName() : string { return 'items'; } public function failSilently(): bool { return true; } }
celestriode/dynamic-registry 适用场景与选型建议
celestriode/dynamic-registry 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 10 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 celestriode/dynamic-registry 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 celestriode/dynamic-registry 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 31
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-10-16