PHP code example of alopez / php-configuration-loader
1. Go to this page and download the library: Download alopez/php-configuration-loader 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/ */
alopez / php-configuration-loader example snippets
// A random configuration file lost in your directories
$name = 'foo';
$value = 'fooValue';
return new \ConfigurationLoader\SingleConfiguration($name, $value);
// Databases definitions
use ConfigurationLoader\GroupConfiguration;
use ConfigurationLoader\SingleConfiguration;
$name = 'db';
return new GroupConfiguration($name,
[
new SingleConfiguration('database1',
[
'host' => 'bla',
'user' => 'bla',
'password' => 'bla',
'bla' => 'bla'
]
),
new SingleConfiguration('database2',
[
'host' => 'bla2',
'user' => 'bla2',
'password' => 'bla2',
'bla' => 'bla2'
]
)
]
);
$loader = new \ConfigurationLoader\ConfigurationLoader($configPath, $lazyOrNot);
// The path must be considering that the current directory is your conf folder,
// You must not precise '.php' extension.
$filepath = 'path/to/your/file/without/.extension';
// If not lazy you have to load the file (it will also return the values you want to get)
// Let's say you want to load the file containing our newly created 'foo' SingleConfiguration:
$data = $loader->load($filepath);
// will echo 'fooValue'
var_dump($data);
// Once data is already load you can use get in order to get the configuration
// It also will return 'fooValue'
$data = $loader->get('foo')
$loader->load('dbconf');
$data = $loader->get('db');
var_dump($data);
// var_dump will return a formatted array with the following format:
array(
'database1' => array(...),
'database2' => array(...)
)
// foo.php
return new \ConfigurationLoader\SingleConfiguration('foo', 'foo');
// foo.local.php
return new \ConfigurationLoader\SingleConfiguration('foo', 'bar');
// index.php
$loader = new \ConfigurationLoader\ConfigurationLoader($configPath);
$loader->load('foo');
// will return 'bar'
var_dump($loader->get('foo'));
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.