Download the PHP package fedale/setting-bundle without Composer
On this page you can find all versions of the php package fedale/setting-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fedale/setting-bundle
More information about fedale/setting-bundle
Files in fedale/setting-bundle
Package setting-bundle
Short Description Symfony bundle for runtime, multi-tenant application settings stored in the database
License MIT
Informations about the package setting-bundle
Fedale Setting Bundle
Multi-tenant application settings stored in the database and editable at runtime — not Symfony parameters compiled into the container.
- Per-tenant fallback:
tenant N→tenant 0(global) → caller-provided default. - Layered PSR-6 cache, automatically invalidated on every write.
- Abstract storage (
SettingStorageInterface): a Doctrine ORM bridge is included, but it can be swapped (API, file, Mongo, ...). - Free-form keys: no closed set, any string is a valid key.
- Optional per-key value validation, opt-in and enforced on every write.
- Optional scoped settings: declared, typed settings resolved along a chain of scopes (e.g. global → grid), on top of the tenant fallback.
Installation
Register the bundle (Symfony Flex does it for you):
Configuration (optional, defaults shown):
Database schema
The bundle auto-registers the ORM mapping of the Setting entity; generate the
migration in your app with php bin/console make:migration. Equivalent schema:
tenant_id = 0 holds the global default configuration; tenant_id > 0 holds the
per-tenant overrides.
value is stored as a string; type (string|int|float|bool|json) drives the
(de)serialization: scalars stay human-readable (123, not "123"), complex values
are stored as JSON under type = 'json'.
Usage
Reading settings in Twig
When TwigBundle is installed, the bundle registers a setting() function. It is
read-only — writes are done in PHP via SettingsManagerInterface::set().
Signature:
Examples:
Key points:
- The key is a free-form string — any setting created at runtime (including from an admin UI) is readable, with no registration required anywhere.
- The per-tenant fallback applies: current tenant first, then tenant 0 (global),
finally the
defaultyou pass in the template. - The function exists only if Twig is installed. In a Symfony app with
TwigBundle that is already guaranteed; registration is conditional, see
FedaleSettingBundle::loadExtension().
Multi-tenant: resolving the current tenant
When tenantId is not passed, the bundle uses TenantProviderInterface. By default
(DefaultTenantProvider) the current tenant is always 0. Multi-tenant apps
register their own implementation:
Custom storage
To use a source other than Doctrine, implement
Fedale\SettingBundle\Contract\SettingStorageInterface and point
fedale_setting.provider at the service id. Caching and the per-tenant fallback are
still handled by the bundle.
Validating values
By default keys are free-form: any value is accepted. You can opt specific
keys into validation, enforced at the single write choke point
(SettingsManager::set()), so the rules hold no matter where the write comes from
— a form, a CLI command or plain code. Keys without declared rules stay free-form.
Validation builds on symfony/validator (install it: composer require symfony/validator). Declare the per-key constraints by registering a service that
implements SettingConstraintsProviderInterface; the bundle ships
ArrayConstraintsProvider for the common static-map case:
Then enable validation, pointing it at the provider:
Now a write that breaks a rule throws SettingValidationException instead of
persisting:
For dynamic rules (e.g. constraints stored in the database or computed per key),
implement SettingConstraintsProviderInterface directly instead of using
ArrayConstraintsProvider. Return [] for any key that must stay free-form:
With autowiring, just point constraints_provider at the class id:
How the validation flow fits together
SettingsManagerInterface::set($key, $value, ...)is called (form, CLI or code).- Before persisting, the manager calls the configured
SettingValidatorInterface. - With validation enabled,
ConstraintsSettingValidatorasks your provider for the constraints of$key. No constraints → the value is accepted (free-form). - Otherwise the value is checked with
symfony/validator; on failure aSettingValidationExceptionis thrown and nothing is written.
This means the rules are enforced once, at the single write choke point, so you cannot bypass them by writing from a different entry point.
Scoped settings
Free-form keys fit application-wide settings. Some values instead depend on where they apply: a grid option set for every grid or for one grid, a booking rule set for an event type, a resource or a single occurrence. Scoped settings cover that case: a consumer bundle declares its settings, and the bundle resolves each one along a chain of scopes, most specific first.
They are an additional layer: SettingsManagerInterface, the setting table and
free-form keys are untouched.
Enabling them
With the default store: null the feature is off: ScopedSettingsResolver::isEnabled()
returns false, lookups find nothing and writes throw. The resolver and the writer
are always registered, so a bundle can depend on them unconditionally.
The doctrine store uses its own table, mapped only when it is selected; generate
the migration with php bin/console make:migration. Equivalent schema:
A lookup reads only the scopes of its chain, through a PSR-6 cache (the pool and
ttl of fedale_setting.cache) with one item per tenant, namespace and scope.
Levels with many ids, such as occurrences or users, stay cheap.
Declaring a setting
One class per setting, implementing SettingDefinitionInterface or extending
AbstractSettingDefinition. With autoconfiguration it is picked up automatically
(tag fedale_setting.definition).
| Part | Meaning |
|---|---|
namespace() / key() |
Identity; [a-z0-9_.], the key is unique within the namespace |
type() |
Bool, Int, Float, String, Enum |
supportsScope() |
The scope levels the setting can be set at |
choices() |
label key => value; required for Enum, null = free input |
constraints() |
Validator constraints checked on write, when symfony/validator is installed |
isApplicable($value, $context) |
Guard at resolve time, with a context supplied by the caller |
There is no default value in the definition: when nothing is found the setting is simply missing from the result, and the consumer keeps its own configuration in code.
Resolving: tenant first, then the chain
A scope is a level and, except for global, an id: Scope::global(),
new Scope('grid', 'users'). The consumer builds the chain, most specific first.
Tenant 0 holds platform defaults, which never beat a tenant's own choice, so
the whole chain of the current tenant is walked before the one of tenant 0:
With these stored values:
| tenant | scope | renderer |
|---|---|---|
| 0 | global | table |
| 0 | grid:users | list |
| 5 | global | cards |
tenant 5 gets cards on the users grid (its own global beats the platform's
per-grid default), any other tenant gets list there and table elsewhere.
A candidate is skipped, and the lookup moves on, when:
- its scope level is no longer supported by the definition (logged);
- the stored value no longer fits the definition: wrong type, choice no longer offered (logged);
isApplicable($value, $context)returnsfalse. The third argument ofresolve()/get()is passed through as$context. For example, gridview passes the grid's options and rejects a renderer that grid does not support, so the global value is used instead.
The same lookup with five levels, for a booking rule:
Writing
ScopedSettingsWriter is the single write choke point:
Each value must be a declared setting, settable at that scope, of the right type,
one of the choices if any, and pass constraints(). Keys not passed are left
untouched.
Custom store
Implement Fedale\SettingBundle\Scoped\Store\ScopedStoreInterface and point
fedale_setting.scoped.store at the service id. The store persists strings and
does not know the setting types; the bundle wraps it in the PSR-6 cache.
Cache
The settings.0 and settings.{tenantId} layers are cached separately and merged
in memory on each read. A set() only invalidates the layer it touches: changing a
global default (tenant 0) therefore propagates to all tenants by invalidating a
single entry.
License
MIT.
All versions of setting-bundle with dependencies
psr/cache Version ^3.0
psr/log Version ^1.1 || ^2.0 || ^3.0
symfony/config Version ^6.4 || ^7.0 || ^8.0
symfony/dependency-injection Version ^6.4 || ^7.0 || ^8.0
symfony/http-kernel Version ^6.4 || ^7.0 || ^8.0
symfony/service-contracts Version ^2.5 || ^3.0