rares/database-config-bundle

Bundle that lets you save configuration values in the database.

2.0.2 2018-07-17 07:51 UTC

This package is auto-updated.

Last update: 2024-04-18 01:36:35 UTC


README

The DatabaseConfigBundle is a bundle for Symfony 3.4 / 4 which provides functionality for saving user defined parameters into the database and then retrieving them easily.

Installation

To install this bundle, you need to require it in Composer:

    composer require "rares/database-config-bundle"

Then enable the bundle in your AppKernel file:

$bundles = [
    ...,
    new Rares\DatabaseConfigBundle\RaresDatabaseConfigBundle(),
];

Then just update your database schema:

bin/console doctrine:schema:update --force

This bundle comes with two entities, ConfigGroup (which uses the table database_config_group) and ConfigValue (which uses the table database_config_value) so make sure that no table conflicts exist.

Features

This bundle lets you easily read/write/delete configuration parameters, as well as group your parameters into different groups, which can also have parent groups if so desired.

Service

To use the bundle, you just have to get one service from the container:

use Rares\DatabaseConfigBundle\Service\DatabaseConfigService;

$service = $this->get(DatabaseConfigService::class);

The service has the following basic methods:

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.

Also, the getGroup($name) function returns a ConfigGroupModel class which has some helper functions that lets you interact easily with the config values of that group:

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 Extension

This bundle also provides a simple Twig extension that lets you easily retrieve config values in your templates:

{{ database_config_value('value_name', 'group_name') }}

The group name is optionally.This simply calls the get($name, $group = null) method from the DatabaseConfigService.

Make sure to take a look at the demo branch for an example on how to use this bundle.