zicht/key-value-bundle

Zicht KeyValue Bundle

5.1.0 2024-02-06 13:03 UTC

README

存储预定义键值对的组件。

安装

composer require zicht/key-value-bundle
{
    "scripts": {
        "post-install-cmd": [
            "Zicht\\Bundle\\KeyValueBundle\\Composer\\ScriptHandler::createKeyValueStorageDirectory"
        ],
        "post-update-cmd": [
            "Zicht\\Bundle\\KeyValueBundle\\Composer\\ScriptHandler::createKeyValueStorageDirectory"
        ]
    }
}

如何定义

在你的自定义包中定义一个 KeysDefinerInterface 或扩展 AbstractKeyDefiner 服务,并用 zicht_bundle_key_value.keys_definer 标签该服务。

class FooBundleKeyDefiner extends AbstractKeyDefiner
{
    /** @var SchemaService */
    private $schemaService;

    public function __construct(SchemaService $schemaService)
    {
        $this->schemaService = $schemaService;
    }

    public function getPredefinedKeys()
    {
        return [
            PredefinedKey::createKey(
                name: 'zicht.foo_bundle.my_predefined_key',
                default:  $this->getDefaultValue('zicht.foo_bundle.my_predefined_key', false),
                pretty_name: 'My predefined key',
                form_type: 'checkbox',
                form_options: ['required' => false]
            ),
            PredefinedKey::createKey(
                'zicht.foo_bundle.display_almost_soldout_threshold',
                100,
                'Set the threshold-amount for showing "almost soldout!" on various places in the website',
                'number'
            ),
            PredefinedKey::createKey(
                'zicht.foo_bundle.zipcode_required', // THIS IS LOCALE DEPENDENT
                $this->getDefaultValue('', ['nl' => true, 'en' => false]),
                'Show the zip-code input when creating a user account, this is not required for all visitors',
                'zicht_locale_dependent_type',
                ['type' => 'checkbox']
            ),
            PredefinedJsonSchemaKey::createKey(
                $this->schemaService,
                '/bundles/yourbundle/key-value-storage/foo-config.schema.json', # relative to the public web dir
                $this->getDefaultValue('foo-config.schema.json', [])
            ),
        ];
    }
}

每个定义的键都有一个唯一的键和一个默认值。当用户想要更改关联的值时,自定义值将作为 json_data 字段存储在数据库中。

为了区分不同包之间的键,建议使用以下配置:vendor.bundle_name.purpose_of_this_key

每个定义的键都有一个表单类型和表单选项,允许简单的输入,例如表单类型 'text',或者复杂的输入,例如带有选项 ['data_class' => null] 的表单类型 'file',后者将请求文件上传。

确保目录 web/media/key_value_storage 存在且可写。

配置

zicht_key_value:
    locales:
        -
            locale: nl
            label: Nederlands
        -
            locale: en
            label: Engels
    json_defaults:
        zicht.foo_bundle.homepage_url: '{"nl":"/nl/thuis","en":"/en/home"}'
    defaults:
        zicht.foo_bundle.display_threshold: 200
    cache:
        type: service
        id: cache.app # or another service you have defined

缓存

如果启用,则缓存的项将使用 KeyValueStorageManager::CACHE_TAG 前缀并使用 KeyValueStorageManager::CACHE_TAG 标签。

服务

如果您想在代码中的任何位置使用键值存储,则提供了一个存储服务。以 zicht_bundle_key_value.key_value_storage_manager 的名称注册。

Twig

还有一个可用的 twig 扩展。使用带有 key 参数的 get_from_key_value_storage 方法返回 value

Symfony

new Zicht\Bundle\KeyValueBundle\ZichtKeyValueBundle(), 添加到您的 AppKernelregisterBundles

Sonata

要使用 zicht_bundle_key_value.admin.key_value_admin 的管理,在您的 sonata_admin 配置中使用此配置以显示您的键的管理。

要更改其中一个键的值,您必须先添加。添加值后,它变得可编辑。

待办事项

  • 在决定定义键之前,列出应该提出的问题(并且答案为“是”)。这些问题可以是:这个值是否经常修改?我们是否希望允许客户端访问这个值及其修改?是否有替代方案?这个值是否将在依赖注入模式中使用?这将防止创建不适合在 KeyValueStorage 中使用的键。

  • 构建一个桥接器来解耦 Symfony 和 Sonata。

维护者