PHP code example of weew / config

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

    

weew / config example snippets


$loader = new ConfigLoader();
$loader->addPath('path/to/my/config');
$config = $loader->load();

// get config value
$config->get('key', 'defaultValue');

// set config value
$config->set('key', 'value');

// check if config is set
$config->has('key');

// remove config value
$config->remove('key');

// check if config is set, throws MissingConfigException
// optionally a type may be specified (string, int, array, etc..)
$config
    ->ensure('key', 'errorMessage')
    ->ensure('stringNode', 'errorMessage', 'string')
    ->ensure('arrayNode', 'errorMessage', 'array');

return [
    'key' => 'value',
    'list' => [
        'key' => 'value',
    ],
];

// config1
return [
    'list' => [
        'foo' => 'bar'
    ]
];

// config2
return [
    'reference' => 'foo {list.foo}'
];

// returns 'foo bar'
$config->get('reference');

// config1
return [
    'list' => [
        'foo' => 'bar'
    ]
];

// config2
return [
    'reference' => '{list}'
];

// returns ['foo' => 'bar']
$config->get('reference');

$loader->addRuntimeConfig(['my' => 'config']);

// or

$loader->addRunetimeConfig(new Config(['my' => 'config']));

$loader->setEnviroonment('prod');

$loader->getEnvironmentDetector()
    ->addEnvironmentRule('integ', ['integ', 'integration', 'stage']);

 $loader->getEnvironmentDetector()
     ->addIgnoreRule('sample', ['dist', 'ignore', 'sample']);
 

class MyDriver implements IConfigDriver {}

$loader->addDriver(new MyDriver());

class MyDetector implements IEnvironmentDetector {}

$loader->setEnvironmentDetector(new MyDetector());

- test
    - db.php
- prod
    - db.php
- config.php
- config_test.php
- config_prod.php