PHP code example of tobento / service-config

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

    

tobento / service-config example snippets


use Tobento\Service\Config\Config;
use Tobento\Service\Config\PhpLoader;
use Tobento\Service\Collection\Translations;
use Tobento\Service\Dir\Dirs;

// create config:
$config = new Config(new Translations());

// adding a loader:
$dirs = (new Dirs())->dir('home/private/config');

$config->addLoader(new PhpLoader($dirs));

// loading data from a file:
$config->load(file: 'app.php', key: 'app');

// or set data directly:
$config->set('database', ['name' => 'db_name']);

// Get config data:
$appName = $config->get('app.name');

$dbName = $config->get('database.name');

use Tobento\Service\Config\Config;
use Tobento\Service\Config\ConfigInterface;
use Tobento\Service\Collection\Translations;

$trans = new Translations();

$config = new Config($trans);

var_dump($config instanceof ConfigInterface);
// bool(true)

use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;

$config = new Config(new Translations());

$config->set('database', [
    'host' => 'localhost',
    'name' => 'db_name',
]);

$config->set('sitename', 'A sitename');

use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;

$config = new Config(new Translations());

$config->set('database', [
    'host' => 'localhost',
    'name' => 'db_name',
]);

// Add new data:
$config->set('database.driver', 'mysql');

// Set data (overwrites existing):
$config->set('database.name', 'db_name');

use Tobento\Service\Config\Config;
use Tobento\Service\Config\PhpLoader;
use Tobento\Service\Collection\Translations;
use Tobento\Service\Dir\Dirs;

// create config:
$config = new Config(new Translations());

// add loader:
$dirs = (new Dirs())->dir('home/private/config');

$config->addLoader(new PhpLoader($dirs));

// loading data:
$config->load(file: 'database.php', key: 'database');

return [
    'host' => 'localhost',
    'name' => 'db_name',
];

use Tobento\Service\Config\Config;
use Tobento\Service\Config\PhpLoader;
use Tobento\Service\Config\DataInterface;
use Tobento\Service\Collection\Translations;
use Tobento\Service\Dir\Dirs;

// create config:
$config = new Config(new Translations());

// add loader:
$dirs = (new Dirs())->dir('home/private/config');

$config->addLoader(new PhpLoader($dirs));

// just loading data:
$data = $config->load(file: 'database.php');

var_dump($data);
// array(3) { ... }

// or by the data method:
$data = $config->data(file: 'database.php');

var_dump($data instanceof DataInterface);
// bool(true)

use Tobento\Service\Config\Config;
use Tobento\Service\Config\JsonLoader;
use Tobento\Service\Collection\Translations;
use Tobento\Service\Dir\Dirs;

// create config:
$config = new Config(new Translations());

// add loader:
$dirs = (new Dirs())->dir('home/private/config');

$config->addLoader(new JsonLoader($dirs));

// loading data:
$config->load(file: 'database.json', key: 'database');

use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;

$config = new Config(new Translations());

$config->set('sitename', 'A sitename');
$config->set('app.name', 'An app name');

$sitename = $config->get('sitename');

// using dot notation:
$appname = $config->get('app.name');

use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;
use Tobento\Service\Config\ConfigNotFoundException;

$config = new Config(new Translations());

$sitename = $config->get('sitename', 'Default Sitename');

// would throw ConfigNotFoundException:
$sitename = $config->get('sitename');

use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;

$config = new Config(new Translations());

$sites = $config->get('sites', 'Sites');

// returns the default value:
$sites = $config->get('sites', ['first', 'second']);

// returns the value set as the same data type:
$sites = $config->get('sites', 'Default Sites');

use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;

$config = new Config(new Translations());

$config->set('sitename', 'A sitename');
$config->set('app.name', 'An app name');
$config->set('app.name', 'App Name', 'de');

var_dump($config->has('sitename'));
// bool(true)

// using dot notation:
var_dump($config->has('app.name'));
// bool(true)

// translated:
var_dump($config->has('app.name', 'de'));
// bool(true)

use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;

$trans = new Translations();
$trans->setLocaleFallbacks(['it' => 'en']);
$trans->setLocaleMapping(['en-Us' => 'en']);

$config = new Config($trans);

use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;

$config = new Config(new Translations());

// default locale:
$config->set('sitename', 'Sitename');

// de-CH locale:
$config->set('sitename', 'Seitenname', 'de-CH');

use Tobento\Service\Config\Config;
use Tobento\Service\Config\PhpLoader;
use Tobento\Service\Collection\Translations;
use Tobento\Service\Dir\Dirs;

// create config:
$config = new Config(new Translations());

// add loader:
$dirs = (new Dirs())->dir('home/private/config');

$config->addLoader(new PhpLoader($dirs));

// loading default data:
$config->load(file: 'site.php', key: 'site');

// loading de locale data:
$config->load(
    file: 'de/site.php',
    key: 'site',
    locale: 'de'
);

use Tobento\Service\Config\Config;
use Tobento\Service\Collection\Translations;

$config = new Config(new Translations());
$config->set('sitename', 'Sitename');
$config->set('sitename', 'Seitenname', 'de');

var_dump($config->get('sitename'));
// string(8) "Sitename"

var_dump($config->get(key: 'sitename', locale: 'de'));
// string(10) "Seitenname"

// returns default as locale does not exist
// and no other fallback is set:
var_dump($config->get(key: 'sitename', locale: 'it'));
// string(8) "Sitename"

// returns default value set as locale does not exist:
var_dump($config->get(key: 'sitename', default: 'Site It', locale: 'it'));
// string(7) "Site It"