PHP Classes

xconfig: Parse configuration values as key value pairs

Recommend this page to a friend!
  Info   Example   Demos   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 245 All time: 7,987 This week: 37Up
Version License PHP version Categories
xconfig 2.0.2GNU Lesser Genera...7PHP 5, Files and Folders, Systems adm..., C..., P...
Description 

Author

This class can parse configuration values defined as key value pairs.

It can take a string of configuration values on which each key is separate from the value using a = sign and there is one configuration value per line.

Lines starting with # are taken as comment lines, so they are ignored.

The parser can take an array of default values for pre-defined keys.

Configuration parameters can be retrieved like class variables. The class also provides an array interface that can be counted and iterated.

Picture of philippe thomassigny
  Performance   Level  
Name: philippe thomassigny <contact>
Classes: 6 packages by
Country: Mexico Mexico
Age: 55
All time rank: 120513 in Mexico Mexico
Week rank: 180 Up3 in Mexico Mexico Up
Innovation award
Innovation award
Nominee: 1x

Example

<?php

echo '<h1>XConfig examples</h1>';

include_once
'../include/xconfig/XConfig.class.php';

$config = new \xconfig\XConfig(file_get_contents('myconfig.conf'), array('defparam1' => 'defvalue1', 'defparam2' => 'defvalue2', 'defparam3' => 'defvalue3'));

echo
'<h2>content of $config:</h2>';
print
$config;
echo
'<br />';

echo
'<h3>get a parameter to a local variable:</h3>';
$param1 = $config->parameter1;
echo
$param1 . '<br />';
echo
'<br />';

echo
'<h3>use directly a parameter into a sentence:</h3>';
foreach(
$config->parameter2 as $p)
  echo
$p . '<br />';
echo
'<br />';

echo
'<h3>set a new parameter:</h3>';
echo
'parameter3=value3<br />';
$config->parameter3 = 'value3';
echo
'<br />';

echo
'<h3>new content of $config:</h3>';
print
$config;
echo
'<br />';

echo
'<h3>iterate the $config:</h3>';
print
$config;
echo
'<br />';
 
echo
'<h2>Merge two config files:</h2>';

$globalconfig = new \xconfig\XConfig(file_get_contents('myglobalconfig.conf'));
$localconfig = new \xconfig\XConfig(file_get_contents('mylocalconfig.conf'));
$globalconfig->merge($localconfig);

echo
'<h3>Content of merged configuration:</h3>';
print
$globalconfig;
echo
'<br />';

?>


Details

XConfig

Build Status

Version 2.0.2 The XConfig library is used to easily build a config object based on a descriptor file.

Change History

v2.0.2: - Error corrected on static NEWLINE string

v2.0.1: - Some errors corrected in Iterator (was stopped if there was a false value in the parameter) - constructor and merge method enhanced

v2.0.0: - Now compatible with PHP7 - Now uses namespace xconfig

v1.1.0: - Compiler now accept ';' as comment - Better compiler, respect '=' after first occurence as value - Compiler now converts 'true', 'yes', 'on' to true and 'false', 'no', 'off', 'none' to false

v1.0.2: - Default values added - __print function made to print better the variables - manual more complete

v1.0.1: - Original release

User guide

The configuration file have the following syntax for example:

# this file is named myconfig.conf, used in following examples
# the # denotes a comment.
; is also a comment
parameter1=value1
parameter2=value2
parameter2=value3

As version 1.1, xconfig now accept true, on, yes as a boolean 'true' and false, off, no, none as a boolean 'false'. For instance, that means parameter=off is now a boolean false, and parameter=yes is now a boolean true.

Before verion 1.0, note the config file is always read as a STRING. That means parameter=0, parameter=false, parameter=123 will be caught as "0", "false", "123", not integers or booleans

This will be converted into the XConfig object. The XConfig object is easily usable as:

$config = new XConfig('String of the config file');

or

$config = new XConfig(Array of parameters);

Concrete Example 1:

include_once 'include/xconfig/XConfig.class.php');
$config = new XConfig(file_get_contents('myconfig.conf'));

Concrete Example 2:

include_once 'include/xconfig/XConfig.class.php');
$config = new XConfig(array(
    'parameter1' => 'value1',
    'parameter2' => array('value2', 'value3')
  ));

Once you have an instance of your configuration, you may use it like this:

// assign a local variable
$param1 = $config->parameter1;
echo $param1 . '<br />';

// use directly the variable
foreach($config->parameter2 as $p)
  echo $p . '<br />';

// set a new parameter
$config->parameter3 = 'value3';

// iterate the config
foreach($config as $parameter => $value)
  echo $parameter . ' = ' . $value . '<br />';

Advanced topic

Default values:

You may pass an array of default values to the constructor so if the parameter is not present into the config file, it will take the default value. Note: default values will be taken only if the parameter DOES NOT EXIST into the config file. This means an empty value is considerated as a value

Something like this: parameter1= will not fire the default value because the parameter is present into the config file

You may encapsulate the config object into a specific personal object with a local default set of parameters.

Example:

class myConfig extends XConfig { private $default = array(

'parameter1' => 'default1'

);

public function __construct($data) {

parent::__construct($data, $this->default);

} }

Merging:

You may merge two config file (or more), for example when you have a master config file and a local replacement values config file:

include_once 'include/xconfig/XConfig.class.php');
$globalconfig = new XConfig(file_get_contents('myglobalconfig.conf'));
$localconfig = new XConfig(file_get_contents('mylocalconfig.conf'));
$globalconfig->merge($localconfig);

with files:

#global config:
ip=127.0.0.1
port=80
domain=test.com
#local config:
port=8080
title=Welcome

The result config after merging local into global will be:

ip=127.0.0.1
port=8080
domain=test.com
title=Welcome


  Demo XConfigExternal page  
  Files folder image Files (6)  
File Role Description
Files folder imageexample (4 files)
Files folder imageinclude (1 directory)
Accessible without login Plain text file README.md Doc. Auxiliary data

  Files folder image Files (6)  /  example  
File Role Description
  Accessible without login Plain text file index.php Example Example script
  Accessible without login Plain text file myconfig.conf Data Auxiliary data
  Accessible without login Plain text file myglobalconfig.conf Data Auxiliary data
  Accessible without login Plain text file mylocalconfig.conf Data Auxiliary data

  Files folder image Files (6)  /  include  
File Role Description
Files folder imagexconfig (1 file)

  Files folder image Files (6)  /  include  /  xconfig  
File Role Description
  Plain text file XConfig.class.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:245
This week:0
All time:7,987
This week:37Up