• Stars
    star
    73
  • Rank 433,657 (Top 9 %)
  • Language
    PHP
  • License
    MIT License
  • Created 11 months ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A bundle to easily create typesafe, user-configurable settings for symfony applications

Documentation

codecov

Settings bundle

Settings-bundle is a symfony bundle that let you manage your application settings on the frontend side.

Introduction

By default symfony is mostly configured by parameters in configuration files, where a recompilation of the container is required, or via environment variables, which can not be easily changed by the application itself.

However you often want administrators and users of your application let change settings and configuration of your application. This bundle provides a simple way to do this. Unlike other bundles with a similar goal, this bundle tries to be as modular as possible and to be as type-safe as possible. Therefore you define your Settings as a class, and access objects of this class in your application, instead of doing simple key-value lookups with mixed return types.

All relevant definitions of settings are done directly in the settings class via metadata attributes. This makes it easy to understand and maintain the settings. The bundle also provides a simple way to generate forms to change the settings, which can be easily integrated into your application.

Features

  • Class based settings, which get easily managed by the bundle
  • Type-safe access to settings
  • Easy to use API
  • Almost zero configuration required in many cases, as the bundle tries to derive as much information as possible from code metadata like property types, etc.
  • Various storage backends, like database, json files, PHP files, etc. (custom backends can be easily implemented)
  • Use symfony/validator to easily restrict possible values of settings parameters
  • Automatically generate forms to change settings
  • Easy possibility to version settings and automatically migrate old stored data to the current format
  • Possibility to lazy load settings, so that only the settings, which are really needed, are loaded
  • Profiler integration for easy debugging

Installation

Add the bundle to your symfony project via composer:

composer require jbtronics/settings-bundle

If you are using symfony flex, the bundle should be automatically enabled. Otherwise you have to add the bundle to your config/bundles.php file:

return [
    // ...
    Jbtronics\SettingsBundle\JbtronicsSettingsBundle::class => ['all' => true],
];

Usage

The following section is just a quick overview. See documentation for full info.

Settings classes are simple PHP classes, which are annotated with the #[Settings] attribute. They must live in the path configured to store settings classes (normally src/Settings), in your symfony project. The bundle will automatically find and register all settings classes in this directory.

The properties of the classes are used for storing the different data. Similar to the #[ORM\Column] attribute of doctrine, you can use the #[SettingsParameter] attribute to make a class property to a managed parameter. The properties can be public, protected or private (as SettingsBundle accesses them via reflection), but you have some kind of possibility to access the properties to get/set the configuration parameters in your software. You have to configure, which type mapper should be used to map the normalized data from the storage backend to the type of property. The bundle comes with a few default type mappers, but you can also implement your own type mappers.

<?php
// src/Settings/TestSettings.php

namespace App\Settings;

use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
use Jbtronics\SettingsBundle\ParameterTypes\StringType;
use Jbtronics\SettingsBundle\ParameterTypes\IntType;
use Jbtronics\SettingsBundle\Storage\JSONFileStorageAdapter;
use Symfony\Component\Validator\Constraints as Assert;


#[Settings(storageAdapter: JSONFileStorageAdapter::class)] // The settings attribute makes a simple class to settings
class TestSettings {
    use SettingsTrait; // Disable constructor and __clone methods

     //The properties are public here for simplicity, but it can also be protected or private

    //In many cases this attribute with zero config is enough, the type mapper is then derived from the declared type of the property
    #[SettingsParameter()]
    public string $myString = 'default value'; // The default value can be set right here in most cases

    //Or you can explicitly set the type mapper and some options
    #[SettingsParameter(type: IntType::class, label: 'My Integer', description: 'This value is shown as help in forms.')] 
    #[Assert\Range(min: 5, max: 10,)] // You can use symfony/validator to restrict possible values
    public ?int $myInt = null;
}

The main way to work with settings is to use the SettingsManagerInterface service. It offers a get() method, which allows to retrieve the current settings for a given settings class. If not loaded yet, the manager will load the desired settings from the storage backend (or initialize a fresh instance with default values). The instances are cached, so that the manager will always return the same instance for a given settings class. The manager also offers a save() method, which allows to save the current settings to the storage backend and persist the changes.

use Jbtronics\SettingsBundle\Settings\SettingsManagerInterface;

class ExampleService {
    public function __construct(private SettingsManagerInterface $settingsManager) {}

    public function accessAndSaveSettings(): void
    {
        /** @var TestSettings $settings This is an instance of our previously defined setting class, containing the stored settings */
        $settings = $this->settingsManager->get(TestSettings::class);

        //To read the current settings value, just access the property
        dump('My string is: ' . $settings->myString);

        //To change the settings, just change the property (or call the setter)
        $settings->myString = 'new value';

        //And save the settings to the storage backend
        $this->settingsManager->save($settings);


        //You can also access the settings via a given name (which is the part before the "Settings" suffix of the class name in lowercase, by default)
        $settings = $this->settingsManager->get('test');

        //You can set an invalid value to the parameters
        $settings->myInt = 42;

        //But the bundle will throw an exception, when you try to save the settings
        $this->settingsManager->save($settings); // Throws an excpetion
    }
}

