Re: PEAR::Config question
| From: | Bertrand Mansion | Date: | Fri, 30 May 2003 08:24:17 +0000 |
| Subject: | Re: PEAR::Config question | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-5706@lists.php.net to get a copy of this message | ||
<webmaster@dyingculture.net> wrote :
> Hi Bertrand,
>
>> You are probably not using the class correctly.
>
> That's what I think too :-)
>
>> If you want more help, post some code reproducing the problem so I can
> have a look.
>
> Ok here it is :
>
> This part is in a file that is automatically prepended to every script:
> require_once('Config.php');
> $configuration = new Config;
> //$root =&
> $configuration->parseConfig("../includes/configuration_{$media}.xml",
> 'xml');
> $root =&
> $configuration->parseConfig("../includes/configuration_{$media}.php",
> 'phparray', array('name' => 'conf'));
> $conf = $root->toArray();
> $conf = $conf['root']['configuration'];
> unset($root);
>
> then I change some value in the $conf array and I use the following function
> to save the configuration file. (I do this because I have many forms for
> manipulating the configuration values)
>
> function sauver_configuration()
> {
> global $configuration;
> global $conf;
> $temp = new Config;
> $tableau = array();
> $tableau['configuration'] = $conf;
> $root =& $temp->parseConfig($tableau, 'phparray');
> $configuration->setRoot($root);
> return $configuration->writeConfig('testy.xml', 'xml'); // I save to
> another
> file for testing purposes but am I obliged to specify the filename if it is
> the same file from which we read the values ?
> }
>
> I think this is quite a bad way to use the class but I found no real help in
> the exampls and the documentation.
Indeed, it's messy. ;)
1. Config is a bit expensive, so you should use it mostly when you need to
manipulate configuration data, not to read them only, unless you use a cache
engine. The idea is to generate the configuration file with Config and once
it is configured, just read it using php functions like parse_ini_file(),
arrays, etc. Knowing that ini files are read faster then any other type of
configurations, even arrays (but offer less features too).
2. You can convert directly from a format to another using the Config
package, there is no need to manipulate the array yourself. Config is
actually a tree of containers that can be of type directive, section,
comment and blank. So you manipulate Config like any other trees.
In your case, something like that could do:
$configuration = new Config();
$configuration->parseConfig("conf_{media}.php", 'phparray');
$configuration->writeConfig("conf_{media}.xml", 'xml');
Now, if you want to be more specific:
$configuration = new Config();
$root =& $configuration->parseConfig("conf_{media}.php", 'phparray');
$mysection &= $root->getItem('section', 'mysection');
$newconfig = new Config();
$root =& $newconfig->getRoot();
$root->addItem($mysection);
$newconfig->writeConfig("conf_mysection.xml", 'xml');
Bertrand Mansion
Mamasam