PHP code example of jasny / config

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

    

jasny / config example snippets


use Jasny\Config;

$config = (new Config())
  ->load('settings.yml')
  ->load('settings.dev.yml', ['optional' => true]);

use Symfony\Component\Yaml;

$parser = new class() extends Yaml\Parser() {
    public function parse($value, $options = 0) {
        return parent::parseFile($value, $options | Yaml\Yaml::PARSE_CONSTANT); 
    }
}

$yamlLoader = new YamlSymfonyLoader($options, $parser);

$map = [
    "APP_SECRET" => "secret",
    "APP_DATABASE_PASSWORD" => "db.password"
];

$config->load('env', ['map' => $map]);


$dynamodb = Aws\DynamoDb\DynamoDbClient::factory([
    'region' => 'eu-west-1',
    'version' => '2012-08-10'
]);
 *
$config = new Jasny\Config();

$config->load($dynamodb, [
   'table' => 'config',
   'key_field' => 'key',
   'key_value' => 'myapp'
]);

use Jasny\Config\Loader;

$loader = new Loader\DelegateLoader([
    'yaml' => new Loader\YamlLoader(['base_path' => __DIR__ . '/config']),
    'json' => new Loader\JsonLoader(['base_path' => __DIR__]) 
]);

$config = new Config([], $loader);
$config->load('composer.json');
$config->load('settings.yml');

$config = new Jasny\Config([
    'foo' => 'bar',
    'db' => [
        'host' => 'localhost',
        'username' => 'root',
        'password' => 'god'
    ]
]);

$config = (new Config())
    ->load('composer.json')
    ->load('config/settings.yml')
    ->load($dynamoDB, ['table' => 'config', 'key_value' => 'myapp']);

$config->get('foo'); // bar
$config->get('db.host'); // localhost

// throws a NotFoundException
$config->get('something_random');

$config = Config::loadFromScript('tmp/settings.php');

if (!isset($config)) {
    $config = (new Config())
        ->load('composer.json')
        ->load('config/settings.yml');
        
    $config->saveAsScript('tmp/settings.php');
}