Forms

The bundle can automatically generate forms to change settings classes. This is done via the SettingsFormFactoryInterface, which creates a form builder containing fields to edit one or more settings classes. You can also render just a subset of the settings. Validation attributes are checked and mapped to form errors. This way you can easily create a controller, to let users change the settings:

<?php

class SettingsFormController {

    public function __construct(
        private SettingsManagerInterface $settingsManager,
        private SettingsFormFactoryInterface $settingsFormFactory,
        ) {}

    #[Route('/settings', name: 'settings')]
    public function settingsForm(Request $request): Response
    {
        //Create a builder for the settings form
        $builder = $this->settingsFormFactory->createSettingsFormBuilder(TestSettings::class);

        //Add a submit button, so we can save the form
        $builder->add('submit', SubmitType::class);

        //Create the form
        $form = $builder->getForm();

        //Handle the form submission
        $form->handleRequest($request);

        //If the form was submitted and the data is valid, then it
        if ($form->isSubmitted() && $form->isValid()) {
            //Save the settings
            $this->settingsManager->save();
        }

        //Render the form
        return $this->render('settings.html.twig', [
            'form' => $form->createView(),
        ]);
    }
}

Form rendering can be customized via the Parameter attributes. See documentation for full info.

Twig templates

In twig templates you can access the settings via the settings_instance() function, which behaves like the SettingsManagerInterface::get() function and returns the current settings instance:

{# @var settings \App\Settings\TestSettings #}
{% set settings = settings_instance('test') %}
{{ dump(settings) }}

{# or directly #}
{{ settings_instance('test').myString }}

License

SettingsBundle is licensed under the MIT License. This mostly means that you can use Part-DB for whatever you want (even use it commercially) as long as you retain the copyright/license information.

See LICENSE for more information.

More Repositories

1

CrookedStyleSheets

Webpage tracking only using CSS (and no JS)
CSS
3,288
star
2

UE4-CheatSheet

An Cheat Sheet for Unreal Engine 4 C++ programming, licensed under CC BY-NC-SA 4.0
TeX
431
star
3

ESP32Console

Extensible UART console library for ESP32 with useful included commands.
C++
152
star
4

PCBruler

A PCB Ruler created in KiCAD
66
star
5

DS1054_BodePlotter

A Python program that plots Bode diagrams of a component using a Rigol DS1054Z and a JDS6600
Python
34
star
6

SDBatchToolsGUI

A Gui for the Substance Designer Tools
C#
24
star
7

WireDebugger

Debug AVRs with DebugWire via SerialPort
C#
21
star
8

AVR-ZIF-Programmer

An AVR Programmer based on USBasp which uses a ZIF-Socket for Programming.
KiCad Layout
10
star
9

2fa-webauthn

Webauthn Two-Factor-Authentictication Plugin for scheb 2fa
PHP
8
star
10

bs-treeview

TreeView element for browsers without any dependencies
TypeScript
7
star
11

FM1216duino

An Arduino Library to controlo FM1216-Tuner over I2C (only Soft-I2C)
C
7
star
12

FTController

A Motor controller for Fischertechnik with many cool features.
HTML
5
star
13

Source2Strings

A repository containing extracted strings from existing Source2 games
Shell
5
star
14

Recolldroid

A android app for comfortable use of Recoll-WebUI on android.
Java
4
star
15

ESP07-Breakout

The KiCAD Project files for my ESP07-Breakout
KiCad Layout
4
star
16

Part-DB-android

Part-DB Barcode Scanner for Android
Java
3
star
17

StuRa-Finanzsoftware

A little web project to submit PaymentOrders via a web formula and manage them in a backend. Developed for the student council of the University of Jena.
PHP
3
star
18

GolonkaBBCodeParser

A copy of Golonka\BBCode after original repo got deleted (it's a dependency for Part-DB)
PHP
2
star
19

dompdf-font-loader-bundle

A symfony bundle to easily load custom fonts for dompdf (on cache warming)
PHP
2
star
20

Portal2HQMaterials

High Quality remake Materials for Portal 2
2
star
21

FRMorp

A tool for dumping firmware from SPMP8000 devices originally written by openschemes.com
C
1
star
22

WiFI-Sinus-Gen

The ESP8266 Code for my WLAN DDS Sinus Generator
Arduino
1
star
23

WiFi-Sinus-Generator-android

The Repository for the WiFi-Sinus-Generator Android App
Java
1
star