2017 © Pedro Peláez
 

library config_lite

Lightweight package for ini style configuration/settings text files

image

pear/config_lite

Lightweight package for ini style configuration/settings text files

  • Tuesday, July 4, 2017
  • by cweiske
  • Repository
  • 19 Watchers
  • 34 Stars
  • 5,726 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 17 Forks
  • 0 Open issues
  • 4 Versions
  • 12 % Grown

The README.md

Config_Lite

Build Status, (*1)

Description

a simple, lightweight and fast class for ini style configuration files, with the native PHP function `parse_ini_file' under the covers., (*2)

Config_Lite is inspired by Pythons ConfigParser., (*3)

A "Config_Lite" file consists of global key/value pair (KVP) entries and optional sections, "[section]", followed by "name = value" (KVP) entries., (*4)

Installation

Pear, (*5)

pear install Config_Lite-beta, (*6)

Composer, (*7)

Create a composer.json file and run composer install, (*8)

{
    "repositories": [
        {
            "type": "pear",
            "url": "pear.php.net"
        }
    ],
    "require": {
        "pear-pear.php.net/config_lite": "*"
    }
}

Composers autoload example usage, (*9)

<?php
require __DIR__ . '/../vendor/autoload.php';

$config = new Config_Lite;

Examples

A simple configuration file: `test.ini', (*10)


public_key_file = "~/.ssh/id_rsa.pub" debug = yes [general] lang = "en" [db] user = "dionysis" password = "c2oiVnY!f8sf"

Read Configuration file:, (*11)

<?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:, (*12)

<?php

require_once 'Config/Lite.php';

// write with file locking
$config = new Config_Lite('test.ini', LOCK_EX);

$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:, (*13)


$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:, (*14)

<?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());
}

Config without File - Streams, Filter or stdout:, (*15)

<?php

require_once 'Config/Lite.php';

$config = new Config_Lite();

$filename = sprintf(
    "php://filter/write=string.rot13/resource=%s", "test.ini"
);

$config->write($filename, array(
        'public_key_file' =>  "~/.ssh/id_rsa.pub",
        'general' => array(
        'lang' => 'fr'
    ),
    'db' => array(
        'user' => 'dionysis',
        'password' => 'd0g1tcVs$HgIn1'
        )
    )
);

// Writing to stdout
$config->write("php://stdout", array(
        'public_key_file' =>  "~/.ssh/id_rsa.pub",
        'general' => array(
        'lang' => 'fr'
    ),
    'db' => array(
        'user' => 'dionysis',
        'password' => 'd0g1tcVs$HgIn1'
        )
    )
);

global Configuration options (without sections) :, (*16)


$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'];

implemented IteratorAggregate, allows iteratation of the object with foreach:, (*17)


$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); } } echo $s;

Options

setProcessSections(bool), (*18)

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., (*19)

setQuoteStrings(bool), (*20)

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

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 comparison
  • 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
  • backslashes in doublequoted values get parsed, to omit this, use $config->read('/test.cfg', INI_SCANNER_RAW);
  • 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, i'd recommend to use get with base64_decode and set with base64_encode. For a regex one could also use setSingleTickDelimiter()., (*22)

Save regex (as global option) base64 encoded :, (*23)

<?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_Ini ( https://github.com/pce/Config_Lite_Ini ) with metainfos to supports global sections,
  • support of comments and multiline strings (both supported by Pear::Config)

Credits

  • @see contributors
  • mfonda ensured toString and output are equal, added accessors and options
  • CloCkWeRX improvements and Jenkins Integration
  • cweiske for various suggestions

Contributing

Patches are Welcome!, (*24)

Create an Issue with a Link to your forked branch., (*25)

https://github.com/pce/config_lite, (*26)

or report bugs at: http://pear.php.net/package/Config_Lite, (*27)

The Versions

04/07 2017

dev-master

9999999-dev http://pear.php.net/package/Config_Lite

Lightweight package for ini style configuration/settings text files

  Sources   Download

PHP-3.0

The Development Requires

by Patrick C. Engel

03/07 2017

dev-issue14

dev-issue14 http://pear.php.net/package/Config_Lite

Lightweight package for ini style configuration/settings text files

  Sources   Download

PHP-3.0

The Development Requires

by Patrick C. Engel

29/03 2016

dev-issue11

dev-issue11 http://pear.php.net/package/Config_Lite

Lightweight package for ini style configuration/settings text files

  Sources   Download

PHP-3.0

The Development Requires

by Patrick C. Engel

25/02 2016

dev-refactor-soc

dev-refactor-soc http://pear.php.net/package/Config_Lite

Lightweight package for ini style configuration/settings text files

  Sources   Download

PHP-3.0

The Development Requires

by Patrick C. Engel