PHP code example of bonsaicms / settings

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

    

bonsaicms / settings example snippets


Settings::set('theme', 'dark');
Settings::set('limits', ['upload' => 10, 'daily' => 100]);
Settings::set('owner', User::first());   // an Eloquent model
Settings::save();

Settings::get('owner')->is($user);       // true — same model, re-fetched from the DB

Settings::set('theme', 'dark');   // in memory only — the store is untouched
Settings::get('theme');           // 'dark'
// ... request ends here without save() → the change is lost

Settings::set('theme', 'dark');
Settings::save();                 // now it is in the store

Settings::get('never-set');       // null
Settings::has('never-set');       // false

Settings::set('theme', null);     // this is a delete
Settings::save();                 // the entry is removed from the store

use BonsaiCms\Settings\SettingsFacade as Settings;

Settings::set('count', 1);
Settings::get('count');              // 1

Settings::set('ratio', 1.2);
Settings::get('ratio');              // 1.2

Settings::set('enabled', true);
Settings::get('enabled');            // true

Settings::has('enabled');            // true
Settings::has('missing');            // false

// Many at once
Settings::set([
    'a' => 'A',
    'b' => 'B',
    'c' => 'C',
]);

Settings::get(['a', 'b', 'x']);
// Collection(['a' => 'A', 'b' => 'B', 'x' => null])

Settings::all();
// Collection(['a' => 'A', 'b' => 'B', 'c' => 'C', ...])

Settings::save();

use Illuminate\Database\Eloquent\Model;
use BonsaiCms\Settings\Contracts\SerializationWrappable;
use BonsaiCms\Settings\Models\SerializableModelTrait;

class MyModel extends Model implements SerializationWrappable
{
    use SerializableModelTrait;
}

$model = MyModel::first();

Settings::set('model', $model);
Settings::save();

// Same request, or any later one:
Settings::get('model')->is($model);   // true

interface SerializationWrappable
{
    static function wrapBeforeSerialization($wrappable);

    static function unwrapAfterSerialization($wrappedClass, $wrappedValue);
}

use BonsaiCms\Settings\Contracts\SerializationWrappable;

class Money implements SerializationWrappable
{
    public function __construct(
        public readonly int $amount,
        public readonly string $currency,
    ) {}

    static function wrapBeforeSerialization($wrappable)
    {
        return [
            'amount' => $wrappable->amount,
            'currency' => $wrappable->currency,
        ];
    }

    static function unwrapAfterSerialization($wrappedClass, $wrappedValue)
    {
        return new Money($wrappedValue['amount'], $wrappedValue['currency']);
    }
}

Settings::set('price', new Money(1000, 'EUR'));
Settings::save();

Settings::get('price');   // Money(1000, 'EUR') — a new instance with the same state

->withMiddleware(function (Middleware $middleware) {
    $middleware->append([
        \BonsaiCms\Settings\Http\Middleware\LoadSettings::class,
        \BonsaiCms\Settings\Http\Middleware\SaveSettings::class,
    ]);
})

'default' => env('SETTINGS_DRIVER', 'database'),

'drivers' => [
    'database' => [
        'driver' => 'database',
        'connection' => env('SETTINGS_DATABASE_CONNECTION'),   // null = the app's default connection
        'table' => env('SETTINGS_DATABASE_TABLE', 'bonsaicms_settings'),
        'model' => BonsaiCms\Settings\Models\Setting::class,
    ],

    'redis' => [
        'driver' => 'redis',
        'connection' => env('SETTINGS_REDIS_CONNECTION', 'default'),
        'key' => env('SETTINGS_REDIS_KEY', 'bonsaicms_settings'),
    ],

    'file' => [
        'driver' => 'file',
        'path' => env('SETTINGS_FILE_PATH', storage_path('app/bonsaicms_settings.json')),
    ],

    'array' => [
        'driver' => 'array',
    ],
],

'drivers' => [
    'tenant_one' => ['driver' => 'redis', 'connection' => 'default', 'key' => 'settings_tenant_one'],
    'tenant_two' => ['driver' => 'redis', 'connection' => 'default', 'key' => 'settings_tenant_two'],
],

use BonsaiCms\Settings\Contracts\SettingsRepositoryFactory;

$repository = app(SettingsRepositoryFactory::class)->driver('tenant_two');

// Point the manager at it — note this shares the manager's cache, so refresh() first
Settings::refresh();
Settings::setRepository($repository);

'driver_implementations' => [
    // …the four above, plus:
    'dynamodb' => App\Settings\DynamoDbSettingsRepository::class,
],

'migrations' => [
    'driver' => env('SETTINGS_MIGRATION_DRIVER', 'database'),
],

'implementations' => [
    BonsaiCms\Settings\Contracts\SettingsManager::class           => BonsaiCms\Settings\SettingsManager::class,
    BonsaiCms\Settings\Contracts\SettingsSerializer::class        => BonsaiCms\Settings\SettingsSerializer::class,
    BonsaiCms\Settings\Contracts\SettingsDeserializer::class      => BonsaiCms\Settings\SettingsDeserializer::class,
    BonsaiCms\Settings\Contracts\SettingsRepositoryFactory::class => BonsaiCms\Settings\SettingsRepositoryFactory::class,
],

'throwExceptions' => [
    'serialize' => env('APP_DEBUG'),
    'deserialize' => env('APP_DEBUG'),
],
bash
php artisan vendor:publish --tag=settings-migrations
bash
php artisan migrate
bash
php artisan migrate --path=database/migrations/YYYY_MM_DD_HHMMSS_bonsaicms_create_settings_table.php
bash
php artisan vendor:publish --tag=settings-config