PHP code example of xy2z / lite-config

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

    

xy2z / lite-config example snippets



use xy2z\LiteConfig\LiteConfig as Config;

Config::loadArray([
  'version' => '1.0',
  'app' => [
      'name' => 'Example'
  ]
]);

echo Config::get('version'); # 1.0
echo Config::get('app'); # Array('name' => 'Example')
echo Config::get('app.name', 'default name'); # Example
echo Config::get('app.type', 'Desktop'); # Desktop

# config/settings.php
return [
  'app_name' => 'Example',
];

# index.php
Config::loadDir('config/', true);
echo Config::get('settings.app_name'); # key is 'filename.key'

# No key prefix
Config::loadFile('config/settings.php');
echo Config::get('key');

# Prefix filename to key
Config::loadFile('config/db.ini', true);
echo Config::get('db.key');

use xy2z\LiteConfig\LiteConfig as Config;
use Symfony\Component\Yaml\Yaml;

Config::loadArray(Yaml::parseFile(__DIR__ . '/config/file.yml'));

echo Config::get('key');

use xy2z\LiteConfig\LiteConfig;
use Symfony\Component\Yaml\Yaml;

class CustomLiteConfig extends LiteConfig {

    protected static function custom_handler(string $extension, string $path) {
        if (($extension === 'yml') || ($extension === 'yaml')) {
            return Yaml::parseFile($path);
        }

        // Handle other extensions here...
    }

}

CustomLiteConfig::loadDir(__DIR__ . '/config', true);
var_dump(CustomLiteConfig::all());