定制 celestriode/dynamic-registry 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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;
    }
}

统计信息

  • 总下载量: 31
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-10-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固