PHP code example of borschphp / config

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

    

borschphp / config example snippets




orsch\Config\Config;
use Borsch\Config\Reader\DotEnv;

$env = new DotEnv();
$env_data = $env->fromFile(__DIR__ . '/.env');

$config = new Config($env_data);

$key = $config->has('key') ? $config->get('key') : 'other_value';
$other = $config->getOrDefault('other', 'default_value');



orsch\Config\Aggregator;
use Borsch\Config\Config;
use Borsch\Config\Reader\DotEnv;
use Borsch\Config\Reader\Ini;
use Borsch\Config\Reader\Json;
use Borsch\Config\Reader\Yaml;

$ini = new Ini();
$ini_data = $ini->fromFile(__DIR__ . '/config.ini');

$env = new DotEnv();
$env_data = $env->fromFile(__DIR__ . '/.env');

$json = new Json();
$json_data = $json->fromFile(__DIR__ . '/config.json');

$yaml = new Yaml();
$yaml_data = $yaml->fromFile(__DIR__ . '/config.yaml');

$aggregator = new Aggregator(
    [
        $ini_data,
        $env_data,
        $json_data,
        $yaml_data,
        [
            'key' => 'value'
        ]
    ]
);

/** @var Config $config */
$config = $aggregator->getMergedConfig();

$key = $config->has('key') ? $config->get('key') : 'other_value';
$other = $config->getOrDefault('other', 'default_value');

$aggregator = new Aggregator(
    [
        Application\Config\MyConfigProvider::class
    ]
);



orsch\Config\Aggregator;
use Borsch\Config\Config;
use Borsch\Config\Provider\DotEnvProvider;
use Borsch\Config\Provider\IniProvider;
use Borsch\Config\Provider\JsonProvider;
use Borsch\Config\Provider\PhpFileProvider;
use Borsch\Config\Provider\YamlProvider;

$aggregator = new Aggregator(
    [
        new PhpFileProvider('config/production.*.config.php'),
        new JsonProvider('config/production.*.config.json'),
        new YamlProvider('config/production.*.config.y{a,}ml'),
        new IniProvider('config/production.*.config.ini'),
        new DotEnvProvider('config/.env.production.*'),
    ],
    cache_file: __DIR__.'/storage/cache/config.cache.php',
    use_cache: true
);

/** @var Config $config */
$config = $aggregator->getMergedConfig();

$key = $config->has('key') ? $config->get('key') : 'other_value';
$other = $config->getOrDefault('other', 'default_value');



orsch\Config\Aggregator;
use Borsch\Config\Config;
use Borsch\Config\Provider\IniProvider;

class DatabaseConfiguration
{
    private string $username;
    private string $password;
    private string $database;

    public function getUsername(): string { return $this->username; }
    public function setUsername(string $username): void { $this->username = $username; }

    public function getPassword(): string { return $this->password; }
    public function setPassword(string $password): void { $this->password = $password; }

    public function getDatabase(): string { return $this->database; }
    public function setDatabase(string $database): void { $this->database = $database; }
}

$aggregator = new Aggregator([
    new IniProvider('config/config.ini'),
]);

/** @var Config $config */
$config = $aggregator->getMergedConfig();

/** @var DatabaseConfiguration $db */
$dbConfig = $config->bind('database', DatabaseConfiguration::class);

echo $dbConfig->getUsername();