GithubHelp home page GithubHelp logo

clockwerx / config_lite Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pear/config_lite

1.0 3.0 0.0 3.22 MB

a lightweight class for ini style configuration/settings text files.

PHP 100.00%

config_lite's Introduction

Config_Lite

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 key/value pair (KVP) entries and optional sections, "[section]", followed by "name = value" (KVP) entries.

Examples

A simple 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:
$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

require_once 'Config/Lite.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
try {
	$config->save();
} catch (Config_Lite_Exception $e) {
    echo "\n", 'Exception Message: ', $e->getMessage();
}
Alternative file creation with write:
<?php

require_once 'Config/Lite.php';

$filename = 'test.ini';

$config = new Config_Lite();

try {
	$config->write($filename, array(
			'public_key_file' =>  "~/.ssh/id_rsa.pub",
			'general' => array(
				'lang' => 'fr'
			),
			'db' => array(
				'user' => 'dionysis',
				'password' => 'd0g1tcVs$HgIn1'
			)
		)
	);
} catch (Config_Lite_Exception $exception) {
    printf("Failed to write file: %s.\n", $filename);
    printf("Exception Message: %s\n", $exception->getMessage());
    printf("Exception Stracktrace: %s\n", $exception->getTraceAsString());
}
global Configuration options (without sections) :
$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'];
iterate (SPL Iterator) :
$config = new Config_Lite($filename);

foreach ($config as $section => $name) {
	if (is_array($name)) {
		$s.= sprintf("[%s]\n", $section);
		foreach ($name as $key => $val) {
			$s.= sprintf("\t%s = %s\n", $key, $val);
		}
	} else {
		$s.= sprintf("%s=%s\n", $section, $name);
	}
}

Options

setProcessSections(bool)

Sets whether or not sections should be processed If true, values for each section will be placed into a sub-array for the section. If false, all values will be placed in the global scope.

setQuoteStrings(bool)

Sets whether or not to doubleQuote If true, everything but bool and numeric values get doublequoted.

Notes & Limitations

  • Config_Lite is an OO frontend to `parse_ini_file' and writing ini files, but you can also use the public method `write' if you only want to write an array as ini file

  • 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

  • newline chars defaults to "\n", editable with `setLinebreak'

  • comments get dropped when writing after reading

  • no support of comments and multiline strings, because reading with `parse_ini_file' does not support it.

If you want to save userinput like images or a regex, iโ€™d recommend to use `get' with base64_decode and `set' with base64_encode.

Save regex (as global option) base64 encoded :
<?php

require_once 'Config/Lite.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);
}

IDEAS

  • Config_Lite_Parser with extended read and writefunctions (parse with Linereader), to support comments and multiline strings (both supported by Pear::Config)

Contributing

Patches are Welcome!
Create an Issue with a Link to your forked branch.

config_lite's People

Contributors

clockwerx avatar mfonda avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.