dwalczyk / setting-bundle
Symfony bundle that allows you to easily define, read and change settings.
Installs: 6
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.2
- doctrine/orm: ^2.11
- symfony/cache: ^6.0|^7.0
- symfony/framework-bundle: ^6.0|^7.0
- symfony/property-access: ^6.0|^7.0
- symfony/serializer: ^6.0|^7.0
- symfony/stopwatch: ^6.0|^7.0
- twig/twig: ^3.0
- webmozart/assert: ^1.11
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.5
- spaze/phpstan-disallowed-calls: ^3.1
README
Setting bundle is a bundle of the Symfony framework that allows you to easily define, read and change settings.
Integrated with:
- symfony cache
- doctrine orm
- symfony profiler
- symfony serializer
- twig
Table of Contents
- Installation & configuration
- Usage
- Data storages
- Data transformers
- Cache
- More effective user (setting owner) passing
- Twig
- Symfony profiler
Installation & configuration
- Run composer require
composer require dwalczyk/setting-bundle
- create file
config/dwalczyk_setting.yaml
and paste the contents from below:
dwalczyk_setting: data_storage: 'DWalczyk\SettingBundle\Extension\Doctrine\DataStorage\DoctrineDataStorage' #cache: cache.app #definitions: []
- Add to
config/doctrine.yaml
doctrine: orm: mappings: SettingBundle: is_bundle: true dir: 'Resources/config/doctrine' prefix: 'DWalczyk\SettingBundle\Extension\Doctrine\Entity' alias: SettingBundle
- Create and execute migration
php bin/console doctrine:migrations:diff php bin/console d:m:m
Usage
- First we need to define the setting.
via php
<?php declare(strict_types=1); namespace App; use DWalczyk\SettingBundle\AbstractSettingExtension; use DWalczyk\SettingBundle\SettingDefinition; final class SettingExtension extends AbstractSettingExtension { public function getDefinitions(): array { return [ new SettingDefinition(name: 'black_mode', type: 'bool', defaultValue: false), ]; } }
or via yaml configuration
dwalczyk_setting: definitions: black_mode: type: bool default_value: false
- Now you can read or set value globally or per user.
#[Route('/')] public function test(SettingsInterface $settings): Response { $settings->get('black_mode'); // false $settings->get('black_mode', 'John'); // false // set "global scope" value $settings->set('black_mode', true); $settings->get('black_mode'); // true $settings->get('black_mode', 'John'); // true // set "user scope" value $settings->set('black_mode', false, 'John'); $settings->get('black_mode'); // true $settings->get('black_mode', 'John'); // false // ... }
Data storages
Data storages are classes responsible for writing and reading previously saved settings.
Available built-in data storages:
DWalczyk\SettingBundle\Extension\Doctrine\DataStorage\DoctrineDataStorage
- Doctrine ORM storage
You can create your custom data storage, just create symfony service that implements
DWalczyk\SettingBundle\DataStorageInterface
and insert its name to configuration.
namespace App\DataStorage; use DWalczyk\SettingBundle\DataStorageInterface; class Custom implements DataStorageInterface { public function read(string $name, ?string $ownerIdentifier): ?string { // TODO: Implement read() method. } public function write(string $name, ?string $value, ?string $ownerIdentifier): void { // TODO: Implement write() method. } }
dwalczyk_setting: data_storage: 'App\DataStorage\Custom'
Data transformers
Data transformers determine how to format data before writing and after reading from data storage.
They are also responsible for formatting the values from the "defaultValue" definition.
Built-in data transformers:
DoctrineDataTransformer
- handle doctrine types, read moreSerializerDataTransformer
- handle all types supported by symfony serializerNativePhpSerializerDataTransformer
- handle all types supported by serialize/unserialize native php functions
By default, data transformers are loaded in the order:
DoctrineDataTransformer
SerializerDataTransformer
If you want to use NativePhpSerializerDataTransformer
instead of SerializerDataTransformer
add this code to your config:
services: DWalczyk\SettingBundle\Extension\Core\DataTransformer\NativePhpSerializerDataTransformer: tags: - { name: 'dwalczyk_setting.data_transformer', priority: 1 }
Or create custom data transformer
<?php namespace App; use DWalczyk\SettingBundle\DataTransformerInterface; use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem; #[AsTaggedItem('dwalczyk_setting.data_transformer', priority: 2)] class CustomDataTransformer implements DataTransformerInterface { // implement required methods }
DoctrineDataTransformer
Supported types:
doctrine-entity<fqcn/of/entity>
- single entity e.g.doctrine-entity<App\Mail\Entity\MailTemplate>
doctrine-entity<fqcn/of/entity>[]
- multiple entity e.g.doctrine-entity<App\Mail\Entity\MailTemplate>[]
Cache
To use cache, you must configure cache in your symfony configuration and then cache
option in the bundle configuration.
framework: cache: app: cache.adapter.filesystem
dwalczyk_setting: cache: cache.app
or with custom pool:
framework: cache: pools: setting_pool: adapter: cache.adapter.filesystem
dwalczyk_setting: cache: setting_pool
if you don't want to use cache, leave the cache
option empty or do not define it.
More effective user (setting owner) passing
Implement SettingOwnerInterface
in your security user class.
class User implements UserInterface, PasswordAuthenticatedUserInterface, SettingOwnerInterface { public function getSettingIdentifier(): string { return (string) $this->id; } ... }
Now you are able to call:
#[Route('/')] public function test(SettingsInterface $settings): Response { $settings->get('black_mode', $this->getUser()); // or $settings->set('black_mode', true, $this->getUser()); // ... }
Twig
Functions:
{{ setting(settingName) }}