Proposal for "Config_Lite"

» Metadata » Status
  • Category: Configuration
  • Proposer: Patrick Engel 
  • License: LGPL
» Description

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.
» Dependencies » Links
» Timeline » Changelog
  • First Draft: 2010-08-31
  • Proposal: 2010-09-09
  • Call for Votes: 2011-01-16
  • Voting Extended: 1970-01-01
  • Patrick Engel
    [2010-09-19 17:09 UTC]

    - Code beautified, fits PEAR Coding Standard (according to phpcs).
    http://code.pc-e.org/pear/Config_Lite/Lite.phps

    - throws Config_Lite_Exception, which inherits Exception, updated the unit Tests:
    http://code.pc-e.org/pear/Config_Lite/tests/AllTests.phps

  • Patrick Engel
    [2010-09-24 11:47 UTC]

    • updated the Config_Lite Package (used pear to create it),
    • throws Exception, no dependencies,
    • $ phpunit AllTests.php works,
    • phpcs is still quite,
    • package validated
      • to run the phpunit AllTests.php, i used a "if Condition",

    like PHP_CodeSniffer.

    Is it valid to test if it is installed,
    also in the Package's `Lite.php'?

    Should we better throw an Exception that inherits PEAR_Exception?

  • Patrick Engel
    [2010-12-01 04:28 UTC]

    - draft: https://github.com/pce/config_lite
    - added static autoload method
    - added (set|get)String with (add|strip)slashes
    - Config_Lite_Exception is a Interface, and is implemented by Config_Lite_Exceptions which inherits SPL Excpetions,

  • Patrick Engel
    [2011-01-07 23:36 UTC]

    - added suggested global options with null by cw,
    - updated the examples, tests and docblocks,
    - also validated and updated the Pear package (with the build script of the gitrepo)

  • Thies C. Arntzen
    [2024-01-18 18:02 UTC]