Configuration Manipulation With PHP Config - Different Strokes (
Page 4 of 9 )
I noted, on the previous page, that altering the second argument
to
writeConfig() significantly affects the format of the configuration file.
Let's look at that a little more closely, by trying out the different variants.
Consider the following rewrite of the previous example, which writes the
configuration data as a generic configuration file instead of a PHP
structure:
<?php
// configuration data
$mailConfig = array(
'name' => 'Bruce Banner',
'email' => 'hulk@angry.green.guy',
'host' => 'mail.apollo.domain'
);
// include class
include("Config.php");
// instantiate object
$c = new Config();
// read configuration data and get reference to root
$root =& $c->parseConfig($mailConfig, "PHPArray");
// write configuration to file as generic conf file $c->writeConfig("mail.conf",
"GenericConf");
?>
Here's what it looks like:
name:Bruce
Banner
email:hulk@angry.green.guy
host:mail.apollo.domain
As with the PHPArray type, you can customize the output format by specifying
the characters to be used for comments, delimiters and line endings. So, for
example, if you wanted the variable-value pairs separated by commas, you could
use this script,
<?php
// configuration data
$mailConfig = array(
'name' => 'Bruce Banner',
'email' => 'hulk@angry.green.guy',
'host' => 'mail.apollo.domain'
);
// include class
include("Config.php");
// instantiate object
$c = new Config();
// read configuration data and get reference to root
$root =& $c->parseConfig($mailConfig, "PHPArray");
// set options for delimiters
$options = array('equals' => ',');
// write configuration to file as generic conf file $c->writeConfig("mail.conf",
"GenericConf", $options);
?>
and the output would change to look like this:
name,Bruce
Banner
email,hulk@angry.green.guy
host,mail.apollo.domain
You can write your configuration data in Windows INI file format with the
following script,
<?php
// configuration data
$mailConfig = array(
'name' => 'Bruce Banner',
'email' => 'hulk@angry.green.guy',
'host' => 'mail.apollo.domain'
);
// include class
include("Config.php");
// instantiate object
$c = new Config();
// read configuration data and get reference to root
$root =& $c->parseConfig($mailConfig, "PHPArray");
// write configuration to file as INI file $c->writeConfig("mail.conf",
"IniFile");
?>
which produces this output:
name=Bruce
Banner
email=hulk@angry.green.guy
host=mail.apollo.domain
And finally, you can even write configuration data using the XML standard,
with the XML type (note that this requires the presence of two other XML classes
from PEAR, the Parser class and the Util class in order to work). Here's the
script,
<?php
// configuration data
$mailConfig = array(
'name' => 'Bruce Banner',
'email' => 'hulk@angry.green.guy',
'host' => 'mail.apollo.domain'
);
// include class
include("Config.php");
// instantiate object
$c = new Config();
// read configuration data and get reference to root
$root =& $c->parseConfig($mailConfig, "PHPArray");
// write configuration to file as XML file $c->writeConfig("mail.conf",
"XML");
?>
and here's the output:
<?xml version="1.0"
encoding="ISO-8859-1"?>
<name>Bruce
Banner</name>
<email>hulk@angry.green.guy</email>
<host>mail.apollo.domain</host>
If you know a little bit about XML, you'll know that is not really
well-formed XML, since the root element is missing. This can be easily
rectified; just add an option specifying the name of this root element to the
writeConfig(), as done earlier with the PHPArray type. Here's the revised
script,
<?php
// configuration data
$mailConfig = array(
'name' => 'Bruce Banner',
'email' => 'hulk@angry.green.guy',
'host' => 'mail.apollo.domain'
);
// include class
include("Config.php");
// instantiate object
$c = new Config();
// read configuration data and get reference to root
$root =& $c->parseConfig($mailConfig, "PHPArray");
// set root element
$options = array('name' => 'mailSettings');
// write configuration to file as XML file $c->writeConfig("mail.conf",
"XML", $options);
?>
and the revised, well-formed output:
<?xml version="1.0"
encoding="ISO-8859-1"?>
<mailSettings>
<name>Bruce
Banner</name>
<email>hulk@angry.green.guy</email>
<host>mail.apollo.domain</host>
</mailSettings>