zicht / key-value-bundle
Zicht KeyValue Bundle
Requires
- php: ^7.4 || ^8
- doctrine/common: ^2.13 || ^3
- doctrine/orm: ^2 || ^3
- doctrine/persistence: ^1 || ^2
- sonata-project/admin-bundle: ^3 || ^4
- swaggest/json-schema: ^0.12
- symfony/dependency-injection: ^5.4
- symfony/form: ^4.4 || ^5
- symfony/translation-contracts: ^1.1 || ^2 || ^3
- twig/string-extra: ^3.3
- twig/twig: ^2.12.1 || ^3
- zicht/framework-extra-bundle: ^9 || ^10
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- phpunit/phpunit: ^9.6
- vimeo/psalm: ^5
- dev-release/5.x
- dev-release/4.x
- 5.1.0
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.6.2
- 4.6.1
- 4.6.0
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.2
- 4.2.1
- 4.2.0
- 4.1.0
- 4.0.1
- 4.0.0
- 3.5.1
- 3.5.0
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.1
- 3.3.0
- 3.2.0
- 3.1.0
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.1
- 2.3.0
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
- dev-sonata-4--1
- dev-feature/translate-json-schemas
- dev-release/3.x
- dev-release/2.x
- dev-release/1.0.x
This package is auto-updated.
Last update: 2024-09-06 14:22:29 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(),
添加到您的 AppKernel
的 registerBundles
。
Sonata
要使用 zicht_bundle_key_value.admin.key_value_admin
的管理,在您的 sonata_admin
配置中使用此配置以显示您的键的管理。
要更改其中一个键的值,您必须先添加。添加值后,它变得可编辑。
待办事项
-
在决定定义键之前,列出应该提出的问题(并且答案为“是”)。这些问题可以是:这个值是否经常修改?我们是否希望允许客户端访问这个值及其修改?是否有替代方案?这个值是否将在依赖注入模式中使用?这将防止创建不适合在 KeyValueStorage 中使用的键。
-
构建一个桥接器来解耦 Symfony 和 Sonata。
维护者
- Erik Trapman erik@zicht.nl