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.. }
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()
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.
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)
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.
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"
------------------------------------------------------------
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
$ERROR
as in;
if(empty($config->param)) { echo "oops: $config->ERROR \n"; exit; }
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
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?