rares / database-config-bundle
允许你在数据库中保存配置值的工具包。
2.0.2
2018-07-17 07:51 UTC
Requires
- php: ^7.0
- doctrine/orm: ^2.4.5
- symfony/config: ^3.4|^4.0
- symfony/dependency-injection: ^3.4|^4.0
- symfony/http-kernel: ^3.4|^4.0
- twig/twig: ^1.35|^2.0
README
DatabaseConfigBundle 是一个针对 Symfony 3.4 / 4 的工具包,它提供了将用户定义的参数保存到数据库中并轻松检索的功能。
安装
要安装此工具包,您需要在 Composer 中要求它。
composer require "rares/database-config-bundle"
然后在您的 AppKernel 文件中启用工具包。
$bundles = [
...,
new Rares\DatabaseConfigBundle\RaresDatabaseConfigBundle(),
];
然后只需更新您的数据库模式。
bin/console doctrine:schema:update --force
此工具包包含两个实体,ConfigGroup(使用数据库_config_group 表)和 ConfigValue(使用数据库_config_value 表),请确保没有表冲突。
功能
此工具包允许您轻松地读取/写入/删除配置参数,并将参数分组到不同的组中,如果需要,组也可以有父组。
服务
要使用此工具包,您只需从容器中获取一个服务。
use Rares\DatabaseConfigBundle\Service\DatabaseConfigService;
$service = $this->get(DatabaseConfigService::class);
该服务有以下基本方法
public function get($name, $group = null); // Lets you retrive a config value with a name and optionally a group.The name of the values are unique in their respective groups (null counts as the default group).Any values that do not exist will return NULL.
public function save($name, $value, $group = null); // Save a config value with a name and optionally a group.Any object that is supported by the Doctrine object column type can be saved (so pretty much any object that can be serialized).This functiona is also used to update a value of an existing item.The group does not have to exists beforehand.
public function delete($name, $group = null); // Delete a config value with a name and optionally a group.
public function getGroup($name); // Get the group with the specified name.The group has some helper functions that will be discussed below that let you retrieve config values belonging to that group more easily.
public function saveGroup($name, $parent = null); // Save the group with the specified name and optionally a parent.This creates a new group or updates an existing one with the respective parent.
public function deleteGroup($name); // Delete a group with the specific name.This will delete all the group values as well as any child groups.
public function getAll(); // Get all stored config values as ConfigValue entities, regardless of their group.
此外,getGroup($name) 函数返回一个 ConfigGroupModel 类,它包含一些辅助函数,可让您轻松地与该组的配置值交互
public function get($name); // Gets the value with the specified name from this group.Returns NULL if there is no value for that key.
public function save($name, $value); // Save a value with that name to this group.The value is updated if a config value already exists with that name.
public function delete($name); // Delete the config value with the specified name from this group.
public function getChild($name); // Get a child group by name from this group.Returns NULL if the child does not exist.
public function getAll(); // Get all the values belonging to this group as a key/value array.
public function getName(); // Get the name of this group.
public function getParent(); // Get the parent group.Returns NULL if the group has no parent.
Twig 扩展
此工具包还提供了一个简单的 Twig 扩展,允许您在模板中轻松检索配置值
{{ database_config_value('value_name', 'group_name') }}
组名称是可选的。这实际上调用的是 DatabaseConfigService 的 get($name, $group = null) 方法。
请务必查看 demo 分支,以了解如何使用此工具包的示例。