PHP code example of temant / settings-manager

1. Go to this page and download the library: Download temant/settings-manager 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/ */

    

temant / settings-manager example snippets


use Temant\SettingsManager\SettingsManager;
use Temant\SettingsManager\Enum\SettingType;

// Create a manager with your Doctrine EntityManager
$manager = new SettingsManager($entityManager);

// Set values — type is auto-detected
$manager->set('site.name', 'Acme Corp');
$manager->set('cache.ttl', 3600);
$manager->set('debug', false);

// Get typed values back
$manager->getValue('site.name');   // 'Acme Corp' (string)
$manager->getValue('cache.ttl');   // 3600 (int)
$manager->getValue('debug');       // false (bool)

// Safe defaults for missing keys
$manager->getOrDefault('ui.theme', 'dark'); // 'dark'

use Temant\SettingsManager\Enum\SettingType;

// Auto-detect type (default)
$manager->set('key', 'value');
$manager->set('count', 42);
$manager->set('enabled', true);
$manager->set('rate', 0.75);
$manager->set('config', '{"nested": true}');    // detected as JSON
$manager->set('tags', ['php', 'doctrine']);      // stored as ARRAY
$manager->set('launch', new DateTimeImmutable()); // stored as DATETIME

// Explicit type
$manager->set('port', '8080', SettingType::STRING); // force string, not int

// With metadata
$manager->set(
    name: 'smtp.host',
    value: 'mail.example.com',
    description: 'SMTP server hostname',
    group: 'email',
);

// Prevent accidental overwrites
$manager->set('api.key', 'secret', allowUpdate: false);
// throws SettingAlreadyExistsException if 'api.key' exists

// Full entity (with metadata, timestamps, etc.)
$entity = $manager->get('site.name');
$entity->getValue();       // typed value
$entity->getRawValue();    // raw string from DB
$entity->getType();        // SettingType::STRING
$entity->getDescription(); // ?string
$entity->getGroup();       // ?string
$entity->getCreatedAt();   // DateTimeImmutable
$entity->getUpdatedAt();   // ?DateTimeImmutable

// Shorthand — typed value directly
$manager->getValue('site.name');              // 'Acme Corp'

// With fallback
$manager->getOrDefault('missing.key', 'default'); // 'default'

// Existence check
$manager->exists('site.name'); // true
$manager->has('site.name');    // alias

use Temant\SettingsManager\Enum\UpdateType;

// Update value and auto-detect new type
$manager->update('cache.ttl', 7200);

// Update value but keep the original type (validates compatibility)
$manager->update('cache.ttl', 'not-an-int', UpdateType::KEEP_CURRENT);
// throws SettingTypeMismatchException

$manager->remove('old.setting');

// Remove multiple
$manager->removeMany(['key1', 'key2', 'key3']);

// Remove everything
$manager->clear();

$manager->setMany([
    'site.name'  => ['value' => 'Acme', 'type' => SettingType::STRING, 'group' => 'site'],
    'site.url'   => ['value' => 'https://acme.dev', 'group' => 'site'],
    'cache.ttl'  => ['value' => 3600, 'description' => 'Cache lifetime in seconds'],
    'debug.mode' => ['value' => false],
]);

// Substring search (case-insensitive)
$results = $manager->search('site');    // all settings with 'site' in the name

// Group filtering
$emailSettings = $manager->findByGroup('email');

$total = count($manager); // implements Countable

// Export all settings to array
$data = $manager->export();

// Export to JSON
$json = $manager->exportJson();

// Import from array
$manager->import($data);

// Import from JSON
$manager->importJson($json);

// Static utility classes also available
use Temant\SettingsManager\Utils\SettingsExporter;
use Temant\SettingsManager\Utils\SettingsImporter;

$json = SettingsExporter::toJson($manager);
SettingsImporter::fromJson($manager, $json);

$manager = new SettingsManager($entityManager, 'settings', [
    'site.name' => [
        'value' => 'My App',
        'type' => SettingType::STRING,
        'description' => 'Application name',
        'group' => 'site',
    ],
    'cache.ttl' => [
        'value' => 3600,
        // type auto-detected as INTEGER
    ],
    'debug' => [
        'value' => false,
    ],
]);

$manager = new SettingsManager($entityManager, 'app_config');

$manager->clearCache();