-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.jekyll-cache/ | ||
_site/ | ||
|
||
Gemfile.lock | ||
.bundles_cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
source "https://rubygems.org" | ||
|
||
# do NOT include the Jekyll gem ! | ||
#gem 'github-pages', group: :jekyll_plugins | ||
gem "kramdown-parser-gfm" | ||
gem "just-the-docs" | ||
gem "jekyll" | ||
gem 'jekyll-seo-tag' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
title: settings-bundle | ||
description: A bundle to allow typesafe, user-configurable settings for symfony applications. | ||
remote_theme: just-the-docs/just-the-docs | ||
|
||
#url: "https://docs.part-db.de" | ||
#baseurl: "/" # the subpath of your site, e.g. /blog | ||
|
||
url: "https://jbtronics.github.io" | ||
baseurl: "/settings-bundle" # the subpath of your site, e.g. /blog | ||
|
||
#favicon_ico: "/assets/favicon.ico" | ||
|
||
aux_links: | ||
Repository: https://github.com/jbtronics/settings-bundle | ||
#Demo: https://part-db.herokuapp.com | ||
|
||
|
||
search_enabled: true | ||
|
||
enable_copy_code_button: true | ||
heading_anchors: true | ||
|
||
back_to_top: true | ||
back_to_top_text: "Back to top" | ||
|
||
# Footer "Edit this page on GitHub" link text | ||
gh_edit_link: true # show or hide edit this page link | ||
gh_edit_link_text: "Edit this page on GitHub." | ||
gh_edit_repository: "https://github.com/jbtronics/settings-bundle" # the github URL for your repo | ||
gh_edit_branch: "master" # the branch that your docs is served from | ||
gh_edit_source: docs # the source that your files originate from | ||
gh_edit_view_mode: "tree" # "tree" or "edit" if you want the user to jump into the editor immediately | ||
|
||
callouts_level: quiet # or loud | ||
callouts: | ||
highlight: | ||
color: yellow | ||
important: | ||
title: Important | ||
color: blue | ||
new: | ||
title: New | ||
color: green | ||
note: | ||
title: Note | ||
color: purple | ||
warning: | ||
title: Warning | ||
color: red | ||
|
||
plugins: | ||
- jekyll-seo-tag |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
url: http://localhost:4000 | ||
baseurl: "/" | ||
|
||
theme: just-the-docs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
--- | ||
title: Home | ||
layout: home | ||
nav_order: 0 | ||
--- | ||
|
||
# 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 | ||
* 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 | ||
* Profiler integration for easy debugging | ||
|
||
## Installation | ||
|
||
Add the bundle to your symfony project via composer: | ||
```bash | ||
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: | ||
```php | ||
return [ | ||
// ... | ||
Jbtronics\SettingsBundle\SettingsBundle::class => ['all' => true], | ||
]; | ||
``` | ||
|
||
## Usage | ||
|
||
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 | ||
<?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 Symfony\Component\Validator\Constraints as Assert; | ||
|
||
|
||
#[Settings] // The settings attribute makes a simple class to settings | ||
class TestSettings { | ||
|
||
//The property is public here for simplicity, but it can also be protected or private | ||
#[SettingsParameter(type: StringType::class, label: 'My String', description: 'This value is shown as help in forms.')] | ||
public string $myString = 'default value'; // The default value can be set right here in most cases | ||
|
||
#[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. | ||
|
||
```php | ||
|
||
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 | ||
} | ||
} | ||
|
||
``` | ||
|
||
## 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](https://github.com/jbtronics/settings-bundle/blob/master/LICENSE) for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/bash | ||
|
||
# | ||
# This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). | ||
# | ||
# Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics) | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published | ||
# by the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
# | ||
|
||
# Serve the documentation locally with jekyll | ||
bundle exec jekyll serve --config "_config.yaml,_config_dev.yaml" --livereload --watch |