NAME

ConfigReader 1.3 - A class to read and set variables from a configuration file


SYNOPSIS

    include("class.ConfigReader.php3");
    $config = new Config;
    $config->AUTOBOOL = true;
    $config->get_config("/path/and/name/of/my/config.file");

    $Var1    = $config->Var1;    // Is functionally equivalent to
    $Var1    = $config->param("Var1");

    // Automatically booleanizes variables
    if($config->param("bUseFile"))
    {
        // Yup..
    }


DESCRIPTION

ConfigReader is a simple class which reads, parses, and interpolates a configuration file for use in PHP3 programs.


METHODS


new("/path/and/name/of/config.file")

The only method you'll need to concern yourself with in ConfigReader is the new() method. It receives the full path and name of the configuration file you want to interpolate. When the new() method is complete, the configuration file has been read, interpolated, and the variables from it are ready for use. If your PHP version does not support new() with args, use the following method to instantiate a config opject;

    include("class.ConfigReader.php3");
    $config = new Config;
    $config->get_config("/path/and/name/of/config.file");
    // ... proceed as usual ...


booleanize($key)

New to version 1.1 is the booleanize() method. This method checks the value of the submitted key. If the value is ``Yes'' or ``True'' (case insensitive), then the variable is converted into a true boolean type and is set to ``true''. If the submitted key does NOT match it is booleanized and set to false. (effectively unsetting the variable). This can be done automatically by setting AUTOBOOL to true, and following the rules regarding boolean naming conventions. See AUTOBOOL for the specifics.


automatic arrays

New in 1.3 is the ability to create arrays from your configuration file. If a duplicate key is found in the configuration file, it will automatically become an array. All values associated with the configuration variable will become elements in that config vars array. Here's an example;

    MyArray    =    EntryZero
    bLogErrors =    true
    MyArray    =    EntryOne
    ErrorLog   =    /var/log/httpd/error_log
    MyArray    =    EntryTwo

When parsed, the above configuration variables will be set. Notice that the array variables can appear anywhere in your configuration file. They do not need to be sequential. The parsed array would look like this if you were to code it yourself;

    $config->MyArray = array( "EntryZero", "EntryOne", "EntryTwo" );

This is all done automatically and there's no way to turn it off. If you inadvertantly duplicate a configuration variable name (key) anywhere in your config file, it will be parsed and set as an array. If your duplicated configuration key happens to start with a 'b' and AUTOBOOL is set, the array will be destroyed in favor of a boolean value. (So check your key names for conflicts)


VARIABLES


AUTOBOOL (boolean, default == false)

New in version 1.2 is the AUTOBOOL variable. If set to true, the class will automatically call the booleanize() method whenever it encounters a properly formatted boolean key. Example;

    bVar1    =    Yes
    bVar2    =    No
    bVar3    =    true
    bVar4    =    false

Properly formatted keys must start with a lower-case letter 'b', and be immediately followed by an Upper-case letter. The proper values for a boolean key are ``true'' or ``yes''. (case insensitive). If the value is anything other than ``true'' or ``yes'', the boolean key is given a value of false.

To enable AUTOBOOL, you have to omit the config file from the new() method. Here is the proper procedure for creating a new ConfigReader object using AUTOBOOL;

    include("class.ConfigReader.php3");
    $config = new ConfigReader();

    $config->AUTOBOOL = true;

    $config->get_config("/path/to/config/file");

Note, if at any time you omit the path and file from a new ConfigReader() call (older versions of PHP require this), you must call get_config() to read and parse the configuration files contents.


CONFIG FILE


Configuration file format rules

Any line NOT beginning with a letter of the alphabet is completely ignored. (case insensitive [A-Za-z]) This allows for any commenting scheme you want, including simple whitespace characters.

Each line in the config file is read independantly. (ie, configuration vars must not span multiple lines)

Config files are in the format:

    [Key][whitespace+?]=[whitespace+?][Value][CR]

