a simple, lightweight and fast class for ini style configuration files, with the native PHP function "parse_ini_file" under the hood.
Config_Lite is inspired by Pythons ConfigParser.
A "Config_Lite" file consists of global options as "name = value" entries on top and sections, "[section]", followed by "name = value" entries.
Examples
The Config_Lite Pear Package:
https://github.com/pce/config_lite
A Configuration File: `test.ini'
public_key_file = "~/.ssh/id_rsa.pub"
debug = yes
[general]
lang = "en"
[db]
user = "dionysis"
password = "c2oiVnY!f8sf"
Read Configuration file:
<?php
require_once 'Config/Lite.php';
$config = new Config_Lite('test.ini');
echo $config->get('db', 'user'); // dionysis
echo $config->get(null, 'public_key_file'); // ~/.ssh/id_rsa.pub
if (true === $config->getBool(null, 'debug', true)) {
echo $config;
}
// read with ArrayAccess
echo $config['db']['password']; // c2oiVnY!f8sf
?>
Save Configuration file:
<?php
$config->set('db', 'user', 'JohnDoe')
->set('db', 'password', 'd0g1tcVs$HgIn1');
// set with ArrayAccess
$config['public_key_file'] = '~/.ssh/id_rsa.pub';
$config['general'] = array('lang' => 'de');
// save object to file
$config->save();
?>
Create configuration file:
<?php
$config = new Config_Lite('test.ini');
$config->set('db', 'user', 'JohnDoe')
->set('db', 'password', 'd0g1tcVs$HgIn1');
// set global bool 'debug'
$config->set(null, 'debug', false);
// save object to file
$config->save();
?>
Alternative file creation with write:
<?php
$config = new Config_Lite();
$config->write('test.ini', array(
'public_key_file' => "~/.ssh/id_rsa.pub",
'general' => array(
'lang' => 'fr'
),
'db' => array(
'user' => 'dionysis',
'password' => 'd0g1tcVs$HgIn1'
)
);
?>
global Configuration options (without sections) :
<?php
$config->set(null, 'private_key_file', '~/.ssh/id_rsa');
// set with arrayaccess
$config['public_key_file'] = '~/.ssh/id_rsa.pub';
$config->sync();
echo $config->get(null, 'public_key_file');
// get with arrayaccess
echo $config['private_key_file'];
?>
Notes & Limitations
Use getString and setString to save and read Strings with double and single-quotes.
Use getBool if you need a real bool type, eg. for strict equality comparision.
The methods `set' and `get' keep values untouched, but the write method normalize "bool" values to a human readable representation, doublequotes strings and numeric values without any quotes.
File locking is not part of this Class.
No support of comments and multiline strings, because reading with "parse_ini_file" does not support it.
If you want to save a regex, i'd recommend to use "get" with base64_decode and "set" with base64_encode.
Save regex (as global option) base64 encoded :
<?php
$config = new Config_Lite('regex-test.ini');
$regex = '/Hello \"(.*?)\"/';
$config->set(null, 'regex', base64_encode($regex));
// save object, here sync to read it back, just to test
$config->sync();
// in 'regex-test.ini': regex = "L0hlbGxvIFwiKC4qPylcIi8="
$regex = base64_decode($config->get(null, 'regex'));
if (preg_match($regex, 'Hello "World"!')) {
printf("matched. regex:%s", $regex);
} else {
printf("no match found. regex:%s", $regex);
}
?>
Game MENANG123 merupakan game gacor terpercaya. |