PHP code example of fedale / setting-bundle

1. Go to this page and download the library: Download fedale/setting-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fedale / setting-bundle example snippets


// config/bundles.php
return [
    Fedale\SettingBundle\FedaleSettingBundle::class => ['all' => true],
];

use Fedale\SettingBundle\Contract\SettingsManagerInterface;

final class GeneralController
{
    public function __construct(
        private readonly SettingsManagerInterface $settings,
    ) {
    }

    public function index(): Response
    {
        // tenant 12: "Cliente XYZ" if an override exists, otherwise the global default
        $title = $this->settings->get('general.title', 'Gestionale');
        $theme = $this->settings->get('general.theme', 'classic'); // inherited from tenant 0
        $useCache = $this->settings->get('general.useCache', false); // already a bool

        // write (current tenant, resolved by the TenantProvider)
        $this->settings->set('general.footer', '(c) 2026', type: 'string');
        $this->settings->set('general.useCache', true, type: 'bool');

        // write explicitly to a tenant / to the global scope
        $this->settings->set('general.title', 'Cliente XYZ', tenantId: 12);
        $this->settings->set('general.theme', 'dark', tenantId: 0); // default for everyone

        // ...
    }
}

// src/Setting/RequestTenantProvider.php
use Fedale\SettingBundle\Contract\TenantProviderInterface;

final class RequestTenantProvider implements TenantProviderInterface
{
    public function __construct(private readonly RequestStack $stack) {}

    public function getCurrentTenantId(): int
    {
        return (int) $this->stack->getCurrentRequest()?->attributes->get('_tenant_id', 0);
    }
}

// config/services.php
use Fedale\SettingBundle\Contract\SettingConstraintsProviderInterface;
use Fedale\SettingBundle\Setting\Validation\ArrayConstraintsProvider;
use Symfony\Component\Validator\Constraints as Assert;

return static function (ContainerConfigurator $container): void {
    $container->services()
        ->set('app.setting_constraints', ArrayConstraintsProvider::class)
        ->args([[
            'myKey'         => [new Assert\Length(min: 3, max: 10)],
            'general.title' => [new Assert\NotBlank()],
            'general.email' => [new Assert\Email()],
        ]]);

    $container->services()
        ->alias(SettingConstraintsProviderInterface::class, 'app.setting_constraints');
};

try {
    $this->settings->set('myKey', 'ab'); // shorter than 3
} catch (\Fedale\SettingBundle\Exception\SettingValidationException $e) {
    $e->getKey();                // 'myKey'
    $e->getViolationMessages();  // ['This value is too short. It should have 3 characters or more.']
}

// src/Setting/AppSettingConstraintsProvider.php
use Fedale\SettingBundle\Contract\SettingConstraintsProviderInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints as Assert;

final class AppSettingConstraintsProvider implements SettingConstraintsProviderInterface
{
    /**
     * @return list<Constraint>
     */
    public function constraintsFor(string $key): array
    {
        return match (true) {
            // every key under "limits." must be a positive integer
            str_starts_with($key, 'limits.') => [new Assert\Positive()],
            'myKey' === $key                 => [new Assert\Length(min: 3, max: 10)],
            default                          => [], // free-form
        };
    }
}

use Fedale\SettingBundle\Scoped\AbstractSettingDefinition;
use Fedale\SettingBundle\Scoped\Scope;
use Fedale\SettingBundle\Scoped\SettingType;
use Symfony\Component\Validator\Constraints as Assert;

final class PageSizeSetting extends AbstractSettingDefinition
{
    public function namespace(): string { return 'gridview.ui'; }
    public function key(): string { return 'page_size'; }
    public function type(): SettingType { return SettingType::Int; }
    public function label(): string { return 'ui_settings.page_size'; }

    public function supportsScope(Scope $scope): bool
    {
        return \in_array($scope->level, ['global', 'grid'], true);
    }

    public function constraints(): array
    {
        return [new Assert\Range(min: 5, max: 500)];
    }
}

use Fedale\SettingBundle\Scoped\Scope;
use Fedale\SettingBundle\Scoped\ScopeChain;
use Fedale\SettingBundle\Scoped\ScopedSettingsResolver;

$chain = ScopeChain::of(new Scope('grid', 'users'), Scope::global());

// Every resolved setting of the namespace: key => ResolvedValue
$resolved = $resolver->resolve('gridview.ui', $chain, $gridOptions);

$resolved['renderer']->value;     // 'cards', already in the native type
$resolved['renderer']->scope;     // Scope global: where it comes from...
$resolved['renderer']->tenantId;  // 5: ...and for which tenant

// A single setting, or null
$resolver->get('gridview.ui', 'page_size', $chain)?->value;

// The values stored at exactly one scope, without inheritance (e.g. to fill a form)
$resolver->values('gridview.ui', new Scope('grid', 'users'));

// What each setting falls back to if the head of the chain is cleared for the
// current tenant: the "inherit" hint of the form editing that scope
$resolver->inherited('gridview.ui', $chain, $gridOptions);

$chain = ScopeChain::of(
    new Scope('occurrence', '981'),
    new Scope('series', '12'),
    new Scope('resource', '7'),
    new Scope('eventType', 'lesson'),
    Scope::global(),
);

$deadline = $resolver->get('booking.policy', 'cancel_deadline_hours', $chain)?->value
    ?? 24; // the consumer's own default

use Fedale\SettingBundle\Exception\SettingValidationException;
use Fedale\SettingBundle\Scoped\ScopedSettingsWriter;

try {
    $writer->save('gridview.ui', new Scope('grid', 'users'), [
        'renderer'  => 'cards',
        'page_size' => '50',  // form input is accepted and cast to int
        'striped'   => null,  // null or '' clears the key: back to "inherit"
    ]);                       // 4th argument: tenant id, the current one by default
} catch (SettingValidationException $e) {
    $e->getKey();               // the first invalid key; nothing was saved
    $e->getViolationMessages();
}