ConfigKeys cannot contain whitespace, (spaces or tabs) as they will automatically be stripped out.

ConfigValues CAN contain whitespace, but MUST be wrapped in single quote characters. (eg 'Hi There')

So the following are valid config settings: (the | signifies the beginning of a line)

    |CONFIGKEY=CONFIGVALUE
    |MyName   =    CDI
    |MyEmail  =    cdi@thewebmasters.net
    |ETag     =    '<A HREF="mailto:cdi@thewebmasters.net";>'

Notice that Values which contain whitespace must be wrapped inside single quotes. The single quotes will be stripped from the parsed results.

The following are INVALID config settings:

    | MyName    =    CDI     //    Ignored - leading whitespace on line

    |23skidoo   =    Wowzers //    Ignored - doesn't start with a letter

    |MyName          CDI     //    Will parse as MyNameCDI="" (null)

    |Rating     =    "The Avengers movie SUCKED"

    //   Will parse as: Rating="TheAvengersmovieSUCKED"


RETREIVING PARSED KEY/VALUE PAIRS

Here's an example;

    ------------------------------------------------------------

    include("class.ConfigReader.php3");

    $config = new Config("/path/and/name/of/my/config.file");

    $MyName  = $config->param["MyName"];  // The "hard" way
    $MyEmail = $config->param["MyEmail"];
    $ETag    = $config->param["ETag"];

    //  -OR-

    $MyName  = $config->MyName;           // The "easy" way.
    $MyEmail = $config->MyEmail;
    $ETag    = $config->ETag;             // Note: Case Sensitive keys

    ------------------------------------------------------------

As you can see, ConfigReader sets up an array ( param() ) of key=value pairs.

It also sets, for lack of a better term, a global variable with the same name as the key. If memory usage is a concern, you can simply unset($config->param); to remove the array from memory and just use the globals, or, you can step through the param array and clear the globals with unset($config->$key) just as easily. If your config file is large enough that it becomes a memory concern, you've already got more problems than I can help you with anyway.

Multiple configuration files can be read by creating different objects;

    include("class.ConfigReader.php3");

    $new = new Config("/my/new/config.file");
    $old = new Config("/my/old/config.file");

    if(empty($new->param))
    {
        echo "Errors: $new->ERROR \n";
        exit;
    }
    if(empty($old->param))
    {
        echo "Errors: $old->ERROR \n";
        exit;
    }

    unset($new->param);    // Lose the array, just use globals
    unset($old->param);

    $NewName = $new->NewName;
    $OldName = $old->NewName;    // Notice the vars don't conflict


ERRORS

Errors get reported to STDERR via error_log. If something goes wrong, grab it from there. The error text can also be retrieved from $ERROR as in;

    if(empty($config->param))
    {
        echo "oops: $config->ERROR \n";
        exit;
    }


DOCUMENTATION

You're reading it.


INSTALLATION

Copy the class file to the directory your include_path as defined in your php3.ini file. If you don't have an include_path set, drop this into the same directory with the rest of the calling programs source.


VERSION

Revision 1.3 04/03/1999 CDI cdi@thewebmasters.net


HISTORY

1.3 Added the automatic array() creation based on duplicate key names. Version released Apr 04, 1999.

1.2 Added the AUTOBOOL var, which if set, calls booleanize() automatically whenever a boolean variable is encountered. Version not released.

1.1 Added booleanize() method to convert boolean config vars to actual boolean types. Version not released.

1.0 Initial public release


AUTHOR

CopyRight (c) 1999 CDI, cdi@thewebmasters.net, All Rights Reserved

This program is free software; you can redistribute it and/or modify it under the GNU General Artistic License, with the following stipulations;

Changes or modifications must retain these Copyright statements. Changes or modifications must be submitted to the author.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Artistic License for more details. This software is distributed AS-IS.

Address Bug Reports or Comments to CDI, cdi@thewebmasters.net.

The latest version of this class should be available from the following locations:

http://www.thewebmasters.net/php/

What you're still here?